HTML页面,简单的图片无缝滚动特效(向左,向右,暂定),通过speed来控制。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>网页图片无缝滚动-JavaScript</title>
<style type="text/css">
#div1{
width: 1200px;
height: 169px;
margin: 100px auto;
position: relative; /*相对定位*/
/*background: red;*/
overflow: hidden;
}
#div1 ul{
width: 1280px;
padding-left: 0;
position: absolute; /*绝对定位*/
left: 0;
top: 0;
margin-top:0;
margin-bottom: 0;
}
#div1 ul li{
width: 300px;
height: 169px;
/*margin-left: 20px;*/
float: left;
list-style: none;
}
</style>
<script type="text/javascript">
window.onload = function(){
var oDiv = document.getElementById('div1');
var oUl = oDiv.getElementsByTagName('ul')[0];
var aLi = oUl.getElementsByTagName('li');
var speed = null;
oUl.innerHTML=oUl.innerHTML+oUl.innerHTML;
oUl.style.width=aLi[0].offsetWidth*aLi.length+'px';
function move(){
if (oUl.offsetLeft<-oUl.offsetWidth/2) {
oUl.style.left='0';
}
if(oUl.offsetLeft>0){
oUl.style.left=-oUl.offsetWidth/2+'px';
}
oUl.style.left=oUl.offsetLeft+speed+'px';
}
var timer=setInterval(move,30);
oDiv.onmouseover=function(){
clearInterval(timer);
}
oDiv.onmouseout=function(){
timer=setInterval(move,30);
}
document.getElementsByTagName('a')[0].onclick=function(){
speed=-2;
}
document.getElementsByTagName('a')[1].onclick=function(){
speed=2;
}
document.getElementsByTagName('a')[2].onclick=function(){
speed=0;
}
}
</script>
</head>
<body>
<a href="javascript:;">向左</a>
<a href="javascript:;">向右</a>
<a href="javascript:;">暂停</a>
<div id="div1">
<ul>
<li><img src="gd/1.jpg"></li>
<li><img src="gd/2.jpg"></li>
<li><img src="gd/3.jpg"></li>
<li><img src="gd/4.jpg"></li>
</ul>
</div>
</body>
</html>