纯原生JS实现仿京东淡入淡出轮播图,不使用JQuery

原生js实现仿京东淡入淡出轮播图


一. 实现功能简述

图片自动轮播,切换图片淡入淡出,鼠标放在图片上停止轮播,鼠标移开恢复,触碰轮播指标点切换到相应图片,JSON储存图片数据,相对来讲图片数据独立一点,能够根据图片数量控制轮播图的数量


二. 思路

三个div装图片的盒子绝对定位叠在一起,通过修改z-index属性,在每次切换时把顶层图片放在底层 (这里使用双向链表进行循环) 。
流程示意


三. 代码

部分代码概述:
class :

class简述
shuffling轮播需要的功能按钮
leftToward左箭头
rightToward右箭头
transpunction轮播指标点
shufflingImg轮播图

函数:

function参数简述功能简述
imgInitial无参数初始化图片
punct_Initial无参数初始化指标点
arrowToward无参数左右箭头初始化
imgOnMouseOver无参数当鼠标放在图片上的事件
autoShuffling无参数自动轮播
imgShiftelement : 待切换的图片对应的盒子图片切换操作
punctOneprassLayer : 压盖图层调整指标点的状态
indesivery无参数调整轮播图的堆叠顺序(z-index)
createDotimgName : 图片的名称生成一个指标点DOM元素
node_Initial无参数初始化轮播链表

轮播图对象(shufflingInfo):

变量简述
imgList图片列表
punctList指标点列表
locality当前的轮播位置
length图片列表总长度
nodes轮播图链表
chron自动轮播定时器
primary第一张轮播图
secondary第二张轮播图
ternary第三张轮播图

效果图

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>轮播图</title>
</head>
<style>
    *{
        padding: 0;
        border: 0;
        margin: 0;
    }
    a{
        text-decoration: none;
        color: inherit;
    }
    .shuffling{
        position: relative;
        width: 700px;
    }
    .leftToward{
        z-index: 1000;
        position: absolute;
        background-color:rgba(0, 0, 0, 0.425);
        width: 25px;
        height: 35px;
        border-radius: 0px 35px 35px 0px;
        text-align: center;
        font-size: 20px;
        color: white;
        left: 0px;
        top: 179.175px;
    }
    .rightToward{
        z-index: 1000;
        position: absolute;
        background-color:rgba(0, 0, 0, 0.418);
        width: 25px;
        height: 35px;
        border-radius: 35px 0px 0px 35px ;
        text-align: center;
        font-size: 20px;
        color: white;
        left: 675px;
        top: 179.175px;
    }
    .transpunction{
        z-index: 1000;
        position: absolute;
        display: flex;
        flex-wrap: nowrap;
        top: 340px;
        left: 30px;
        list-style: none;
    }
    .shufflingPoint{
        position: relative;
        width: 15px;
        height: 15px;
        margin-right: 5px;
        border-radius: 15px;
        background-color: rgba(211, 211, 211, 0.253);
    }
    .shufflingPoint .pressLayer{
        position: absolute;
        left: 0px;
        top: 0px;
        width: 15px;
        height: 15px;
        border-radius: 15px;
        background-color: rgba(255, 31, 31, 0);
    }
    .shufflingPoint .intrapoint{
        margin: 3px auto;
        width: 9px;
        height: 9px;
        border-radius: 10px;
        background-color: rgba(255, 255, 255, 0.78);
    }
    .shufflingImg{
        position: relative;
    }
    .shufflingImg img{
        position: absolute;
        left: 0px;
        top: 0px;
        width: 700px;
        opacity: 1;
        transition: opacity ease-in 0.2s;
    }
</style>
<body>
    <div class="f_shuffling">    
        <div class="shuffling">
            <div id="leftToward" class="leftToward">
                <a href="javascript:void(0)"><</a>
            </div>
            <div id="rightToward" class="rightToward">
                <a href="javascript:void(0)">></a>
            </div>
        </div>
        <div id="transpunction" class="transpunction">
            <!-- 在这里加小点 -->
            <!-- <li class="shufflingPoint" info="默认元素">
                <div class="intrapoint"></div>
                <div class="pressLayer"></div>
            </li> -->
        </div>
        <div id="shufflingImg" class="shufflingImg">
            <img id="ternary" style="opacity: 1; z-index: 4;" src="./windows_img/10.jpg" alt="">
            <img id="secondary" style="opacity: 1; z-index: 5;" src="./windows_img/10.jpg" alt="">
            <img id="primary" style="opacity: 0; z-index: 6;" src="./windows_img/10.jpg" alt="">
        </div>
    </div>
</body>
<script>
    var imgList = [//在这里动态新增图片
        img1 = {name:"img1",address:"./windows_img/9.jpg"},
        img2 = {name:"img2",address:"./windows_img/10.jpg"},
        img3 = {name:"img3",address:"./windows_img/11.jpg"},
        img4 = {name:"img4",address:"./windows_img/12.jpg"},
        img5 = {name:"img5",address:"./windows_img/13.jpg"},
        img6 = {name:"img6",address:"./windows_img/14.jpg"}, 
        img7 = {name:"img7",address:"./windows_img/15.jpg"}       
    ];
    var shufflingInfo = new ShufflingInfo(imgList);
    var node = shufflingInfo.nodes[0];
    window.onload = function(){
        Initial();//初始化
        autoShuffling();//自动轮播
    }
    function Initial(){//初始化
        imgInitial();//图片初始化
        punct_Initial();//小点点初始化
        arrowToward();//左右箭头初始化
        imgOnMouseOver();//当鼠标放在图片上面时
    }
    function autoShuffling(){//自动轮播
        var length = shufflingInfo.length;
        var punctList = shufflingInfo.punctList;
        shufflingInfo.chron = setInterval(function(){
            var index = shufflingInfo.locality;
            index = index < length-1 ? index+1 : 0;
            imgShift(punctList[index].children[1]);
        },2000);
    }
    function imgInitial(){//图片初始化
        shufflingInfo.primary.src = imgList[0].address;
        shufflingInfo.secondary.src = imgList[0].address;
        shufflingInfo.ternary.src = imgList[0].address;
    }
    function imgOnMouseOver(){//当鼠标放在图片上面时
        var shufflingImg = document.getElementById("shufflingImg");
        shufflingImg.onmouseover = function(){
            clearInterval(shufflingInfo.chron);
        }
        shufflingImg.onmouseout = function(){
            autoShuffling();
        }
    }
    function punct_Initial(){//小点点初始化
        var transpunction = document.getElementById("transpunction");
        for(var i = 0; i < imgList.length ; i++){
            var point = createDot(imgList[i].name);
            var pressLayer = point.children[1];
            pressLayer.onmouseover = function(){//小点点添加事件
                clearInterval(shufflingInfo.chron);
                imgShift(this);
            }
            pressLayer.onmouseout = function(){
                autoShuffling();
            }
            transpunction.insertBefore(point,null);
        }
        imgShift(shufflingInfo.punctList[shufflingInfo.locality].children[1]);
    }
    function arrowToward(){//左右箭头初始化
        var leftToward = document.getElementById("leftToward");
        var rightToward = document.getElementById("rightToward")
        leftToward.onclick = function(){//左箭头添加事件
            var index = shufflingInfo.locality;
            clearInterval(shufflingInfo.chron);//先清除定时器
            if(index > 0 && index < shufflingInfo.length){
                var punctList = shufflingInfo.punctList;
                imgShift(punctList[index-1].children[1]);
            }
            autoShuffling();//切完图片激活定时器
        }
        rightToward.onclick = function(){//右箭头添加事件
            var index = shufflingInfo.locality;
            clearInterval(shufflingInfo.chron);//先清除定时器
            if(index >= 0 && index < shufflingInfo.length - 1){
                var punctList = shufflingInfo.punctList;
                imgShift(punctList[index+1].children[1]);
            }
            autoShuffling();//切完图片激活定时器
        }
    }
    function imgShift(element){//统一图片切换操作
        if(punctOne(element) != null){
            indesivery();
            var imgSrc = eval(element.getAttribute("data")+".address"); 
            node.element.style.zIndex = node.element.style.zIndex - 3;//顶层图片放在底层
            node.element.style.opacity = 1;//底层图片设置透明度为1
            node.element.style.opacity = 1;
            node.pre.element.src = imgSrc;
            node.next.element.style.opacity = 0;//下一层图片设置成0,在这tranform
            node = node.next;//
        }
    }
    function punctOne(pressLayer){//调整小点点的状态
        var point = pressLayer.parentNode;
        var intrapoint = point.children[0];
        var punctList = document.getElementById("transpunction").children;
        if(intrapoint.style.display == "block")
            return null;
        for(var i = 0 ; i < punctList.length; i++){
            punctList[i].children[0].style.display = "none";
            if(punctList[i].children[0] == intrapoint){
                shufflingInfo.locality = i;     
            }
        }
        point.children[0].style.display = "block";
        return pressLayer;
    }
    function indesivery(){//初始化轮播图的z-index属性
        var priIndex = parseInt(shufflingInfo.primary.style.zIndex);
        var dubIndex = parseInt(shufflingInfo.secondary.style.zIndex);
        var trindex = parseInt(shufflingInfo.ternary.style.zIndex);
        if((priIndex+dubIndex+trindex)<= 6){
            shufflingInfo.primary.style.zIndex = parseInt(3+priIndex);
            shufflingInfo.secondary.style.zIndex = parseInt(3+dubIndex);
            shufflingInfo.ternary.style.zIndex = parseInt(3+trindex);
        }
    }
    function createDot(imgName){//创建轮播小点点
        var point = document.createElement("li");
        var pressLayer = document.createElement("div");
        var intrapoint = document.createElement("div");
        point.className = "shufflingPoint";
        pressLayer.id = imgName;
        pressLayer.className = "pressLayer";
        pressLayer.setAttribute("data",imgName);
        intrapoint.className = "intrapoint";
        intrapoint.style.display = "none";
        point.insertBefore(intrapoint,null);
        point.insertBefore(pressLayer,null);
        return point;
    }
    function ShufflingInfo(imgList){//自定义轮播图对象
        //储存轮播图的系列信息
        this.imgList = imgList;//图片列表
        this.punctList = document.getElementById("transpunction").children;//点列表
        this.locality = 0;//当前轮播位置
        this.length = imgList.length;//图片列表长度
        this.nodes = node_Initial();//轮播链表
        this.chron = null;//轮播定时器
        this.primary = document.getElementById("primary");//轮播图1
        this.secondary = document.getElementById("secondary");//轮播图2
        this.ternary = document.getElementById("ternary");//轮播图3
    }
    function node_Initial(){
        //初始化轮播图链表
        //注意这里只有3个元素,因为我只用了三个图
        var nodes = [
            new Node(this.primary),
            new Node(this.secondary),
            new Node(this.ternary),
        ];
        nodes[0].pre = nodes[2];
        nodes[0].next = nodes[1];
        nodes[1].pre = nodes[0];
        nodes[1].next = nodes[2];
        nodes[2].pre = nodes[1];
        nodes[2].next = nodes[0];
        return nodes;
    }
    function Node(element){//自定义双向链表对象
        this.element = element;
        this.pre = null;
        this.next = null;
    }
</script>
</html>
  • 4
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值