博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
JavaScript实现京东轮播图效果——自动轮播,左右按钮切换图片,小圆圈跟随图片变化,点击小圆圈可跳转图片,无缝循环播放
阅读量:3958 次
发布时间:2019-05-24

本文共 4381 字,大约阅读时间需要 14 分钟。

JavaScript实现京东轮播图效果——自动轮播,左右按钮切换图片,小圆圈跟随图片变化,点击小圆圈可跳转图片,无缝循环播放

静态效果图如下:静态效果图

  • CSS部分
*{            margin: 0;            padding: 0;        }        body{            user-select: none;        }        .banner{            position: relative;            width: 384px;            height: 470px;            margin: 50px auto;        }        .ban-image{            position: absolute;            width: 100%;            height: 100%;        }        .ban-image img{            opacity: 0;            position: absolute;            transition:1s;        }        .ban-image img.on{            opacity: 1;        }        .btn-left,.btn-right{            position: absolute;            top: 30%;            width: 45px;            margin-top: -30px;            background-color: rgba(0, 0,0, 0.5);            font-size: 18px;            text-align: center;            line-height: 60px;            color: #fff;            cursor: pointer;        }        .btn-left{            left:0px;            border-top-right-radius: 50%;            border-bottom-right-radius: 50%;        }        .btn-right{            right:0px;            border-top-left-radius: 50%;            border-bottom-left-radius: 50%;        }        .tab{            position: absolute;            bottom: 180px;            left: 15%;            transform: translateX(-50%);        }        .tab li{            float: left;            list-style: none;            width: 10px;            height: 10px;            margin-left: 5px;            border-radius: 50%;            background-color: #ccc;            cursor: pointer;        }        .tab li.on{            background-color: #f70;        }
  • HTML部分
  • JavaScript样式
var a=10;        var oBtnRight = document.querySelector('.btn-right');//获取元素的方式        var oBtnLeft = document.querySelector('.btn-left');//获取元素的方式        var aImages = document.querySelectorAll('.ban-image img');        var aTabs = document.querySelectorAll('.tab li');        var index = 0;//保存图片数组的下标              auto();//自动执行函数        //自动轮播函数        function auto(){            setInterval(function(){                aImages[index].className = '';//清空class类名                aTabs[index].style.backgroundColor = '#ccc';//也可以写成aTabs[index].className = '';                //变换index值                index ++;                index %=5;                  //设置当前样式                aImages[index].className = 'on';                aTabs[index].style.backgroundColor = '#f70';            },3000);        }                                                oBtnRight.onclick = function(){//点击事件  执行函数            //清除上一次的样式            aImages[index].className = '';//清空class类名            aTabs[index].style.backgroundColor = '#ccc';//也可以写成aTabs[index].className = '';            //变换index值            index ++;            index %=5;//如果index>=5{index =0;}               //设置当前样式            aImages[index].className = 'on';            aTabs[index].style.backgroundColor = '#f70';//也可以写成aTabs[index].className = 'on';            console.log('我点击了',aImages[index]);//测试用            console.log('我点击了',aTabs[index]);//测试用        }                oBtnLeft.onclick = function(){//点击事件  执行函数            //清除上一次的样式            aImages[index].className = '';//清空class类名            aTabs[index].style.backgroundColor = '#ccc';//也可以写成aTabs[index].className = '';            //变换index值            index --;            if(index < 0){                index =4;//注意下标从0开始            }               //设置当前样式            aImages[index].className = 'on';            aTabs[index].style.backgroundColor = '#f70';//也可以写成aTabs[index].className = 'on';            console.log('我点击了',aImages[index]);//测试用            console.log('我点击了',aTabs[index]);//测试用        }        //点击小圆点        for (var i =0;i

总:

    
Document

转载地址:http://lbozi.baihongyu.com/

你可能感兴趣的文章
论文MICO for MRI bias field estimation and tissue segmentation品讲
查看>>
后现代
查看>>
VMware6关机后出现is not a valid virtual machine configuration file的解决办法
查看>>
通过ASP实现flash对数据库的访问
查看>>
“==”和equals方法究竟有什么区别?
查看>>
哈佛图书馆墙上的20条训言
查看>>
交流引发深入思考
查看>>
保持我们母语的纯洁
查看>>
免费的互联网时代如何盈利?
查看>>
可怕的宣传力量
查看>>
症状:可以上网,可以上QQ,不能登陆360安全卫士,360浏览器无法同步,有道词典等无法登陆,无法查询。
查看>>
重读《触龙说赵太后》
查看>>
2010的第一次思想触动
查看>>
文学大师做菜艺术20个"须知"
查看>>
SVN + 批处理 + Dropbox + TeamViewer实现远方协同工作
查看>>
vc学习之关于缩放到托盘区
查看>>
vc学习之键盘事件OnKeyDown
查看>>
近期工作
查看>>
春寒料峭,原来春天这样走近
查看>>
电脑坏了--关于联想笔记本声卡驱动
查看>>