基於jQuery實現一個marquee無縫滾動的外掛程式,jquerymarquee

來源:互聯網
上載者:User

基於jQuery實現一個marquee無縫滾動的外掛程式,jquerymarquee

基於jQuery,實現一個marquee無縫滾動的外掛程式,已經發布到 git.oschina.net,示範稍後更新(更新到 http://git.oschina.net/mqycn/jQueryMarquee )。

代碼如下:

/** * 類庫名稱:jQuery.marquee * 實現功能:基於 jquery 實現的 marquee 無縫滾動外掛程式 * 作者首頁:http://www.miaoqiyuan.cn/ * 聯絡郵箱:mqycn@126.com * 使用說明:http://www.miaoqiyuan.cn/p/jquery-marquee * 最新版本:http://git.oschina.net/mqycn/jQueryMarquee*/jQuery.fn.extend({  marquee : function(opt, callback){    opt = opt || {};    opt.speed = opt.speed || 30;    opt.direction = opt.direction || 'left';    opt.pixels = opt.pixels || 2;    switch( opt.direction ){      case "left":      case "right":        opt.weight = "width";        opt.margin = "margin-left";        opt.tpl = '<table><tr><td>[TABLE]</td><td>[TABLE]</td></tr></table>';        break;      case "top":      case "bottom":        opt.weight = "height";        opt.margin = "margin-top";        opt.tpl = '<table><tr><td>[TABLE]</td></tr></tr><td>[TABLE]</td></tr></table>';        break;      default:        throw Error("[jQuery.marquee.js] Options.direction Error!");    }    switch( opt.direction ){      case "left":      case "top":        opt.addon = -1;        break;      case "right":      case "bottom":        opt.addon = 1;        break;      default:        throw Error("[jQuery.marquee.js] Options.direction Error!");    }    callback = typeof callback == "function" ? callback : function(){};    //設定寬度    $(this).each(function(){      if( this.control ){        clearInterval(this.control);      } else {        //如果第一次執行,初始化代碼        $(this)          .data(opt.weight, opt.weight == 'width' ? $(this).find("table").width() : $(this).find("table").height())          .width($(this).data(opt.weight) * 2)          .html(opt.tpl.replace(/\[TABLE\]/ig, $(this).html()))          .mouseover(function(){            $(this).data("pause", true);          }).mouseout(function(){            $(this).data("pause", false);          });      }      this.control = setInterval((function(){        if( $(this).data("pause") ){          return;        }        var _margin = parseInt($(this).css(opt.margin)) + opt.addon * opt.pixels;        if( opt.addon == -1 && _margin + $(this).data(opt.weight) < 0 ){          _margin = 0;        }else if( opt.addon == 1, _margin > 0 ){          console.log(_margin < 0,$(this).data(opt.weight));          _margin = -1 * $(this).data(opt.weight);        }        $(this).css(opt.margin, _margin + "px");        callback.bind(this)();      }).bind(this), opt.speed);    });    return $(this);  }});

如果在IE9以下使用,還需要在之前增加如下代碼:

/** * IE8外掛程式(解決 function 不支援 bind 的問題),非原創*/if (!Function.prototype.bind) {  Function.prototype.bind = function(oThis) {    if (typeof this !== "function") {      throw new TypeError("[jQuery.marquee.ie8] Caller is not a function");    }    var aArgs = Array.prototype.slice.call(arguments, 1),      fToBind = this,      fNOP = function() {},      fBound = function() {        return fToBind.apply(this instanceof fNOP && oThis ? this : oThis, aArgs.concat(Array.prototype.slice.call(arguments)));      };    fNOP.prototype = this.prototype;    fBound.prototype = new fNOP();    return fBound;  };}

一共有三個選擇性參數,一個回調方法。

direction,移動方向:支援 左:left 右:right 上:top 下:bottom;

pixels,每次移動的像素數

speed,兩次移動之前的間隔時間數(毫秒)

調用方法如下:

$("scroll-a").marquee();$("scroll-b").marquee({direction:'top'});$("scroll-c").marquee({direction:'top',pixels:2,speed:30});$("scroll-d").marquee({direction:"top",pixels:2,speed:30}, function(){  console.log("執行了一次");});

以上所述是小編給大家介紹的基於jQuery實現一個marquee無縫滾動的外掛程式,希望對大家有所協助,如果大家有任何疑問請給我留言,小編會及時回複大家的。在此也非常感謝大家對幫客之家網站的支援!

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.