第一张图片切换到第二张为啥没有动画效果的
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>简易轮播图</title>
<style>
* {
margin: 0;
padding: 0;
}
.box {
width: 650px;
height: 360px;
border: 1px solid #000;
margin: 100px auto;
position: relative;
overflow: hidden;
}
.box>#uls {
width: 6000px;
list-style: none;
position: relative;
transition: left .5s ease;
}
.box>#uls>li {
float: left;
}
.box #btn-left,
.box #btn-right {
position: absolute;
top: 50%;
transform: translateY(-50%);
font-size: 32px;
background: rgba(0, 0, 0, .5);
color: #fff;
text-decoration: none;
border-radius: 5px;
width: 40px;
height: 40px;
line-height: 40px;
text-align: center;
}
.box #btn-left {
left: 0;
}
.box #btn-right {
right: 0;
}
</style>
<script>
window.addEventListener('load', function () {
var btnLeft = document.getElementById('btn-left');
var btnRight = document.getElementById('btn-right');
var uls = document.getElementById('uls');
var firstLi = uls.getElementsByTagName('li')[0];
var cloneChild = firstLi.cloneNode(true);
uls.appendChild(cloneChild)
var index = 0;
var lock = true;
btnRight.onclick = function () {
if (!lock) return;
lock = false;
index++;
if (index > 4) {
setTimeout(() => {
index = 0;
uls.style.transition = 'none';
uls.style.left = -index * 650 + 'px';
}, 500);
}
uls.style.left = -index * 650 + 'px';
uls.style.transition = 'left .5s ease';
setTimeout(() => {
lock = true;
}, 500);
};
btnLeft.onclick = function () {
if (!lock) return;
lock = false;
if (index == 0) {
index = 5;
uls.style.left = -index * 650 + 'px';
uls.style.transition = 'none';
setTimeout(() => {
index = 4;
uls.style.left = -index * 650 + 'px';
uls.style.transition = 'left .5s ease';
}, 0);
} else {
index--;
uls.style.left = -index * 650 + 'px';
uls.style.transition = 'left .5s ease';
}
setTimeout(() => {
lock = true;
}, 500);
}
})
</script>
</head>
<body>
<div id="box" class="box">
<ul id="uls">
<li><img src="./images/0.jpg" alt=""></li>
<li><img src="./images/1.jpg" alt=""></li>
<li><img src="./images/2.jpg" alt=""></li>
<li><img src="./images/3.jpg" alt=""></li>
<li><img src="./images/4.jpg" alt=""></li>
</ul>
<a href="javascript:;" id="btn-left"><</a>
<a href="javascript:;" id="btn-right">></a>
</div>
</body>
</html>
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星