基于jQuery實現(xiàn)一個marquee無縫滾動的插件
基于jQuery,實現(xiàn)一個marquee無縫滾動的插件,已經(jīng)發(fā)布到 git.oschina.net,演示稍后更新(更新到 http://git.oschina.net/mqycn/jQueryMarquee )。
代碼如下:
/**
* 類庫名稱:jQuery.marquee
* 實現(xiàn)功能:基于 jquery 實現(xiàn)的 marquee 無縫滾動插件
* 作者主頁:http://www.miaoqiyuan.cn/
* 聯(lián)系郵箱: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(){};
//設(shè)置寬度
$(this).each(function(){
if( this.control ){
clearInterval(this.control);
} else {
//如果第一次執(zhí)行,初始化代碼
$(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 的問題),非原創(chuàng)
*/
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;
};
}
一共有三個可選參數(shù),一個回調(diào)方法。
direction,移動方向:支持 左:left 右:right 上:top 下:bottom;
pixels,每次移動的像素數(shù)
speed,兩次移動之前的間隔時間數(shù)(毫秒)
調(diào)用方法如下:
$("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("執(zhí)行了一次");
});
以上所述是小編給大家介紹的基于jQuery實現(xiàn)一個marquee無縫滾動的插件,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
相關(guān)文章
Query中click(),bind(),live(),delegate()的區(qū)別
這篇文章主要介紹了Query中click(),bind(),live(),delegate()之間的區(qū)別。需要的朋友可以過來參考下,希望對大家有所幫助2013-11-11
Jquery AutoComplete自動完成 的使用方法實例
jQuery的Autocomplete(自動完成、自動填充)插件有不少,但比較下來我感覺,還是bassistance.de的JQuery Autocomplete plugin比較強大,我們就來寫一些代碼感受一下。2010-03-03

