标签:style blog http os width io
基本思路:采用定时器,为鼠标添加onmouseover和onmouseout功能,采用上一篇文章所写的js运动的实现方法来实现网站侧栏的“分享到”功能。
<!DOCTYPE HTML>
<!--
-->
<html>
<head>
<meta charset="utf-8">
<title></title>
<style>
#div1{
width:150px;
height:200px;
background:green;
position:absolute;
top:50px;
left:-150px;
}
#div1 span{
position:absolute;
width:20px;
height:60px;
line-height:20px;
background:blue;
right:-20px;
top:70px;
}
</style>
<script>
window.onload=function ()
{
var oDiv=document.getElementById('div1');
oDiv.onmouseover=function ()
{
startMove(0);
};
oDiv.onmouseout=function ()
{
startMove(-150);
};
};
var timer=null;
function startMove(iTarget){
var oDiv=document.getElementById('div1');
clearInterval(timer);//保证只有一个定时器在工作,不会因为连续点击多次按钮而开启多个定时器,从而导致速度变快
timer=setInterval(function (){
var speed=0;
if(oDiv.offsetLeft>iTarget)
{
speed=-10;
}
else{
speed=10;
}
if(oDiv.offsetLeft==iTarget)
{
clearInterval(timer);
}
else
{
oDiv.style.left=oDiv.offsetLeft+speed+'px';
}
},30)
}
</script>
</head>
<body>
<div id="div1">
<span>分享到</span>
</div>
</body>
</html>效果图:
侧栏的“分享到”初始状态:
当鼠标移进“分享到”时,显示如下:
当鼠标移出该区域时,显示如下:
(37)JS运动之“分享到”移入移出功能,布布扣,bubuko.com
标签:style blog http os width io
原文地址:http://blog.csdn.net/xuan6251237011/article/details/38037005