zoukankan      html  css  js  c++  java
  • jquery详解图片平滑滚动

    jquery详解图片平滑滚动

    随便写了个DOM,没有美观性,见谅

    原理:
    1、定义两组ul列表放图,第一个ul放5张图,第二个ul为空
    2、为什么要用两个ul?因为要用到jQuery的克隆方法clone()。把第一个ul的图片全部克隆到第二个ul中,形成并列无缝滚动效果
    3、.box设置为绝对定位,.wrap设置为相对定位
    4、设置定时器,让.box的left值一直减-,当left值大于第一个ul的长度时,让他left等于0,然后再重新重置定时器,继续执行

    代码部分:

    //CSS
    *{margin: 0;padding: 0}
    ul{list-style: none}
    .box ul li{display: block;float: left; 300px;height: 300px;margin-right: -1px;background-size: cover;background-position: center center;}
    .box{height: 300px;3010px;}
    #box1,#box2{float: left}
    .wrap{position: relative; 800px;margin: auto;height: 300px;overflow: hidden}
    .box{position: absolute;left: 0;}
    //HTML
    <body>
        <div class="wrap">
            <div class="box">
                <ul id="box1">
                    <li style="background:url(images/1.jpg) no-repeat"></li>
                    <li style="background:url(images/2.jpg) no-repeat"></li>
                    <li style="background:url(images/3.jpg) no-repeat"></li>
                    <li style="background:url(images/4.jpg) no-repeat"></li>
                    <li style="background:url(images/5.jpg) no-repeat"></li>
                </ul>
                <ul id="box2"></ul>
            </div>
        </div>
    </body>
    //JQUERY
    <script>
        var timer='';//设置一个定时器
        var $box1=$('#box1').children().clone(true);/*克隆box1的子元素*/
        $('#box2').append($box1);//将复制的元素插入到#box2中
        var $left=parseInt($('.box').css('left'));//获取.box的left值
        var scroll=function(){
            $left-=2;//设置滚动速度为2
            $('.box').css('left',$left+'px');//left赋值
            if($left<-1500){//当box值小于-1500px时,重置.box left值为0;
                $('.box').css('left','0');
                $left=0;
            }
            timer =setTimeout(scroll,30);
        }
        setTimeout(scroll,100);
        $('.wrap').hover(function(){
            clearTimeout(timer);
        },function(){
            setTimeout(scroll,100);
        });
    </script>

    主要是理解思路,思路理清了,相信你不用看dom都会做了

  • 相关阅读:
    LeetCode Binary Tree Inorder Traversal
    LeetCode Populating Next Right Pointers in Each Node
    LeetCode Construct Binary Tree from Inorder and Postorder Traversal
    LeetCode Reverse Linked List II
    LeetCode Populating Next Right Pointers in Each Node II
    LeetCode Pascal's Triangle
    Palindrome Construct Binary Tree from Preorder and Inorder Traversal
    Pascal's Triangle II
    LeetCode Word Ladder
    LeetCode Binary Tree Zigzag Level Order Traversal
  • 原文地址:https://www.cnblogs.com/yz-blog/p/6600521.html
Copyright © 2011-2022 走看看