Implementation of an marquee seamless scrolling plug in based on jQuery
- 2021-08-03 08:35:46
- OfStack
Based on jQuery, a plug-in for seamless scrolling of marquee has been released to git. oschina. net, and the demonstration will be updated later (updated to http://git.oschina.net/mqycn/jQueryMarquee).
The code is as follows:
/**
* Class library name: jQuery.marquee
* Implementation function: based on jquery Realized marquee Seamless scrolling plug-in
* Author's home page: http://www.miaoqiyuan.cn/
* Contact email address: mqycn@126.com
* Instructions for use: http://www.miaoqiyuan.cn/p/jquery-marquee
* Latest version: 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(){};
// Set Width
$(this).each(function(){
if( this.control ){
clearInterval(this.control);
} else {
// If the 1 Second execution, initialize the code
$(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);
}
});
If you use it under IE9, you need to add the following code before it:
/**
* IE8 Plug-ins (resolve function Not supported bind Problem of), non-original
*/
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;
};
}
1 has 3 optional parameters and 1 callback method.
direction, moving direction: support left: left right: right up: top down: bottom;
pixels, number of pixels per move
speed, the number of times between two moves (in milliseconds)
The call method is as follows:
$("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(" Executed 1 Times ");
});