码迷,mamicode.com
首页 > Web开发 > 详细

网页边上的弹窗

时间:2015-05-26 15:38:05      阅读:122      评论:0      收藏:0      [点我收藏+]

标签:

鼠标移到分享到上,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{width:150px; height:200px; background:#0F0; position:absolute; left:-150px;}
#div1 span{position:absolute; width: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{width:150px; height:200px; background:#0F0; position:absolute; left:-150px;}
#div1 span{position:absolute; width: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>

 

网页边上的弹窗

标签:

原文地址:http://www.cnblogs.com/lzzhuany/p/4530554.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!