js实现左右轮播效果

来源:互联网 发布:实时 股票 分时 数据 编辑:程序博客网 时间:2024/06/10 00:20

demo代码如下:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html xmlns="http://www.w3.org/1999/xhtml">


<head>


<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />


<title>无缝滚动</title>


<script type="text/javascript" src="http://cdn.bootcss.com/jquery/1.10.2/jquery.min.js">
</script>


<style type="text/css">


body,div,ul,li{margin:0;padding:0;float:left;}
body{background:#052D01;font:12px/1.5 arial;}




#box li{list-style:none;text-align:center;}
#box{position:relative;width:700px;height:210px;border:1px solid #5E8743;margin:20px;}


#box .up{
position: absolute;top:20px;width:40px;height:40px;bottom: 2px;
}


#box .down{
position: absolute;top:20px;width:40px;height: 40px;left:550px;bottom: 2px;
}
#box div{
position: absolute;
left: 60px;
width:460px;
height: 100px;
overflow: hidden;
}
#box ul{
position: absolute;
height: 100px;


}


#box a{color:#fff;font-weight:700;text-decoration:none;}


#box img{width:156px;height:156px;display:block;margin-bottom:5px;border:2px solid #ccc;}






</style>


<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>
</head>


<body>
<div id="box">
<span class="up" style="background-color:red;">up</span>
<span class="down" style="background-color:red;">down</span>
<div>
<ul class="list" style="position: absolute;white-space: nowrap;margin:0;overflow: auto;list-style:none;width:600px">
<li style="display:inline;margin-left:10px"><a href="javascript:;"><img src="list3.png" style="width:100px;height:100px;" /></a></li>
<li style="display:inline;margin-left:10px"><a href="javascript:;"><img src="list2.png" style="width:100px;height:100px;" /></a></li>
<li style="display:inline;margin-left:10px"><a href="javascript:;"><img src="list3.png" style="width:100px;height:100px;" /></a></li>
<li style="display:inline;margin-left:10px"><a href="javascript:;"><img src="list4.png" style="width:100px;height:100px;" /></a></li>
<li style="display:inline;margin-left:10px"><a href="javascript:;"><img src="list2.png" style="width:100px;height:100px;" /></a></li>
</ul>
</div>
</div>
</body>
</html>
0 0
原创粉丝点击