<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>JavaScript 高级课程之分享setInterval(),clearInterval(),onmouseover,onmouseout应用(2_自动判断正负)</title>
<style>
#div1 { width:100px; height:300px; background-color:#005812; position:absolute; left:-100px;}
#div2 { width:30px; height:100px; position:absolute; background-color:#CBBC5D; right:-30px; top:100px;}
</style>
<script>
window.onload = function(){
var oDiv = document.getElementById('div1');
var Timer = null;
oDiv.onmouseover = function(){
fn(0);
};
oDiv.onmouseout = function(){
fn(-100);
};
function fn(value){
if(value < 0){
var si = -10;
}else{
var si = 10;
}
clearInterval(Timer);
Timer = setInterval(function(){
if(oDiv.offsetLeft != value){
oDiv.style.left = oDiv.offsetLeft + si +'px';
}else{
clearInterval(Timer);
}
},20);
}
}
</script>
</head>
<body>
<div id="div1">
<div id="div2"></div>
</div>
</body>
</html>
JavaScript 高级课程之分享setInterval(),clearInterval(),onmouseover,onmouseout应用(2_自动判断正负)
最新推荐文章于 2022-11-22 11:49:20 发布