基于jQuery實(shí)現(xiàn)一個(gè)marquee無縫滾動(dòng)的插件
基于jQuery,實(shí)現(xiàn)一個(gè)marquee無縫滾動(dòng)的插件,已經(jīng)發(fā)布到 git.oschina.net,演示稍后更新(更新到 http://git.oschina.net/mqycn/jQueryMarquee )。
代碼如下:
/**
* 類庫名稱:jQuery.marquee
* 實(shí)現(xiàn)功能:基于 jquery 實(shí)現(xiàn)的 marquee 無縫滾動(dòng)插件
* 作者主頁: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;
};
}
一共有三個(gè)可選參數(shù),一個(gè)回調(diào)方法。
direction,移動(dòng)方向:支持 左:left 右:right 上:top 下:bottom;
pixels,每次移動(dòng)的像素?cái)?shù)
speed,兩次移動(dòng)之前的間隔時(shí)間數(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實(shí)現(xiàn)一個(gè)marquee無縫滾動(dòng)的插件,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
使用jQuery處理AJAX請(qǐng)求的基礎(chǔ)學(xué)習(xí)教程
這篇文章主要介紹了使用jQuery處理AJAX請(qǐng)求的基礎(chǔ)學(xué)習(xí)教程,除此之外還引申了鏈?zhǔn)教幚硎录卣{(diào)的寫法,由淺入深非常值得借鑒,需要的朋友可以參考下2016-05-05
jQuery實(shí)現(xiàn)的圖文高亮滾動(dòng)切換特效實(shí)例
這篇文章主要介紹了jQuery實(shí)現(xiàn)的圖文高亮滾動(dòng)切換特效,涉及jquery基于鼠標(biāo)事件針對(duì)頁面元素遍歷與動(dòng)態(tài)操作的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-08-08
jQuery EasyUI 組件加上“清除”功能實(shí)例詳解
在使用 EasyUI 各表單組件時(shí),尤其是使用 ComboBox(下拉列表框)、DateBox(日期輸入框)、DateTimeBox(日期時(shí)間輸入框)這三個(gè)組件時(shí),經(jīng)常會(huì)遇到下拉框或日期只允許選擇、不允許手動(dòng)輸入功能,怎么解決呢,下面小編給大家分享解決方案,一起看看吧2017-04-04
Query中click(),bind(),live(),delegate()的區(qū)別
這篇文章主要介紹了Query中click(),bind(),live(),delegate()之間的區(qū)別。需要的朋友可以過來參考下,希望對(duì)大家有所幫助2013-11-11
JQuery復(fù)制DOM節(jié)點(diǎn)的方法
這篇文章主要介紹了JQuery復(fù)制DOM節(jié)點(diǎn)的方法,涉及jQuery中clone與appendTo方法的使用技巧,需要的朋友可以參考下2015-06-06
jQuery實(shí)現(xiàn)的原圖對(duì)比窗簾效果
這篇文章主要介紹了jQuery實(shí)現(xiàn)的原圖對(duì)比窗簾效果,需要的朋友可以參考下2014-06-06
Jquery AutoComplete自動(dòng)完成 的使用方法實(shí)例
jQuery的Autocomplete(自動(dòng)完成、自動(dòng)填充)插件有不少,但比較下來我感覺,還是bassistance.de的JQuery Autocomplete plugin比較強(qiáng)大,我們就來寫一些代碼感受一下。2010-03-03

