js大图匀速无缝滚动效果

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>无缝滚动</title>
<style>
*{margin:0;padding: 0;}
#box{
width: 500px;
height: 350px;
margin:20px auto;
overflow: auto;
}
#inner{
width: 2500px;
height: 350px;


}
#inner img{
float: left;
}
button{
width: 60px;
height: 30px;
}
</style>
</head>
<body>
<div id="box">
<div id="inner">
<img src="p1.jpg" alt="">
<img src="p2.png" alt="">
<img src="p3.jpg" alt="">
<img src="p4.jpg" alt="">
<img src="p1.jpg" alt="">
</div> 
</div>
<button>left</button><button>right</button>

<script>
var oBox = document.getElementById("box");
var oBtn = document.getElementsByTagName('button');
var time;
var speed;
// oBtn[0].onclick = function(){  //点击按钮left  图片向左移动
// clearInterval(time);
// time=setInterval(function(){
// oBox.scrollLeft +=1;
// if (oBox.scrollLeft==2000) {
// oBox.scrollLeft=0;
// }; 
// },1)
// }
// oBtn[1].onclick = function(){ //点击按钮right 图片向右移动
// clearInterval(time);
// time=setInterval(function(){
// oBox.scrollLeft -=1;
// if (oBox.scrollLeft==0) {
// oBox.scrollLeft=2000
// };
// },1)
// }


function move(max,min){
clearInterval(time)
time=setInterval(function(){
oBox.scrollLeft+=speed;
if (oBox.scrollLeft==max) {
oBox.scrollLeft=min
};
if (oBox.scrollLeft%500==0) {
clearInterval(time)
setTimeout(function(){
if (speed==1) {
move(2000,0)
}else{move(0,2000)}
},500)
};
},1)
}


oBtn[0].onclick = function(){
speed=1
move(2000,0)
}
oBtn[1].onclick = function(){
speed=-1
move(0,2000)
}
oBox.onmouseover = function(){
clearInterval(time);
}
oBox.onmouseout = function(){
if (speed==1) {
move(2000,0)
}else{
move(0,2000)
}
}
</script>
</body>
</html>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值