您的位置:首页 > Web前端 > JavaScript

自己练习的无缝滚动原生JS

2012-11-03 16:17 330 查看
看了妙趣的课堂自己练习的,因为牵涉到远程就没利用图片 <!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>
<style>
div,ul,li,a{ margin:0; padding:0}
ul,li{list-style:none}
.roll{width:560px; margin:50px auto;overflow:hidden; position:relative}
.prev,.next{ width:80px;height:80px;background:#ccc; color:#fff}
.prev{position:absolute;left:0;top:20px;}
.next{position:absolute;right:0;top:20px;}
.scroll{position:relative;left:100px;width:360px;height:100px;overflow:hidden}
.scroll ul{position:absolute;left:0}
.scroll li{width:100px;height:100px;margin:0 10px; float:left;text-align:center}
</style>
<script>
window.onload = function(){
var odiv = document.getElementById('roll');
var oul = odiv.getElementsByTagName('ul')[0];
var oli = odiv.getElementsByTagName('li');
var obtn = odiv.getElementsByTagName('a')
var speed = 30;
var timer;
oul.innerHTML += oul.innerHTML;
oul.style.width = (oli[0].offsetWidth+oli[0].offsetLeft*2)*oli.length+'px';

timer = setInterval(function(){
oul.style.left = oul.offsetLeft+speed+'px';
if(oul.offsetLeft < -oul.offsetWidth/2){
oul.style.left = '0px'
}else if(oul.offsetLeft > 0){
oul.style.left = -oul.offsetWidth/2+'px'
}
},300)

obtn[0].onmouseover = function(){
speed =-30
}
obtn[1].onmouseover = function(){
speed =30
}
oul.onmouseover= function(){
clearInterval(timer)
}
oul.onmouseout= function(){
timer = setInterval(function(){
oul.style.left = oul.offsetLeft+speed+'px';
if(oul.offsetLeft < -oul.offsetWidth/2){
oul.style.left = '0px'
}else if(oul.offsetLeft > 0){
oul.style.left = -oul.offsetWidth/2+'px'
}
},300)
}

}
</script>
</head>

<body>
<div class="roll" id ="roll">
<a href="###" class="prev">左侧按钮</a>
<a href="###" class="next">右侧按钮</a>
<div class="scroll clearfix">
<ul>
<li style="background:#f7f">第一个</li>
<li style="background:#0fc">第二个</li>
<li style="background:red">第三个</li>
<li style="background:green">第四个</li>
</ul>
</div>
</div>
</body>
</html>


通过 为知笔记 发布
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: