老师,请检查

老师,请检查

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>轮播图</title>
    <style>
        *{
            margin: 0;
            padding: 0;
        }
        .carousel{
            position: relative;
            width: 650px;
            height: 360px;
            border: 1px solid #000;
            margin: 100px auto;
            overflow: hidden;
        }
        .carousel ul{
            list-style: none;
            width: 3900px;
            position: absolute;
            left: 0;
            transition: all .5s ease 0s;
        }
        .carousel ul li{
            float: left;
        }
        .carousel .btn{
            position: absolute;
            top: 50%;
            margin-top: -10px;
            width: 40px;
            height: 40px;
            background-color: #bfc;
            border-radius: 50%;
        }
        .carousel .leftb{
            left: 10px;
        }
        .carousel .rightb{
            right: 10px;
        }
        .carousel a{
            z-index: 999;
        }
    </style>
</head>
<body>
    <div class="carousel">
        <a href="javascript:;" class="leftb btn" id="leftb"></a>
        <a href="javascript:;" class="rightb btn" id="rightb"></a>
        <ul id="carousel_ul">
            <li>
                <img src="../images/beijing/0.jpg" alt="">
            </li>
            <li>
                <img src="../images/beijing/1.jpg" alt="">
            </li>
            <li>
                <img src="../images/beijing/2.jpg" alt="">
            </li>
            <li>
                <img src="../images/beijing/3.jpg" alt="">
            </li>
            <li>
                <img src="../images/beijing/4.jpg" alt="">
            </li>
        </ul>
    </div>
    <script>
        const carousel_ul=document.getElementById('carousel_ul');
        const leftb=document.getElementById('leftb');
        const rightb=document.getElementById('rightb');
        const clone=carousel_ul.firstElementChild.cloneNode(true);
        carousel_ul.appendChild(clone);
        let count=0;
        let lock=true;
        rightb.onclick=function(){
            if(!lock)return;
            lock=false;
            carousel_ul.style.transition='all .5s ease 0s';
            count++;
            if(count>4){
                setTimeout(function(){
                    carousel_ul.style.transition='none';
                    carousel_ul.style.left=0;
                    count=0;
                },500)
            }
            carousel_ul.style.left=(-650)*count+'px';
            setTimeout(function(){
                lock=true;
            },500)
        }
        leftb.onclick=function(){
            if(!lock)return;
            lock=false;
            carousel_ul.style.transition='all .5s ease 0s';
            count--;
            if(count==-1){
                carousel_ul.style.transition='none';
                carousel_ul.style.left=(-650)*6+'px';
                count=5;
            }
            carousel_ul.style.left=(-650)*count+'px';
            setTimeout(function(){
                lock=true;
            },500)
        }
    </script>
</body>
</html>


正在回答 回答被采纳积分+1

登陆购买课程后可参与讨论,去登陆

1回答
好帮手慕久久 2023-02-10 10:19:08

同学你好,左箭头效果不对。可以如下测试:

在左箭头的点击事件中,打印一下count的值:

https://img1.sycdn.imooc.com//climg/63e5a84c0982d91b06600216.jpg

刷新页面,直接点击左箭头,会发现控制台第一次输出(第一次点击),轮播图并不会移动:

https://img1.sycdn.imooc.com//climg/63e5a87f09bdf39311190399.jpg

如下逻辑不对:

https://img1.sycdn.imooc.com//climg/63e5a8e7098c7b9710290377.jpg

调整如下:

https://img1.sycdn.imooc.com//climg/63e5a972098f238508770702.jpg

 leftb.onclick = function () {
         if (!lock) return;
         lock = false;
         carousel_ul.style.transition = 'all .5s ease 0s';
         count--;
         if (count == -1) {
            carousel_ul.style.transition = 'none';
            // carousel_ul.style.left = (-650) * 6 + 'px';
            carousel_ul.style.left = (-650) * 5 + 'px';
            //  count=5;
            setTimeout(function () {
               carousel_ul.style.transition = 'left .5s ease 0s';
               count = 4;
               carousel_ul.style.left = (-650) * count + 'px';
            }, 0)
         } else {
            carousel_ul.style.left = (-650) * count + 'px';
         }

         setTimeout(function () {
            lock = true;
         }, 500)
      }

祝学习愉快!

  • 提问者 慕芸芸 #1
    <!DOCTYPE html>
    <html lang="en">
    
    <head>
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>轮播图</title>
        <style>
            * {
                margin: 0;
                padding: 0;
            }
    
            .carousel {
                position: relative;
                width: 650px;
                height: 360px;
                border: 1px solid #000;
                margin: 100px auto;
                overflow: hidden;
            }
    
            .carousel ul {
                list-style: none;
                width: 3900px;
                position: absolute;
                left: 0;
                transition: all .5s ease 0s;
            }
    
            .carousel ul li {
                float: left;
            }
    
            .carousel .btn {
                position: absolute;
                top: 50%;
                margin-top: -10px;
                width: 40px;
                height: 40px;
                background-color: #bfc;
                border-radius: 50%;
            }
    
            .carousel .leftb {
                left: 10px;
            }
    
            .carousel .rightb {
                right: 10px;
            }
    
            .carousel a {
                z-index: 999;
            }
        </style>
    </head>
    
    <body>
        <div class="carousel">
            <a href="javascript:;" class="leftb btn" id="leftb"></a>
            <a href="javascript:;" class="rightb btn" id="rightb"></a>
            <ul id="carousel_ul">
                <li>
                    <img src="../images/beijing/0.jpg" alt="">
                </li>
                <li>
                    <img src="../images/beijing/1.jpg" alt="">
                </li>
                <li>
                    <img src="../images/beijing/2.jpg" alt="">
                </li>
                <li>
                    <img src="../images/beijing/3.jpg" alt="">
                </li>
                <li>
                    <img src="../images/beijing/4.jpg" alt="">
                </li>
            </ul>
        </div>
        <script>
            const carousel_ul = document.getElementById('carousel_ul');
            const leftb = document.getElementById('leftb');
            const rightb = document.getElementById('rightb');
            const clone = carousel_ul.firstElementChild.cloneNode(true);
            carousel_ul.appendChild(clone);
            let count = 0;
            let lock=true;
            leftb.onclick = function () {
                if(!lock)return;
                lock=false;
                if (count == 0) {
                    carousel_ul.style.transition = 'none';
                    carousel_ul.style.left = (-650) * 5 + 'px';
                    setTimeout(function () {
                        carousel_ul.style.transition = 'all .5s ease 0s';
                        carousel_ul.style.left = (-650) * 4 + 'px';
                        count = 4;
                    }, 0)
                } else {
                    count--;
                    carousel_ul.style.left = -650 * count + 'px';
                }
                setTimeout(function(){
                    lock=true;
                },500)
            }
            rightb.onclick=function(){
                carousel_ul.style.transition = 'all .5s ease 0s';
                if(!lock)return;
                lock=false;
                count++;
                if(count>4){
                    setTimeout(function(){
                        carousel_ul.style.transition = 'none';
                        carousel_ul.style.left=0;
                        count=0;
                    },500)
                }
                carousel_ul.style.left = -650 * count + 'px';
                setTimeout(function(){
                    lock=true;
                },500)
            }
        </script>
    </body>
    
    </html>


    2023-02-10 15:03:01
  • 好帮手慕久久 回复 提问者 慕芸芸 #2

    修改后,代码是对的,很棒!

    2023-02-10 15:05:30
  • 提问者 慕芸芸 回复 好帮手慕久久 #3

    谢谢(*°∀°)=3

    2023-02-10 15:17:25
问题已解决,确定采纳
还有疑问,暂不采纳

恭喜解决一个难题,获得1积分~

来为老师/同学的回答评分吧

0 星
请稍等 ...
意见反馈 帮助中心 APP下载
官方微信

在线咨询

领取优惠

免费试听

领取大纲

扫描二维码,添加
你的专属老师