barra de menu js movida para dentro e para fora

área de conteúdo html e css


<!DOCTYPE html>
<html>
<head>
	<title>分享到</title>
	<style type="text/css">
		*{
			padding:0px;
			margin:0px;
		}
		#box{
           width:150px;
           height:200px;
           background-color:pink;
           position:relative;
           top:100px;
           left:-150px;
		}
		#box span{
			width:50px;
			height:100px;
			background-color:green;
			position:absolute;
			right:-50px;
			top:50px;
			color:white;

		}

	</style>
</head>
<body>
    <div id="box">
    	<span>分享到</span>
    </div>
</body>

área de conteúdo js

<script type="text/javascript">
	//left:10  itarget:100 正   
	//left:600  iTarget:50 负
	window.onload = function(){
		  
        var Dbox = document.getElementById("box");
        
        Dbox.onmouseover = function(){
        	// startMove(10,0);
        	startMove(0);
        }
        Dbox.onmouseout = function(){
        	// startMove(-10,-150);
        	startMove(-150);
        }
}
    var timer = null; 
    // var speed = 10;
    var Dbox = document.getElementById("box");
    function startMove(iTarget){

            clearInterval(timer);//先将之前的定时器关了再执行下面的函数
           	timer = setInterval(function(){
               var speed = 0;
               //当offsetLeft大于iTarget的时候,speed等于负,否则是正的
               if(Dbox.offsetLeft > iTarget){
               	  speed =-10;
               }else{
                  speed =10;
               }

           	   if(Dbox.offsetLeft == iTarget){
                  clearInterval(timer);
           	   	}else{
           	   	   Dbox.style.left = Dbox.offsetLeft+speed+'px';
           	   	}
   	  
           },30)

        }

</script>

![在这里插入图片描述](https://img-blog.csdnimg.cn/20200614152018670.jpg)

Acho que você gosta

Origin blog.csdn.net/weixin_43465609/article/details/106746000
Recomendado
Clasificación