//左右无缝滚动
function ScrollLeftRight(divId, theType){
this.divId = divId;
this.oneWidth = 0;
this.LiArray = null;
this.mainObj = null;
this.beginObj = null;
this.endObj = null;
this.timmer = null;
this.speed = 20;//滚动速度,越大越慢,请不要调得过小
this.theType = theType;//left向左 right向右
this.indexaaaaa=0;
}
//初始化
ScrollLeftRight.prototype.Init = function(){
this.LiArray = $("#" + this.divId + " .list li");
this.oneWidth = $(this.LiArray[0]).width();
if (($("#" + this.divId + " .list").width()) > this.LiArray.length * this.oneWidth) {
return;//如果LI还没有宽带的一半则不实现滚动,也不复制层(已经包含了没有LI)
}
this.MakeHtml();
this.BindEvt();
}
//设置HTML
ScrollLeftRight.prototype.MakeHtml = function(){
var tempHtml = "";
tempHtml += "
";
tempHtml += "
" + $("#" + this.divId + " .list").html() + "
";
tempHtml += "
" + $("#" + this.divId + " .list").html() + "
";
tempHtml += "
";
$("#" + this.divId + " .list").html(tempHtml);
var tempWidth = $("#" + this.divId + " .list").width();
var tempHeight = $("#" + this.divId + " .list").height();
$("#" + this.divId + " .list .extendMain").css({
"width": tempWidth + "px",
"height": tempHeight + "px",
"overflow": "hidden"
});
$("#" + this.divId + " .list .extendWidth").css({
"width": parseInt(this.LiArray.length * this.oneWidth * 2, 10) + "px",
"height": tempHeight + "px",
"overflow": "hidden"
});
$("#" + this.divId + " .list .extendBegin,#" + this.divId + " .list .extendEnd,#" + this.divId + " .list .extendBegin ul,#" + this.divId + " .list .extendEnd ul").css({
"width": parseInt(this.LiArray.length * this.oneWidth, 10) + "px",
"height": tempHeight + "px",
"overflow": "hidden",
"float": "left"
});
this.mainObj = $("#" + this.divId + " .list .extendMain").get(0);
this.beginObj = $("#" + this.divId + " .list .extendBegin").get(0);
this.endObj = $("#" + this.divId + " .list .extendEnd").get(0);
}
//绑定时间
ScrollLeftRight.prototype.BindEvt = function(){
var theObj = this;
theObj.timmer = setInterval(function(){
theObj.Marquee();
}, theObj.speed);
$(this.mainObj).hover(function(){
if (theObj.timmer != null) {
clearInterval(theObj.timmer);
}
}, function(){
theObj.timmer = setInterval(function(){
theObj.Marquee();
}, theObj.speed);
});
}
//滚动
ScrollLeftRight.prototype.Marquee = function(){
if (this.theType == "right") {
if (this.mainObj.scrollLeft <= 0) {
this.mainObj.scrollLeft += this.endObj.offsetWidth;
}
else {
this.mainObj.scrollLeft--;
}
}
else {
if (this.endObj.offsetWidth - this.mainObj.scrollLeft <= 0) {
this.mainObj.scrollLeft -= this.beginObj.offsetWidth;
}
else {
this.mainObj.scrollLeft++;
}
}
}