博客
关于我
强烈建议你试试无所不能的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/

你可能感兴趣的文章
React Native(二):属性、状态
查看>>
JSX使用总结
查看>>
React Native(四):布局(使用Flexbox)
查看>>
React Native(七):Android双击Back键退出应用
查看>>
Android自定义apk名称、版本号自增
查看>>
adb command not found
查看>>
Xcode 启动页面禁用和显示
查看>>
【剑指offer】q50:树中结点的最近祖先
查看>>
二叉树的非递归遍历
查看>>
【leetcode】Reorder List (python)
查看>>
【leetcode】Linked List Cycle (python)
查看>>
【leetcode】Linked List Cycle (python)
查看>>
【leetcode】Candy(python)
查看>>
【leetcode】Clone Graph(python)
查看>>
【leetcode】Sum Root to leaf Numbers
查看>>
【leetcode】Pascal's Triangle II (python)
查看>>
java自定义容器排序的两种方法
查看>>
如何成为编程高手
查看>>
本科生的编程水平到底有多高
查看>>
AngularJS2中最基本的文件说明
查看>>