6-3 编写跑马灯轮播图特效

来源:6-3 编写跑马灯轮播图特效

慕移动4506339

2022-11-15 15:51:41

<!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>
</head>
<style>
* {
margin: 0;
padding: 0;
}

#carosol {
margin: 40px auto;
width: 650px;
height: 360px;
border: 1px solid black;
position: relative;
overflow: hidden;
}

ul {
list-style: none;
width: 5000px;
position: relative;
left: 0;
transition: left 0.5s ease 0s;
}

li {
float: left;
}

div .btn {
width: 50px;
height: 50px;
border-radius: 50%;
background-color: rgba(20, 20, 190, .4);
position: absolute;
top: 50%;
margin-top: -25px;

}

.btn#leftbtn {
left: 10px;
}

.btn#rightbtn {
right: 10px;
}
</style>

<body>
<div id="carosol">
<ul id="list">
<li><a href="javascript:;"><img src="images/0.jpg"> </a></li>
<li><a href="javascript:;"><img src="images/1.jpg"> </a></li>
<li><a href="javascript:;"><img src="images/2.jpg"> </a></li>
<li><a href="javascript:;"><img src="images/3.jpg"> </a></li>
<li><a href="javascript:;"><img src="images/4.jpg"> </a></li>
</ul>
<div id="leftbtn" class="btn"></div>
<div id="rightbtn" class="btn"></div>
</div>
<script>
var list = document.getElementById('list');
var box = document.getElementById('carosol');
var leftbtn = document.getElementById('leftbtn');
var rightbtn = document.getElementById('rightbtn');
//克隆第一个li节点
var clonenode = list.firstElementChild.cloneNode(true);
list.appendChild(clonenode);
var idx = 0;
var l;
var lock = true;
rightbtn.onclick = function () {
//函数节流
if (!lock) return;
lock = false;
list.style.transition = 'left 0.5s ease 0s';
idx++;
l = -650 * idx;
list.style.left = l + 'px';
if (idx > 4) {
var timer = setTimeout(function () {
list.style.transition = 'none';
list.style.left = 0;
idx = 0;
}, 500)
}
setTimeout(function () {
lock = true;
}, 500)
}
leftbtn.onclick = function () {
if (!lock) return;
lock = false;
if (idx == 0) {
list.style.transition = 'none';
list.style.left = -650 * 5 + 'px'
idx = 5;
setTimeout(function () {
list.style.transition = 'left 0.5s ease 0s';
idx = 4;
list.style.left = -650 * 4 + 'px';
}, 0);
} else {
idx--;
list.style.left = -650 * idx + 'px';
}
setTimeout(function () {
lock = true;
}, 500)
}


</script>
</body>

</html>

老师,检查下代码是否有问题

写回答

1回答

好帮手慕久久

2022-11-15

同学你好,代码正确,很棒,祝学习愉快!

0

0 学习 · 15276 问题

查看课程