国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JavaScript > 正文

基于jQuery實現一個marquee無縫滾動的插件

2019-11-19 17:12:46
字體:
來源:轉載
供稿:網友

基于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無縫滾動的插件,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對武林網網站的支持!

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 信宜市| 攀枝花市| 凌云县| 休宁县| 沐川县| 当雄县| 奎屯市| 绥芬河市| 三台县| 镇雄县| 台安县| 岐山县| 德化县| 临颍县| 内乡县| 和田县| 望都县| 海丰县| 泸溪县| 白玉县| 盐边县| 万载县| 栖霞市| 庆元县| 阿拉善右旗| 寻乌县| 慈溪市| 蕲春县| 乐东| 永清县| 宁城县| 深州市| 农安县| 山阳县| 肥东县| 沙田区| 师宗县| 郁南县| 沂源县| 永平县| 南充市|