JS练习_轮播图,实现滚动效果
源码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>DOM操作轮播图</title>
<style>
.swiper{
width: 640px;
height: 320px;
overflow: hidden;
position: relative;
}
.images-container{
width: 1920px;
height: 320px;
display: flex;
transition: transform 0.3s;
}
.images-container img{
width: 640px;
height: 320px;
}
/*.images-container:hover{*/
/* transform: translate(-640px);*/
/*}*/
.number-list{
position: absolute;
bottom: 0;
}
</style>
</head>
<body>
<div class="swiper">
<div class="images-container">
<img src="images/桌面壁纸1.jpg" alt="">
<img src="images/桌面壁纸2.jpg" alt="">
<img src="images/桌面壁纸3.jpg" alt="">
</div>
<div class="number-list">
<button>1</button>
<button>2</button>
<button>3</button>
</div>
</div>
<script>
let btnlist = document.querySelectorAll(".number-list button");
let container = document.querySelector(".images-container");
for (let i in btnlist) {
btnlist[i].onclick = function (){
container.style.transform = `translate(${-640 * i}px)`;
}
}
</script>
</body>
</html>
把最实用的经验,分享给最需要的读者,希望每一位来访的朋友都能有所收获!