手搓轮播图

照着视频手搓了一个轮播图

效果图

在这里插入图片描述

轮播图源码

<!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;
            box-sizing: border-box;
        }

        .container {
            width: 500px;
            height: 300px;
            margin: 10px auto;
            /* 只显示第一张图片 */
            overflow: hidden;
            position: relative;
        }

        .carousel {
            width: 100%;
            height: 100%;
            /* 弹性布局,横向排列 */
            display: flex;
            transition: 0.5s;
        }

        .container .carousel img {
            width: 500px;
            height: 300px;
        }

        .indicator {
            position: absolute;
            bottom: 10px;
            display: flex;
            left: 5%;
            /* transform: translateX(-50%); */
        }
        .indicator span{
            width: 20px;
            height: 20px;
            border: 2px solid #ccc;
            border-radius: 50%;
            margin: 0 3px;
            /* outline: 1px solid #ccc; */
           
        }
        .indicator  .active{
            background:white;
            border-color: white;
        }
        .btn{
            position: absolute;
            display: flex;
            bottom: 20px;
            flex-direction: row;
            align-items: end;
right: 10%;        }
         .btn button{
            display: block;
            width: 20px;
            height: 20px;
            border: 2px solid #ccc;
            border-radius: 50%;
            margin-right: 10px;
            right: 5%;
            /* outline: 1px solid #ccc; */
           
        }
    </style>
</head>

<body>
    <div class="container">
        <div class="carousel">
            <div class="item"><img src="./img/1.jpg" alt=""></div>
            <div class="item"><img src="./img/2.jpg" alt=""></div>
            <div class="item"><img src="./img/3.jpg" alt=""></div>
            <div class="item"><img src="./img/4.jpg" alt=""></div>
        </div>
        <div class="indicator">
            <span class="active"></span>
            <span></span>
            <span></span>
            <span></span>
        </div>
        <div class="btn">
            <button id="pre">
                P
            </button>
            <button id="next">
                N
            </button>
        </div>
        </div>
    </div>
    <script>
        let doms = {
            carousel:document.querySelector('.carousel'),
            indicators:document.querySelectorAll('.indicator span'),
            btnR :document.getElementById('next'),
            btnL : document.getElementById('pre')
        }
        let index = 0
        //console.log(doms)
        /**
         * 移动轮播图到第几个板块
         * @param{*}index
         * */
        function moveTo(index){
            //改变图片
            doms.carousel.style.transform = `translateX(-${index}00%)`
            // 改变原点
            let active = document.querySelector('.indicator .active')
            active.classList.remove('active')
            doms.indicators[index].classList.add('active')
        }
        doms.indicators.forEach(function(item,i){
            item.onclick = function(){
                moveTo(i)
                index=i
            }
        })
        
        setInterval(function(){
            index++
            index%=4
            moveTo(index)
        },2000)

        // 注册按钮监听器
        doms.btnR.addEventListener("click",function(){
            index++
            index%=4
            moveTo(index)
        })
        doms.btnL.addEventListener("click",function(){
            index--
            if(index<0){
                index = 3
            }
            moveTo(index)
        })
        
        
    </script>
</body>

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值