使用js制作轮播图

在学习js时写的轮播图实例,效果为平移轮播

通过设置定时器和监听鼠标事件来控制图片的切换
使用 CSS 过渡效果和偏移量来实现平滑的切换动画效果
根据当前显示的图片索引来改变对应的圆点背景颜色。

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1">
<title></title>
<style>
.banner{
width: 100%;
height: 300px;
overflow: hidden;
}
.list{
width: 400%;
height: 300px;
display: flex;
}
 
.list img{
width: 25%;
height: 300px;
}
 
.box{
width: 100%;
height: 300px;
position: relative;
}
.but{
width: 100%;
height: 50px;
position: absolute;
top: 125px;
display: flex;
justify-content: space-between;
}
.but div{
width: 50px;
height: 50px;
background-color: rgba(0, 0, 0, 0.5);
color: #fff;
line-height: 50px;
text-align: center;
font-size: 20px;
}
.iocn{
width: 100%;
height: 30px;
position: absolute;
bottom: 0;
display: flex;
justify-content: center;
}
.iocn span{
display: block;
width: 10px;
height: 10px;
margin: 10px;
border-radius: 100%;
background-color: rgba(0,0,0,0.5);
}
</style>
</head>
<body>
 
<div class="box">
 
<div class="banner">
<div class="list">
<img src="img/1.jpg" alt="">
<img src="img/11.JPG" alt="">
<img src="img/111.JPG" alt="">
<img src="img/1111.jpg" alt="">
</div>
</div>
 
<div class="but">
<div> < </div>
<div> > </div>
</div>
 
<div class="iocn">
<span style="background-color: rgba(255,0,0,0.5);"></span>
<span></span>
<span></span>
</div>
</div>
 
<script>
let box = document.getElementsByClassName('box')[0];
let list = document.getElementsByClassName("list")[0];
let img = list.children;
let imgWidth = img[0].offsetWidth;
let k = 0;
let int = setInterval(scroll,2000);
let iocn = document.getElementsByClassName('iocn')[0].children;
let but = document.getElementsByClassName('but')[0].children;
 
function scroll(){
for(let i = 0; i < iocn.length; i++){
iocn[i].style.backgroundColor = "rgba(0, 0, 0, 0.5)"
}
k++;
list.style.transition = "all 1s";
list.style.marginLeft = "-" + k * imgWidth + "px";
if(k == img.length - 1){
iocn[0].style.backgroundColor = "rgba(255, 0, 0, 0.5)"
setTimeout(function(){
k = 0;
list.style.transition = "";
list.style.marginLeft = "0px";
},1000)
}else{
iocn[k].style.backgroundColor = "rgba(255, 0, 0, 0.5)"
}
}
 
box.onmouseover = function(){
clearInterval(int);
}
 
box.onmouseout = function(){
int = setInterval(scroll,2000);
}
 
but[0].onclick = function(){
for(let i = 0; i < iocn.length; i++){
iocn[i].style.backgroundColor = "rgba(0, 0, 0, 0.5)"
}
 
if(k == 0){
k = img.length-1;
list.style.transition = "";
list.style.marginLeft = "-" + k * imgWidth + "px";
iocn[img.length-2].style.backgroundColor = "rgba(255, 0, 0, 0.5)"
}
 
setTimeout(function(){
k--;
list.style.transition = "all 1s";
iocn[k].style.backgroundColor = "rgba(255, 0, 0, 0.5)"
list.style.marginLeft = "-" + k * imgWidth + "px";
},100)
 
}
but[1].onclick = scroll;
</script>
</body>
</html>

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
以下是使用DW和JS制作的步骤: 1. 在DW中新建一个HTML文件,并在文件中添加一个div元素,用于容纳轮。 2. 在HTML文件中引入CSS和JS文件。 3. 在CSS文件中设置轮的样式,例如宽度、高度、边框等。 4. 在JS文件中编写轮的代码。可以使用setInterval()函数来实现片的自动切换,也可以使用事件监听器来实现用户手动切换片。 5. 将JS代码插入到HTML文件中,确保JS代码在div元素之后。 6. 在HTML文件中添加片元素,并设置片的路径和alt属性。 7. 运行HTML文件,查看轮效果。 以下是一个简单的DW和JS制作的代码示例: ```html <!DOCTYPE html> <html> <head> <title>JS</title> <link rel="stylesheet" type="text/css" href="style.css"> <script type="text/javascript" src="script.js"></script> </head> <body> <div id="slider"> <img src="img/1.jpg" alt="片1"> <img src="img/2.jpg" alt="片2"> <img src="img/3.jpg" alt="片3"> </div> </body> </html> ``` ```css #slider { width: 500px; height: 300px; border: 1px solid #ccc; position: relative; overflow: hidden; } #slider img { position: absolute; top: 0; left: 0; } #slider img:not(:first-child) { display: none; } ``` ```javascript var slider = document.getElementById('slider'); var images = slider.getElementsByTagName('img'); var index = 0; setInterval(function() { images[index].style.display = 'none'; index = (index + 1) % images.length; images[index].style.display = 'block'; }, 2000); ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值