1. 程式人生 > >html簡單的跑馬燈效果

html簡單的跑馬燈效果

很多專案中都會遇到有類似跑馬燈效果的訊息輪播展示,做了一個簡單的訊息輪播展示。
主要程式碼如下:
html

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title>跑馬燈效果</title>
    <meta name="viewport" content="initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0, user-scalable=no, width=device-width"
>
<link href="css/index.css" type="text/css" rel="stylesheet"> </head> <body> <div class="g-winner-roll"> <img src="img/tongzhi.png"> <div class="m-countdown"> </div> </div> <script src="js/jquery.min.js"></script> <script src
="js/index.js">
</script> </body> </html>

css


.g-winner-roll{
    height: 30px;
    border-bottom: 1px solid #d3d3d3;
    background-color: #ffffff;
}

.g-winner-roll img{
    float: left;
    margin-left: 10px;
    margin-top: 7px;
    display: block;
    width: 15px;
    height: 15
px
; }
.m-countdown{ margin-left: 25px; width: 100%; height: 30px; } .m-countdown > a{ text-align: left; margin-left: 10px; height: 30px; line-height: 30px; font-size: 12px; position:absolute; display: none; } .m-countdown > a:first-child{ display: block; } .m-countdown > a >span{ color: rgb(128,128,128); height: 30px; }

index.js

/**
 * Created by rayootech on 16/8/6.
 */
$(function(){
    var list = ["跑馬燈效果資料1","跑馬燈效果資料2","跑馬燈效果資料3","跑馬燈效果資料4","跑馬燈效果資料5","跑馬燈效果資料6"];
    var htmlString ="";
    for(var i=0;i<list.length;i++){
        htmlString += '<a href="#"><span>'+list[i]+'</span></a>';
    }
    $(".m-countdown").html(htmlString);
    showRollling();
});
function showRollling(){
    var height = 40;    //這個數字是輪播圖片的高度
    var animationTime = 600; //這個數字是動畫時間
    var marginTime = 2000; //這個數字是間隔時間
    var nowimg = 0;     //訊號量
    var mytimer = null;
    //將圖片列表中的第一個節點複製,然後追加到佇列的最後。
    //$(".m-countdown a:first-child").clone().appendTo(".m-countdown");

    var mytimer = window.setInterval(
        function(){
            youanniu();
        }
        ,marginTime
    );
    <!--計時器暫停方法-->
    // window.clearInterval(mytimer);
    // mytimer = window.setInterval(
    function youanniu(){
        if(!$(".m-countdown a").is(":animated")){
            //折騰訊號量
            if(nowimg < $(".m-countdown a").length - 1){
                nowimg = nowimg + 1;
                showAnimate(nowimg);
            }else{
                nowimg = 0;
                $(".m-countdown a").each(function(index){
                    if(index==0){
                        $(this).css("display","block");
                    }
                    else {
                        $(this).css("display","none");
                    }
                }).css("top","7px");
            }
        }
    }
    function sho![這裡寫圖片描述](http://img.blog.csdn.net/20160807012707902)wAnimate(index) {
        $(".m-countdown a").eq(index).css("display","block");
        $(".m-countdown a").eq(index-1).animate(
            {
                "top": -height * nowimg
            }
            , animationTime
        );
    }
}

效果:
這裡寫圖片描述