鼠标移到分享到上,div框一点点出现,移出div框,div一点点回到原来位置
<!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" />
<title>分享到</title>
<style>
#div1{150px; height:200px; background:#0F0; position:absolute; left:-150px;}
#div1 span{position:absolute; 20px; height:60px; line-height:20px; background:red; right:-20px; top:70px;}
</style>
<script language="javascript">
window.onload=function ()
{
oDiv=document.getElementById('div1');
oDiv.onmouseover=function ()//调用函数onmove1
{
onmove1();
}
oDiv.onmouseout=function ()//调用函数onmove2
{
onmove2();
}
}
var timer;
function onmove1()
{
oDiv=document.getElementById('div1');//得到div1
clearInterval(timer);//关闭所有定时器,防止定时器没有关闭而造成的错误
timer=setInterval(function (){
if(oDiv.offsetLeft==0)//若果到位置之后,关闭定时器
{
clearInterval(timer);
}
else
{
oDiv.style.left=oDiv.offsetLeft+10+'px';//出来是速度为正
}
},30);
}
function onmove2()
{
oDiv=document.getElementById('div1');
clearInterval(timer);
timer=setInterval(function (){
if(oDiv.offsetLeft==-150)
{
clearInterval(timer);
}
else
{
oDiv.style.left=oDiv.offsetLeft-10+'px';//进去时速度为负
}
},30);
}
</script>
</head>
<body>
<div id="div1">
<span id="sc1">分享到</span>
</div>
</body>
</html>
优化版:将两个函数变为一个函数
<!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" />
<title>无标题文档</title>
<style>
#div1{150px; height:200px; background:#0F0; position:absolute; left:-150px;}
#div1 span{position:absolute; 20px; height:60px; line-height:20px; background:red; right:-20px; top:70px;}
</style>
<script>
window.onload=function ()
{
var oDiv=document.getElementById('div1');
var timer;
var speed;
function onmove(value,speed){
clearInterval(timer);//关闭所有定时器,防止定时器没有关闭而造成的错误
timer=setInterval(function()
{
if(oDiv.offsetLeft!=value)
{
oDiv.style.left=oDiv.offsetLeft+speed+'px';
}
else
{
clearInterval(timer);
}
},30);
}
oDiv.onmouseover=function()
{
onmove(0,10);//到达0位置,速度为正10
}
oDiv.onmouseout=function()
{
onmove(-150,-10);//到达-150位置,速度为负10
}
}
</script>
</head>
<body>
<div id="div1">
<span id="sc1">分享到</span>
</div>
</body>
</html>