jQuery無縫滾動外掛程式

來源:互聯網
上載者:User

標籤:style   class   blog   code   http   tar   

外掛程式代碼

;(function ($) {    // jQuery marquee 外掛程式    $.fn.marquee = function (options) {        // 預設設定        var defaults = {            derection: "top",            interval: 50        };                // 合并後的參數設定        var options = $.extend(defaults, options);        var $mar = $(this),             original = $mar.children().first(),             clone = original.clone(),             height = original.height(),             width = original.width(),             i = 0,             tId = null;  // 唯一的tId,可以通過clearTimeout(tId)清除乾淨                // append clone to marquee        $mar.append(clone);        // scrolltop        var scrolltop = function () {            if (i < height) {                $mar.scrollTop(i++);            } else {                i = 0;                $mar.scrollTop(0);            }            tId = setTimeout(scrolltop, options.interval);        };                // scrollbottom        var scrollbottom = function () {                        if (i === 0) {                i = height;                $mar.scrollTop(i);            } else {                $mar.scrollTop(i--);            }            tId = setTimeout(scrollbottom, options.interval);        };        // scrollleft        var scrollleft = function () {                        if (i < width) {                $mar.scrollLeft(i++)            } else {                i = 0;                    $mar.scrollLeft(0);            }            tId = setTimeout(scrollleft, options.interval);        };        // scrollright        var scrollright = function () {                        if (i === 0) {                $mar.scrollLeft(width);                i = width;            } else {                $mar.scrollLeft(i--);            }            tId = setTimeout(scrollright, options.interval);        };        // scroll to which derection        var scrollto = {            top: scrolltop,            bottom: scrollbottom,            left: scrollleft,            right: scrollright        };                // 根據參數選擇滾動函數        tId = setTimeout(scrollto[options.derection], options.interval);        // when mouse hover clearTimeout or setTimeout        $mar.hover(function () {            clearTimeout(tId);        }, function () {            tId = setTimeout(scrollto[options.derection], options.interval);        });    };})(jQuery);
View Code

樣本一:上滾動(預設)

<!-- html code --><div class="marquee">    <ul>        <li>滾動吧少年</li>        <li>滾動吧少年</li>        <li>滾動吧少年</li>        <li>滾動吧少年</li>        <li>滾動吧少年</li>        <li>滾動吧少年</li>        <li>滾動吧少年</li>    </ul>    </div>
/*  css code  */.marquee{    border:1px solid #0a0;    width:300px;    height:100px;    overflow:hidden;}.marquee li{    font-size:16px;    line-height:1.5;}
// js code:外掛程式應用$(".marquee").marquee();

樣本二:左滾動

<!-- html code --><div class="marquee2">    <ul>        <li>滾動吧少年</li>        <li>滾動吧少年</li>        <li>滾動吧少年</li>        <li>滾動吧少年</li>        <li>滾動吧少年</li>        <li>滾動吧少年</li>        <li>滾動吧少年</li>    </ul>    </div>
/* css code */.marquee2{    width:300px;    height:25px;    border:1px solid #00a;    overflow:hidden;    white-space: nowrap;  /* 文字超出時不換行!! */}.marquee2 ul, .marquee2 li{    display:inline;    font-size:16px;    line-height:25px;}
// js code:外掛程式應用$(".marquee2").marquee({derection: "left", interval: 25});

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.