<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>向左、向右、向上、向下无缝滚动</title>
<style type="text/css">
*{
padding: 0;
margin: 0;
}
.button{
width: 640px;
margin: 50px 0 0 80px;
}
.button input{
width: 80px;
height: 40px;
line-height: 40px;
border: none;
outline: none;
background: #8E3E8B;
border-radius: 10px;
color: #fff;
font-size: 14px;
margin-left: 20px;
cursor: pointer;
}
ul,li{list-style: none;}
.js-mask{
width: 640px;
height: 350px;
border: 1px solid black;
position: relative;
overflow: hidden;
margin: 20px 0 0 100px;
}
.js-mask ul{
position: absolute;
top: 0;
left: 0;
}
.js-mask ul li{
width: 640px;
height: 350px;
}
.js-mask ul img{
width: 640px;
height: 350px;
display: block;
border: none;
}
</style>
</head>
<body>
<div class="button">
<input type="button" value="向左滚动"/>
<input type="button" value="向右滚动"/>
<input type="button" value="向上滚动"/>
<input type="button" value="向下滚动"/>
</div>
<div class="js-mask">
<ul>
<li><img src="img/1.jpeg" alt="" /></li>
<li><img src="img/2.jpg" alt="" /></li>
<li><img src="img/3.jpg" alt="" /></li>
<li><img src="img/4.jpg" alt="" /></li>
<li><img src="img/5.jpeg" alt="" /></li>
<li><img src="img/6.jpg" alt="" /></li>
</ul>
</div>
</body>
<script type="text/javascript">
(function(){
var buttons=document.querySelectorAll('.button input');
var oUl = document.querySelectorAll('.js-mask ul')[0];
oUl.innerHTML+=oUl.innerHTML;
var speed = 6;
var timer;
var oLies = document.querySelectorAll('.js-mask li');
buttons[0].onclick = function(){
clearInterval(timer);
for(var i=0;i<oLies.length;i++){
oLies[i].style.float='left';
};
oUl.style.width=oLies[0].offsetWidth * oLies.length + 'px';
timer=setInterval(function(){
var speed = -6;
if(oUl.offsetLeft <= -oUl.offsetWidth*.5){
oUl.style.left = '0px';
};
oUl.style.left = oUl.offsetLeft + speed + 'px';
},100);
};
buttons[1].onclick = function(){
clearInterval(timer);
for(var i=0;i<oLies.length;i++){
oLies[i].style.float='left';
};
var speed = 6;
oUl.style.left=oUl.offsetWidth*.5;
oUl.style.width=oLies[0].offsetWidth * oLies.length + 'px';
timer=setInterval(function(){
if(oUl.offsetLeft >= 0){
oUl.style.left = '-640px';
};
oUl.style.left = oUl.offsetLeft + speed + 'px';
},100);
};
buttons[2].onclick = function(){
clearInterval(timer);
var speed = -6;
oUl.style.height=oLies[0].offsetHeight * oLies.length + 'px';
timer=setInterval(function(){
if(oUl.offsetTop<=-oUl.offsetHeight*.5){
oUl.style.top = '0px';
};
oUl.style.top = oUl.offsetTop + speed +'px';
},100);
};
buttons[3].onclick = function(){
clearInterval(timer);
var speed = 6;
oUl.style.top='-350px';
oUl.style.height=oLies[0].offsetHeight * oLies.length + 'px';
timer=setInterval(function(){
if(oUl.offsetTop>=0){
oUl.style.top = '-350px';
};
oUl.style.top = oUl.offsetTop + speed +'px';
},100);
};
})()
</script>
</html>