html+javascript实现广告窗自由浮动

首先介绍一种设置div初始位置的定位方法:

.html代码如下:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>

<head>
<meta http-equiv="content-type" content="text/html;charset=gbk"/>
<title>层的移动</title>
<style type="text/css">
#ad{width:120px;height:120px;background:url(images/default.gif);position:relative;}
</style>
<script type="text/javascript">
	
	var obj = null;
	
	
	function init(){
		obj = document.getElementById("ad");
		obj.style.left="10px";//设置初始位置
		obj.style.top="10px";
		alert(obj.style.posLeft);//获得方式一
		alert(parseInt(obj.style.left));//获得方式二
	}
	
	function move(){
		
	}

</script>

</head>
<body οnlοad="init()" >

<div id="ad"  >
	
</div>
</body>
</html>
实现广告窗口在可视范围内浮动代码如下:

</pre><pre name="code" class="html"><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>

<head>
<meta http-equiv="content-type" content="text/html;charset=gbk"/>
<title>层的移动</title>
<style type="text/css">
#ad{width:120px;height:120px;background:url(images/default.gif);position:relative;}
</style>
<script type="text/javascript">
	var id;
	var directionX=true,directionY=true;
	var x=10,y=10;
	var obj = null;
	
	
	function init(){
		obj = document.getElementById("ad");//获得对象
		obj.style.posLeft = x;//对象属性设置
		obj.style.posTop = y;
		id = setInterval(move,100);//100ms重复执行move方法
	}
	
	function move(){
		var width=document.documentElement.clientWidth-obj.offsetWidth;
		var height= document.documentElement.clientHeight-obj.offsetHeight;
		if(x>=width ){//说明移动到最右边
			directionX = false;
		}
		if(x==0){//移动到最左边
			directionX = true;
		}
		if(y >= height){
			directionY = false;
		}
		if(y==0){
			directionY = true;
		}
		if(directionX) 
			x += 10;
		else 
			x -= 10;
		
		if(directionY) 
			y += 10;
		else 
			y -= 10;
		obj.style.posLeft = x;
		obj.style.posTop = y;
	}

</script>

</head>
<body οnlοad="init()" >

<div id="ad">
	
</div>
</body>
</html>


  • 5
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值