JavaScript实现分享侧边栏滑入滑出动画及代码优化

作者:小圆点时间:2026-10-28 23:49:25浏览:566

本文详细介绍利用JavaScript运动框架实现分享侧边栏滑入滑出动画的方法。通过对比初始实现与优化后的代码,展示了如何封装运动逻辑、统一速度控制以及简化事件处理,从而提升代码的可维护性与执行效率。

JavaScript实现分享侧边栏滑入滑出动画及代码优化

本文通过一个具体的实例,详细解析了使用JavaScript运动框架来实现分享侧边栏动画效果的方法。该方案旨在为开发者提供参考,帮助理解前端交互动画的实现逻辑。

以下是具体的代码实现部分:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<style type="text/css">
        *{  
margin:0px;
padding:0px;
        }  
#div1{
width:319px;
height: 340px;
border: 1px solid #FFF;
position: absolute;
top:100px;
left:-320px;
background-image: url(images/1.png);
background-repeat:no-repeat ;
            }  
#div1 span{
width:30px;
height: 130px;
border: 1px solid blue;
position: absolute;
right:-23px;
top:95px;
background: red;
font-family: "微软雅黑";
color: #FFFFFF;
text-align: center;
line-height: 40px;
border-radius: 0px 200px 200px 0px;
            }  
</style>
<script type="text/javascript">
window.onload=function(){
var oDiv=document.getElementById("div1");
var oSpan=oDiv.getElementsByTagName('span')[0];
var time=null;
var speed=8;
oDiv.onmouseover=function(){//这里给整个div加鼠标移入的事件
clearInterval(time);
time=setInterval(function(){
if(oDiv.offsetLeft>=0){clearInterval(time);}
else{
oDiv.style.left=oDiv.offsetLeft+speed+'px';
                        }  
},1);
                }  
oDiv.onmouseout=function(){//这里给整个div加鼠标移出事件
clearInterval(time);
time=setInterval(function(){
if(oDiv.offsetLeft<=-320){clearInterval(time);}
else{
oDiv.style.left=oDiv.offsetLeft-speed+'px';
                        }  
},1);
                }  
            }  
</script>
</head>
<body>
<div id="div1">
<span>分享到</span>
</div>
</body>
</html>

为了提升代码的可读性和复用性,以下是对上述代码进行优化后的版本:


优化后的代码如下:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<style type="text/css">
        *{  
margin:0px;
padding:0px;
        }  
#div1{
width:319px;
height: 340px;
border: 1px solid #FFF;
position: absolute;
top:100px;
left:-320px;
background-image: url(images/1.png);
background-repeat:no-repeat ;
            }  
#div1 span{
width:30px;
height: 130px;
border: 1px solid blue;
position: absolute;
right:-23px;
top:95px;
background: red;
font-family: "微软雅黑";
color: #FFFFFF;
text-align: center;
line-height: 40px;
border-radius: 0px 200px 200px 0px;
            }  
</style>
<script type="text/javascript">
window.onload=function(){
var oDiv=document.getElementById("div1");
var oSpan=oDiv.getElementsByTagName('span')[0];
var time=null;
var spe=8;
var speed=null;
function move(bord){
clearInterval(time);
time=setInterval(function(){
if(oDiv.offsetLeft>bord){speed=-spe;}
else{speed=spe;}
if(oDiv.offsetLeft==bord){clearInterval(time);}
else{
oDiv.style.left=oDiv.offsetLeft+speed+'px';
                          }  
},1);
                }  
oDiv.onmouseover=function(){//这里给整个div加鼠标移入的事件
move(0);
                }  
oDiv.onmouseout=function(){//这里给整个div加鼠标移出事件
move(-320);
                }  
            }  
</script>
</head>
<body>
<div id="div1">
<span>分享到</span>
</div>
</body>
</html>

希望本文所述的JavaScript程序设计技巧能对读者有所帮助。

作者最新文章