JavaScript文本的上下垂直輪播


首先 HTML需要兩個以上的li標簽來承載文本

<div class="tipsList">
    <ul>
        <li>打開房間看風景咖啡</li>
        <li>看似簡單兩節課的積分</li>
        <li>大家辣椒粉都紛紛離開家</li>
    </ul>
</div>

 然后需要給他們的每一個元素賦予足夠的寬度和高度,稍微的添加一些顯示樣式

*{
    margin:0;
    padding:0;
}
.tipsList{
    width:500px;
    height:20px;
    line-height:20px;
    font-size:14px;
    background:#eee;
    border-radius:5px;
    text-align:center;
    overflow:hidden;
    margin:50px auto;
    
}
.tipsList ul{
    position:relative;
}
.tipsList ul li{
    list-style:none;
    position:absolute;
    width:100%;

}

js的思想是1、獲取到第一個li標簽,2、 獲取它的高度並讓它網上移動,3、 再把這個li重新插到ul的底部。使用animate() 和 settimeout() 如此往復循環,jQuery代碼

 1  var h=$(".tipsList").height();
 2 
 3           $(".tipsList ul li").each(function(){
 4               $(this).css({top:$(this).index()*h+'px'});
 5           });
 6 
 7           setInterval(ctxtslide,3000);   //定時器分開寫
 8           function ctxtslide() {
 9               var disapear=$(".tipsList ul li").first();
10               var clone=$(".tipsList ul li").first().clone();//克隆,后面就用這個克隆的
11               clone.css({top:($(".tipsList ul li").length*h)+"px"});
12               $(".tipsList ul").append(clone);
13               $(".tipsList ul li").each(function(){
14                   var top=parseInt($(this).css('top'));
15                   top-=h;
16                   $(this).animate({"top":top+'px'},1000,function () {
17                       disapear.remove();
18                   });
19               });
20           }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM