为什么鼠标移开的时候不能回移?

<!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">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<style type="text/css">
all{ margin:0; padding:0;}
div{ background-color:red; width:200px; height:300px; left:-200px; position:relative;}
span{ background-color:blue;width:20px; height:40px; position:absolute;left:200px; color:#FFF; top:75px;}
</style>
<script>
window.onload=function(){
    var timer=null,
        a=document.getElementById('a');
    
    a.onmouseover=function(){
        Move(0);
    }
    a.onmouseout=function(){
        Move(-200);
        }
    function Move(num){
        clearInterval(timer);
        a=document.getElementById('a');
        if(a.offsetLeft<num){
        timer=setInterval(function(){
            if(a.offsetLeft >= num)
            {
                clearInterval(timer);
            
            }
            else{
                a.style.left=a.offsetLeft+ 1 +'px';
                }
            },30);
        }
        else if(a.offsetLeft>num){
            timer=setInterval(function(){
            if(a.offsetLeft == num)
            {
                clearInterval(timer);
            
            }
            else{
                a.style.left=a.offsetLeft- 1 +'px';
                }
            },30);}
    }
}

</script>
<title>无标题文档</title>
</head>
<div id="a"><span id="box">分享</span></div>
<body>
</body>
</html>


qq_吃药先森_03169839
浏览 1283回答 1
1回答

qq_青枣工作室_0

div{ background-color:red; width:200px; height:300px; left:-200px; position:relative;}这里的 position:relative 要改为 position:absolute;
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript