<!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" xml:lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
<title>Document</title>
<style type="text/css">
*{
margin:0;
padding: 0;
}
ul{
list-style: none;
}
ul li{
float: left;
}
img{
vertical-align: top;
}
#box{
width: 600px;
height: 200px;
background-color: pink;
margin: 100px auto;
overflow: hidden;
position: relative;
}
#scroll{
width: 1800px;
height: 200px;
position: absolute;
left: 0;
top: 0;
cursor: pointer;
}
</style>
<script type="text/javascript">
window.onload = function(){
var box = document.getElementById("box");
var scroll = document.getElementById("scroll");
var num = 0;// 存放scroll的left值;
var timer = null;// 定时器起始值;
timer = setInterval(fn,20);
function fn(){
num--;
// console.log(num);
num <= -1200? num =0 : num ;
scroll.style.left = num +"px";
}
//鼠标经过定时器时,关闭定时器
scroll.onmouseover = function(){
clearInterval(timer);
}
// 鼠标离开定时器时,打开定时器
scroll.onmouseout = function(){
timer = setInterval(fn,20);
}
}
</script>
</head>
<body>
<div id="box">
<ul id="scroll">
<li><img src="images/01.jpg"></li>
<li><img src="images/02.jpg"></li>
<li><img src="images/03.jpg"></li>
<li><img src="images/04.jpg"></li>
<li><img src="images/01.jpg"></li>
<li><img src="images/02.jpg"></li>
</ul>
</div>
</body>
</html>
生活中没有退步。只要我们肯学,无论什么经验都是我们需要领悟的章节。
来源:https://www.cnblogs.com/zhaocong/p/7053214.html