滚动特效的实现
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
<style>
* {
margin: 0;
padding: 0;
}
.box {
width: 1000px;
height: 130px;
border: 1px solid #000;
overflow: hidden;
position: absolute;
top: 50%;
left: 50%;
margin-top: -65px;
margin-left: -500px;
}
.box ul {
list-style: none;
width: 5000px;
position: relative;
}
.box ul li {
float: left;
margin-right: 10px;
}
</style>
</head>
<body>
<div id="box" class="box">
<ul id="list">
<li><img src="images/number/0.png" alt="" /></li>
<li><img src="images/number/1.png" alt="" /></li>
<li><img src="images/number/2.png" alt="" /></li>
<li><img src="images/number/3.png" alt="" /></li>
<li><img src="images/number/4.png" alt="" /></li>
<li><img src="images/number/5.png" alt="" /></li>
</ul>
</div>
<script>
let box = document.getElementById("box");
let list = document.getElementById("list");
list.innerHTML += list.innerHTML;
let left = 0;
let timer;
move();
function move() {
clearInterval(timer);
timer = setInterval(function () {
left -= 4;
if (left <= -1260) {
left = 0;
}
list.style.left = left + "px";
}, 20);
}
box.onmouseenter = function () {
clearInterval(timer);
};
box.onmouseleave = function () {
move();
};
</script>
</body>
</html>
轮播图的实现
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
<style>
* {
margin: 0;
padding: 0;
}
.carousel {
width: 650px;
height: 360px;
border: 1px solid #000;
margin: 50px auto;
position: relative;
overflow: hidden;
}
.carousel ul {
list-style: none;
width: 6000px;
position: relative;
left: 0px;
transition: left 0.5s ease 0s;
}
.carousel ul li {
float: left;
}
.carousel .leftbtn {
position: absolute;
left: 20px;
top: 50%;
margin-top: -25px;
width: 50px;
height: 50px;
background-color: rgb(28, 180, 226);
border-radius: 50%;
}
.carousel .rightbtn {
position: absolute;
right: 20px;
top: 50%;
margin-top: -25px;
width: 50px;
height: 50px;
background-color: rgb(28, 180, 226);
border-radius: 50%;
}
</style>
</head>
<body>
<div class="carousel">
<ul id="list">
<li><img src="images/beijing/0.jpg" alt="" /></li>
<li><img src="images/beijing/1.jpg" alt="" /></li>
<li><img src="images/beijing/2.jpg" alt="" /></li>
<li><img src="images/beijing/3.jpg" alt="" /></li>
<li><img src="images/beijing/4.jpg" alt="" /></li>
</ul>
// 给href赋值一个空javascript函数防止刷新页面
<a href="javascript:;" class="leftbtn" id="leftbtn"></a>
<a href="javascript:;" class="rightbtn" id="rightbtn"></a>
</div>
<script>
let leftbtn = document.getElementById("leftbtn");
let rightbtn = document.getElementById("rightbtn");
let list = document.getElementById("list");
let cloneli = list.firstElementChild.cloneNode(true);
list.appendChild(cloneli);
let idx = 0;
let lock = true;
rightbtn.onclick = function () {
if (!lock) return;
lock = false;
list.style.transition = "left .5s ease 0s";
idx++;
if (idx > 4) {
setTimeout(function () {
list.style.transition = "none";
list.style.left = 0;
idx = 0;
}, 500);
}
list.style.left = -idx * 650 + "px";
setTimeout(function () {
lock = true;
}, 500);
};
leftbtn.onclick = function () {
if (!lock) return;
lock = false;
if (idx == 0) {
list.style.transition = "none";
list.style.left = -5 * 650 + "px";
setTimeout(function () {
list.style.transition = "left .5s ease 0s";
idx = 4;
list.style.left = -idx * 650 + "px";
}, 0);
} else {
idx--;
list.style.left = -idx * 650 + "px";
}
setTimeout(function () {
lock = true;
}, 500);
};
</script>
</body>
</html>