<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Home</title>
<!-- <script src="../js/jquery.min.js" type="text/javascript"></script> -->
<style type="text/css">
*{
margin:0;
padding: 0;
}
#demo {
width: 512px;
height: 400px;
margin: 100px auto;
border: 1px solid #f00;
overflow: hidden;
position: relative;
}
#demo span{
width: 100%;
height: 200px;
position: absolute;
left: 0;
cursor: pointer;
}
#up{
top: 0;
}
#down{
bottom: 0;
}
#pic{
top :0;
position: absolute;
}
</style>
</head>
<body>
<div id="demo">
<img src="images/mi.png" id="pic"/>
<span id="up"></span>
<span id="down"></span>
</div>
</body>
</html>
<script type="text/javascript">
function $(id){return document.getElementById(id);}
var num = 0;
var timer = null;
$("up").onmouseover = function(){
clearInterval(timer);//永远记住,要用定时器,,先清除定时器
timer = setInterval(function(){
num -=3;
if(num >= -1070){
$("pic").style.top = num +"px";//如果图片滚动到顶,,
}
else{
clearInterval(timer);//那么就清除定时器
}
},30);
}
$("down").onmouseover = function(){
clearInterval(timer);
timer = setInterval(function(){
num ++;
if(num < 0 ){
$("pic").style.top = num +"px";
}
else{
clearInterval(timer);
}
},30);
}
$("up").parentNode.onmouseout = function(){//鼠标不在demo的范围内,也会清除定时器,要不然会记录上一次方向不停的滚动
clearInterval(timer);
}
</script>
<html>
<head>
<meta charset="UTF-8">
<title>Home</title>
<!-- <script src="../js/jquery.min.js" type="text/javascript"></script> -->
<style type="text/css">
*{
margin:0;
padding: 0;
}
#demo {
width: 512px;
height: 400px;
margin: 100px auto;
border: 1px solid #f00;
overflow: hidden;
position: relative;
}
#demo span{
width: 100%;
height: 200px;
position: absolute;
left: 0;
cursor: pointer;
}
#up{
top: 0;
}
#down{
bottom: 0;
}
#pic{
top :0;
position: absolute;
}
</style>
</head>
<body>
<div id="demo">
<img src="images/mi.png" id="pic"/>
<span id="up"></span>
<span id="down"></span>
</div>
</body>
</html>
<script type="text/javascript">
function $(id){return document.getElementById(id);}
var num = 0;
var timer = null;
$("up").onmouseover = function(){
clearInterval(timer);//永远记住,要用定时器,,先清除定时器
timer = setInterval(function(){
num -=3;
if(num >= -1070){
$("pic").style.top = num +"px";//如果图片滚动到顶,,
}
else{
clearInterval(timer);//那么就清除定时器
}
},30);
}
$("down").onmouseover = function(){
clearInterval(timer);
timer = setInterval(function(){
num ++;
if(num < 0 ){
$("pic").style.top = num +"px";
}
else{
clearInterval(timer);
}
},30);
}
$("up").parentNode.onmouseout = function(){//鼠标不在demo的范围内,也会清除定时器,要不然会记录上一次方向不停的滚动
clearInterval(timer);
}
</script>