html 代码:
代码如下:
CSS代码
代码如下:
*{
margin:0;
padding: 0;
}
#div1{
overflow: hidden;
background: blue;
position: relative;
width: 600px;
height: 150px;
margin:100px auto;
}
#div1 ul{
position: absolute;
left: 0px;
top: 0px;
list-style: none;
}
#div1 ul li{
float: left;
}
#div1 ul li img{
width:150px;
height:150px;
}
js:代码
代码如下:
window.onload=function(){
var oDiv=document.getElementById('div1');
var oUl=oDiv.getElementsByTagName('ul')[0];
var aLi=oUl.getElementsByTagName('li');
var timer=null;
var speed=2;//控制滚动速度以及方向
oUl.innerHTML=oUl.innerHTML+oUl.innerHTML;
oUl.style.width=aLi[0].offsetWidth*aLi.length+'px';
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;
}
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';
}
}
效果是不是非常棒呢。
Copyright © 2019- huatuo7.cn 版权所有 湘ICP备2022005869号-9
违法及侵权请联系:TEL:199 18 7713 E-MAIL:2724546146@qq.com
本站由北京市万商天勤律师事务所王兴未律师提供法律服务