<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>js动画效果练习1</title>
<style type="text/css">
*{
margin: 0;
padding: 0;
}
#scroll{
margin-top: 100px;
width: 200px;
height: 200px;
position: absolute;
left: -190px;
background: red;
}
.tip{
width: 30px;
height: 60px;
background: blue;
/* position: relative;*/
top: 70px;
left: 200px;
}
</style>
<script type="text/javascript">
// floor向下取整
// alert(Math.floor(3.98))
// 向上取整
// alert(Math.ceil(3.98))
window.onload = function(){
var Div = document.getElementById('scroll');
Div.onmouseover = function(){
startMove(0);
}
Div.onmouseout = function(){
startMove(-190);
}
}
var timer = null;
var alpha = 30;
function startMove(iTarget){
var Div = document.getElementById('scroll');
clearInterval(timer);
timer = setInterval(function(){
var speed = (iTarget - Div.offsetLeft) / 20;
speed = speed > 0?Math.ceil(speed):Math.floor(speed);
if (alpha == iTarget) {
clearInterval(timer);
}else{
Div.style.left = Div.offsetLeft + speed + 'px';
}
} ,30)
}
</script>
</head>
<body>
<div id="scroll">
<!-- <div class="tip"> -->
</div>
</div>
</body>
</html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>js动画效果练习1</title>
<style type="text/css">
*{
margin: 0;
padding: 0;
}
#scroll{
margin-top: 100px;
width: 200px;
height: 200px;
position: absolute;
left: -190px;
background: red;
}
.tip{
width: 30px;
height: 60px;
background: blue;
/* position: relative;*/
top: 70px;
left: 200px;
}
</style>
<script type="text/javascript">
// floor向下取整
// alert(Math.floor(3.98))
// 向上取整
// alert(Math.ceil(3.98))
window.onload = function(){
var Div = document.getElementById('scroll');
Div.onmouseover = function(){
startMove(0);
}
Div.onmouseout = function(){
startMove(-190);
}
}
var timer = null;
var alpha = 30;
function startMove(iTarget){
var Div = document.getElementById('scroll');
clearInterval(timer);
timer = setInterval(function(){
var speed = (iTarget - Div.offsetLeft) / 20;
speed = speed > 0?Math.ceil(speed):Math.floor(speed);
if (alpha == iTarget) {
clearInterval(timer);
}else{
Div.style.left = Div.offsetLeft + speed + 'px';
}
} ,30)
}
</script>
</head>
<body>
<div id="scroll">
<!-- <div class="tip"> -->
</div>
</div>
</body>
</html>