您的当前位置:首页正文

新闻标题无缝循环展示效果(jQuery)

来源:要发发知识网

本文内容是用两种不同的方法实现一个简单的是文字无缝循环(向上)的效果,向左、向右或向下无缝循环原理相同,文字换成图片同理。
运行效果图:

新闻无缝循环.gif

方法一效果原理:
定时器+内容插入和底部高度animate变化。

image.png image.png

方法一代码:(本文使用的jQuery版本是1.11.3,在低版本IE及W3C浏览器中效果相同)

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
        <style type="text/css">
            *{
                margin: 0;
                padding: 0;
            }
            #bor{
                width: 500px;
                height: 40px;
                margin: 200px auto;
                border: 2px black solid;
                position: relative;
                overflow: hidden;
            }
            #bor ul{
                position: absolute; 
                bottom: -40px;
            }
            #bor ul li{
                width: 500px;
                height: 40px;
                list-style: none;
                line-height: 40px;
                text-align: center;
            }
        </style>
        <script type="text/javascript" src="jquery.min.js"></script>
        <script type="text/javascript">
            
            $(function(){
                setInterval(function(){
//              代码实现
//              var f = $("#bor ul li").first().height(0);
//              $("#bor ul").append(f);
//              $("#bor ul li").last().animate({'height':'40px'},500);

//              使用jQuery链式效果实现:
                $("#bor ul").append($("#bor ul li").first().height(0).animate({'height':'40px'},500));
                },2000)
            })
        </script>
    </head>
    <body>
        <div id="bor">
            <ul>
                <li style="background: red;">Google 官方推出应用开发架构指南</li>
                <li style="background: paleturquoise;">一张图,三分钟,掌握 Swift和Kotlin</li>
                <li style="background: greenyellow;">MaterialDesign之对TabLayout的探索</li>
                <li style="background: orange;">你可能没注意到 Handler 的这些问题</li>
                <li style="background: deepskyblue;">横向列表折叠和缩放的自定义 ViewGroup</li>
            </ul>
        </div>
    </body>
</html>

方法二效果原理:
定时器和效果animate结合

image.png image.png

方法二代码:(本文使用的jQuery版本是1.11.3,在低版本IE及W3C浏览器中效果相同)

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
        <style type="text/css">
            *{
                margin: 0;
                padding: 0;
            }
            #bor{
                width: 500px;
                height: 40px;
                margin: 200px auto;
                border: 2px black solid;
                position: relative;
                overflow: hidden;
            }
            #bor ul{
                top: 0px;
                position: absolute; 
            }
            #bor ul li{
                width: 500px;
                height: 40px;
                list-style: none;
                line-height: 40px;
                text-align: center;
            }
        </style>
        <script type="text/javascript" src="jquery.min.js"></script>
        <script type="text/javascript">
            
            $(function(){
                var i=0;//记录变化次数
                setInterval(function(){
                    i++;
                    //如果是最后条进行判断
                    if(i==6){
                        i=1;
                        //ul top变为0
                        $("#bor ul").css({'top':'0px'});
                    }
                    var t = i*-40; 
                    console.log('===='+t);
                    $("#bor ul").animate({'top':t+'px'},300);
                },1000)
            })
        </script>
    </head>
    <body>
        <div id="bor">
            <ul>
                <li style="background: red;">Google 官方推出应用开发架构指南</li>
                <li style="background: paleturquoise;">一张图,三分钟,掌握 Swift和Kotlin</li>
                <li style="background: greenyellow;">MaterialDesign之对TabLayout的探索</li>
                <li style="background: orange;">你可能没注意到 Handler 的这些问题</li>
                <li style="background: deepskyblue;">横向列表折叠和缩放的自定义 ViewGroup</li>
                <li style="background: red;">Google 官方推出应用开发架构指南</li>
            </ul>
        </div>
    </body>
</html>

如有问题欢迎交流。

如转载请注明出处,谢谢!