老师我的效果实现不了,也没想通这个轮播图,希望老师讲解一下

来源:2-3 代码编写(1)

慕仰5214815

2020-11-15 16:43:20

# 具体遇到的问题
首页动态项目慕家居的精品推荐部分轮播图
# 报错信息的截图
http://img.mukewang.com/climg/5fb0e96d0959d45715880712.jpg

# 相关课程内容截图

# 尝试过的解决思路和结果

# 粘贴全部相关代码,切记添加代码注释(请勿截图)

<!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>

        * {

            margin0;

            padding0;

            font-family'微软雅黑';

        }


        ul,

        ol {

            list-stylenone;

        }


        a {

            text-decorationnone;

        }


        .center-wrap {

            width1201px;

            margin0 auto;

        }


        .recommend {

            background-color#EFF0F4;

            height590px;

            positionrelative;

            /* aboutCenter高度使用44px,剩余38 */

            margin-top38px;

            padding-top70px;

        }


        .recommend .center-wrap {

            positionrelative;

        }


        .recommend .center-wrap .similar {

            width232px;

            positionabsolute;

            left50%;

            margin-left-116px;

            padding-top32px;

            text-aligncenter;

        }


        .recommend .center-wrap .similar h3 {

            font-size32px;

            color#696868;

            font-weightnormal;

        }


        .recommend .center-wrap .similar span {

            font-size18px;

            color#9B9B9B;

            positionrelative;

        }


        /* 列表 */

        .recommend .center-wrap .carousel {

            width1150px;

            overflowhidden;

        }


        .recommend .center-wrap .list {

            overflowhidden;

            margin-left61px;

            padding-top195px;

            width10000px;

            left0;

            transitionleft .5s ease 0s;

        }


        .recommend .center-wrap .list li {

            floatleft;

            positionrelative;

            margin-right23px;

        }


        .recommend .center-wrap .list li a img {

            width345px;

            height326px;

        }


        .recommend .center-wrap .list li:last-child {

            margin-left0;

        }


        .recommend .center-wrap .list li a .recomWords {

            width345px;

            height45px;

            positionabsolute;

            left0;

            bottom0;

            background-colorrgba(0000.50);

            font-size16px;

            color#FFFFFF;

            line-height45px;

            text-aligncenter;

        }


        /*  左右箭头图标*/

        .recommend .arrow {

            width48px;

            height48px;

        }


        .recommend .prev {

            positionabsolute;

            left0;

            top346px;

            background-imageurl(images/prev.png);

        }


        .recommend .next {

            positionabsolute;

            right0;

            top346px;

            background-imageurl(images/next.png);

        }


        .recommend .prev:hover {

            background-imageurl(images/prev_active.png);

        }


        .recommend .next:hover {

            background-imageurl(images/next_active.png);

        }

    </style>

</head>


<body>

    <section class="recommend">

        <div class="center-wrap">

            <div class="similar">

                <h3>精品推荐</h3>

                <span>Boutique recommendation</span>

            </div>

            <a href="javascript:;" class='arrow prev' id='leftbtn'></a>

            <a href="javascript:;" class='arrow next' id='rightbtn'></a>

            <div class="carousel">

                <ul class='list' id='list'>

                    <li>

                        <a href="#">

                            <img src="images/recommend1.png" alt="">

                            <div class="recomWords">轻奢风格样板房客厅色彩搭配装修设计</div>

                        </a>

                    </li>

                    <li>

                        <a href="#">

                            <img src="images/recommend2.png" alt="">

                            <div class="recomWords">简约美式风格卧室衣柜设计</div>

                        </a>

                    </li>

                    <li>

                        <a href="#">

                            <img src="images/recommend3.png" alt="">

                            <div class="recomWords">轻奢风格L型厨房装修设计</div>

                        </a>

                    </li>

                    <li>

                        <a href="#">

                            <img src="images/recommend4.png" alt="">

                            <div class="recomWords">轻奢风格L型厨房装修设计</div>

                        </a>

                    </li>

                    <li>

                        <a href="#">

                            <img src="images/recommend5.png" alt="">

                            <div class="recomWords">轻奢风格L型厨房装修设计</div>

                        </a>

                    </li>

                    <li>

                        <a href="#">

                            <img src="images/recommend6.png" alt="">

                            <div class="recomWords">轻奢风格L型厨房装修设计</div>

                        </a>

                    </li>

                    <li>

                        <a href="#">

                            <img src="images/recommend7.png" alt="">

                            <div class="recomWords">轻奢风格L型厨房装修设计</div>

                        </a>

                    </li>

                    <li>

                        <a href="#">

                            <img src="images/recommend8.png" alt="">

                            <div class="recomWords">轻奢风格L型厨房装修设计</div>

                        </a>

                    </li>

                    <li>

                        <a href="#">

                            <img src="images/recommend9.png" alt="">

                            <div class="recomWords">轻奢风格L型厨房装修设计</div>

                        </a>

                    </li>

                </ul>

            </div>

        </div>

    </section>

    <script>

        // 先得到元素

        var list = document.getElementById('list');

        var leftbtn = document.getElementById('leftbtn');

        var rightbtn = document.getElementById('rightbtn');

        // // 克隆

        var clone_li = list.firstElementChild.cloneNode(true);

        list.appendChild('clone_li');

        var idx = 0;

        var lock = true;

        rightbtn.onclick = function () {

            if (!lockreturn;

            lock = false;

            list.style.transition = 'left .5s ease 0s';

            idx++;

            if (idx > 8) {

                setTimeout(function () {

                    list.style.transition = 'none';

                    list.style.left = 0;

                    idx = 0;

                }, 500)

            }

            list.style.left = -idx * 345 + 'px';

            // 函数节流

            setTimeout(function () {

                lock = true;

            }, 500);


        }


        leftbtn.onclick = function () {

            if (!lockreturn;

            lock = false;

            if (idx == 0) {

                list.style.transition = 'none';

                list.style.left = -9 * 345 + 'px';

                setTimeout(function () {

                    list.style.transition = 'left .5s ease 0s';

                    idx = 8;

                    list.style.left = -idx * 345 + 'px';

                }, 0)

            } else {

                idx--;

                list.style.left = idx * 345 + 'px';

            }

            // 函数节流

            setTimeout(function () {

                lock = true;

            }, 500);

        }

    </script>

</body>


</html>


写回答

1回答

好帮手慕久久

2020-11-15

同学你好,本作业中的轮播图,不需要做成无缝滚动的形式,它的可视区是展示3个图片,而无缝滚动只展示一个,二者有差别:

http://img.mukewang.com/climg/5fb0fd9409e9bb6d12270434.jpg


该轮播图效果如下:

以点击右侧按钮为例,每点击一次,轮播图往左侧切换一张,直到最后一张图显示到可视区之后,再点击右侧按钮,轮播图不会再往左侧切换,也不会回到第一张。

整体实现,可修改如下:

为了方便计算,将样式调整如下:

http://img.mukewang.com/climg/5fb0ff5e0982dd3108480310.jpg

http://img.mukewang.com/climg/5fb10437096a6c1b05820272.jpg

http://img.mukewang.com/climg/5fb0ff8f09c0a7ba06120200.jpg

js调整如下:

http://img.mukewang.com/climg/5fb0ffff0969b9e907800143.jpg

http://img.mukewang.com/climg/5fb10030098e40bf07070215.jpg

http://img.mukewang.com/climg/5fb1033809379aaf07130490.jpg

http://img.mukewang.com/climg/5fb1035e090dab0006460361.jpg

同学按照上面修改一下,如果还有问题,可以提交作业,批复作业的老师,会针对代码,再次为同学修改优化的。

祝学习愉快!

0

0 学习 · 15276 问题

查看课程