用自己的运动函数写的轮播图

用自己的运动函数写的轮播图

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }
        
        ul,
        li {
            list-style: none;
        }
        
        .carousel {
            position: relative;
            width: 600px;
            height: 400px;
            margin: 0 auto;
            overflow: hidden;
        }
        
        ul.imgList {
            position: absolute;
            width: 4200px;
            height: 400px;
        }
        
        ul.imgList li {
            float: left;
        }
        
        .btns {
            width: 600px;
            height: 100px;
            position: absolute;
            top: 50%;
            margin-top: -50px;
        }
        
        .btns .leftBtn {
            float: left;
        }
        
        .btns .rightBtn {
            float: right;
        }
        
        .btns span {
            width: 40px;
            line-height: 100px;
            text-align: center;
            font-size: 30px;
            color: white;
            cursor: pointer;
        }
        
        .cirList {
            position: absolute;
            bottom: 10px;
            left: 140px;
            right: 140px;
            height: 30px;
            display: flex;
            justify-content: space-evenly;
        }
        
        .cirList li {
            width: 20px;
            height: 20px;
            background-color: orange;
            border-radius: 50%;
            cursor: pointer;
        }
        
        .cirList li.active {
            background-color: red;
        }
    </style>

</head>

<body>
    <div id="carousel" class="carousel">
        <ul class="imgList">
            <li><img src="images/1.jpeg" /></li>
            <li><img src="images/2.jpeg" /></li>
            <li><img src="images/3.jpeg" /></li>
            <li><img src="images/4.jpeg" /></li>
            <li><img src="images/5.jpeg" /></li>
            <li><img src="images/6.jpeg" /></li>
            <li><img src="images/1.jpeg" /></li>
        </ul>
        <div class="btns">
            <span class="leftBtn">&lt;</span>
            <span class="rightBtn">&gt;</span>
        </div>
        <ul class="cirList">
            <li></li>
            <li></li>
            <li class="active"></li>
            <li></li>
            <li></li>
            <li></li>
        </ul>
    </div>
    <script src="../qf.js"></script>
    <script>
        var carousel = document.querySelector("#carousel");
        var rightBtn = document.querySelector('.rightBtn');
        var leftBtn = document.querySelector('.leftBtn');
        var imgList = document.querySelector(".imgList");
        //得到有多少张轮播的图片
        var listLength = imgList.querySelectorAll("li").length;
        //将得到的小圆点伪数组转化为数组
        var cirListAll = Array.from(document.querySelectorAll(".cirList li"));
        //console.log(cirListAll);
        // console.log(listLength);
        var count = 0;
        //标志位  标志着能不能触发轮播事件 即切换图片
        var flag = true;
        change();
        //右滑函数
        function rightHandler() {
        //假如标志位为假的话  函数就不执行
            if (!flag) {
                return;
            }
            //保证在函数执行期间 不会触发下一次轮播事件
            flag = false;
            //标志着当前是第几张照片
            count++;
            console.log(count);
            //调用自己的运动函数
            QF.animation(imgList, {
                left: -count * 600
            }, 1000, function() {
            //当图片切换到猫腻图的时候   快速将图片拉回第一张图片 且将count置为0
                if (count >= listLength - 1) {
                    imgList.style.left = 0;
                    count = 0;
                }
                //当动画执行完了之后  将标志位设为真 以便下次执行轮播事件
                flag = true;
            })
            //用于改变小圆点的样式
            change();
        }
        //右滑事件
        rightBtn.addEventListener('click', rightHandler)
        //左滑事件
        leftBtn.addEventListener('click', function() {
            if (!flag) {
                return;
            }
            count--;
            flag = false;
            //当减到-1张图的时候 将图片快速拉到猫腻图  然后再减1 接下来就是调用运动函数
            if (count < 0) {
                count = listLength - 1;
                imgList.style.left = -count * 600 + 'px';
                count--;
            }
            QF.animation(imgList, {
                left: -count * 600
            }, 1000, function() {
                flag = true;
            })
            change();
        })

        function change() {
        //排他法  将所有点的样式去除
            cirListAll.forEach((item, index) => {
                item.className = ''
            })
            //当count到猫腻图的时候 显示的是第一个圆点的样式
            if (count === listLength - 1) {
                cirListAll[0].className = 'active'
            } else {
            	//count的值也就是显示对应的小圆点的值
                cirListAll[count].className = 'active'
            }
        }
        
        cirListAll.forEach((item, index) => {
        //给每个小圆点绑定点击事件
            item.onclick = function() {
            	//点击的小圆点的索引也就是count的值 再调用运动函数就行
                count = index;
                QF.animation(imgList, {
                    left: -count * 600
                }, 1000, function() {
                    flag = true;
                })
                change();
            }
        })

        //轮播图自动切换
        let timer = setInterval(function() {
            rightHandler();
        }, 2000);
        //当鼠标进入轮播图的时候  停止轮播图
        carousel.onmouseenter = function() {
            clearInterval(timer);
        }
        //当鼠标离开轮播图的时候  开启轮播图
        carousel.onmouseleave = function() {
            timer = setInterval(function() {
                rightHandler();
            }, 2000);
        }
    </script>
</body>

</html>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值