$().ready( function() {
var box = new scrollbox("scrollbox"); //鍙傛暟涓烘粴鍔ㄥ鍣ㄧ殑id锛屽鍣ㄥ繀椤诲叿澶噊verflow:hidden灞炴€э紝瀹瑰櫒涓嬪繀椤诲叿澶囨爣绛炬牸寮忎负
銆�
box.clientwidth = 650; //鏄剧ず绐楀彛鍖哄煙瀹藉害銆�
box.step = 1; //婊氬姩姝ラ暱銆�
box.speed = 30; //婊氬姩姣忔闀挎墽琛屾椂闂达紙姣锛夈€�
box.space = 4; //濡傛灉浣跨敤margin鍋氬浘鐗囬棿闅旓紝鍦ㄦ璁剧疆闂撮殧璺濈銆�
box.forward = "left"; //婊氬姩鍒濆鏂瑰悜銆傚悜宸︽粴鍔紝鍊间负鈥渓eft鈥濓紝鍚戝彸婊氬姩锛屽€间负鈥渞ight鈥濄€�
box.norecord = "鏈爮鐩殏鏃犺祫鏂欙紝鎰熻阿鎮ㄧ殑鍏虫敞...
"; //鏆傛棤璁板綍鏃舵彃鍏ラ〉闈㈢殑html浠g爜銆�
box.control = true; //鏄惁鍙互鎺у埗婊氬姩鏂瑰悜銆傛槸涓簍rue锛屽惁涓篺alse銆�
box.ctrlclass = "ctrl"; //瀹氫箟鏂瑰悜鎺у埗鎸夐挳鐨勫睘鎬lass銆傚乏鍙充袱涓寜閽缃负鐩稿悓鐨刢lass锛屽苟涓旈〉闈腑涓嶈兘鍐嶆鍑虹幇杩欎釜class銆�
box.ctrlspeed = 10; //瀹氫箟鏂瑰悜鎺у埗鎸夐挳鎸変笅鏃剁殑閫熷害鍔犲€嶅€嶆暟銆�
box.stop = true; //榧犳爣鎮仠鏃讹紝鏄惁鍋滄銆傛槸涓簍rue锛屽惁涓篺alse銆�
box.default(); //鍒濆鍖栥€傚垵濮嬪寲瀹屾垚鍚庡皢鑷姩寮€濮嬫粴鍔ㄣ€�
} );
function scrollbox(box) {
this.box = $("#" + box);
this.name = "li宸﹀彸婊氬姩绫�";
this.author = "keboy";
this.version = "1.0";
this.createdate = "2009-12-15";
}
scrollbox.prototype = {
clientwidth: 0,
space: 0,
step: 0,
defstep: 0,
speed: 0,
forward: "",
norecord: "",
control: false,
ctrlclass: "",
stop: true,
defaulthtml: "",
defaulti: 1,
gocross: 0,
ctrlspeed: 0,
default: function() {
var _this = this;
this.defaulthtml = this.box.find("ul:first").html();
this.defstep = this.step;
this.box.find("li").each( function() {
_this.gocross += ( $(this).get(0).offsetwidth + _this.space );
} );
this.setdefault();
},
setdefault: function() {
var _this = this;
var li = this.box.find("li");
if ( li.length > 0 ) {
var liwidth = 0;
li.each( function() {
liwidth += ( $(this).get(0).offsetwidth + _this.space );
} );
if ( liwidth >= 2 * this.clientwidth ) {
var endhtml = this.box.find("ul:first").html();
endhtml += endhtml;
this.box.find("ul:first").html(endhtml).css("width",liwidth * 2 + "px");
this.start();
}
else {
var newhtml = "";
for ( var i = 0 ; i < this.defaulti ; i++ ) {
newhtml += this.defaulthtml;
}
this.defaulti++;
this.box.find("ul:first").html(newhtml);
this.setdefault();
}
}
else {
this.box.find("ul:first").html(this.norecord);
}
},
start: function() {
var _this = this;
var mar = setinterval(getscroll,this.speed);
function getscroll() {
if ( _this.forward == "left" ) {
if ( _this.box.get(0).scrollleft > _this.gocross ) {
_this.box.get(0).scrollleft -= _this.gocross;
}
else {
_this.box.get(0).scrollleft += _this.step;
}
}
else if ( _this.forward == "right" ) {
if ( _this.box.get(0).scrollleft <= 0 ) {
_this.box.get(0).scrollleft += _this.gocross;
}
else {
_this.box.get(0).scrollleft -= _this.step;
}
}
}
if ( this.control ) {
$("." + this.ctrlclass).css("cursor","pointer");
$("." + this.ctrlclass + ":first").mouseover( function() {
_this.forward = "left";
} ).mousedown( function() {
_this.step = _this.defstep * _this.ctrlspeed;
} );
$("." + this.ctrlclass + ":last").mouseover( function() {
_this.forward = "right";
} ).mousedown( function() {
_this.step = _this.defstep * _this.ctrlspeed;
} );
$(document).mouseup( function() {
_this.step = _this.defstep;
} );
}
if ( this.stop ) {
this.box.mouseover( function() {
clearinterval(mar);
} ).mouseout( function() {
mar = setinterval(getscroll,_this.speed);
} );
}
}
};