js左右轮播代码

<script type="text/javascript">


//获取id
function $ (id)
{
return typeof id === "string" ? document.getElementById(id) : id;
}


//获取tagName
function $$ (elem, oParent)
{
return (oParent || document).getElementsByTagName(elem);
}
//获取class
function $$$ (className, oParent)


{
var aClass = [];
var reClass = new RegExp("(//s|^)" + className + "($|//s)");
var aElem = $$("*", oParent);
for (var i = 0; i < aElem.length; i++) reClass.test(aElem[i].className) && aClass.push(aElem[i]);
return aClass
}


//初始化对象
function Roll ()


{
this.initialize.apply(this, arguments)


}


Roll.prototype =
{
initialize: function (obj)


{


var _this = this;


this.obj = $(obj);


this.oUp = $$$("up", this.obj)[0];


this.oDown = $$$("down", this.obj)[0];


this.oList = $$$("list", this.obj)[0];


this.aItem = this.oList.children;


this.timer = null;


this.iWidth = this.aItem[0].offsetWidth;


this.oUp.onclick = function ()
{
_this.up()


};


this.oDown.onclick = function ()
{
_this.down()
}
},


up: function ()
{


this.oList.insertBefore(this.aItem[this.aItem.length - 1], this.oList.firstChild);


this.oList.style.left = -this.iWidth + "px";
this.doMove(0)


},


down: function ()
{


this.doMove(-this.iWidth, function ()
{


this.oList.appendChild(this.aItem[0]);


this.oList.style.left = 0;
})


},


doMove: function (iTarget, callBack)
{


var _this = this;


clearInterval(this.timer)


this.timer = setInterval(function ()
{


var iSpeed = (iTarget - _this.oList.offsetLeft) / 5;


iSpeed = iSpeed > 0 ? Math.ceil(iSpeed) : Math.floor(iSpeed);


_this.oList.offsetLeft == iTarget ? (clearInterval(_this.timer),
 callBack && callBack.apply(_this)) : _this.oList.style.left = iSpeed + _this.oList.offsetLeft + "px"
}, 30)


}


};


window.onload = function ()
{
new Roll("box");
};


</script>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值