js 悬浮窗

<!DOCTYPE html >
<html>
<head>
	<meta charset="UTF-8">
	<title>js 悬浮窗</title>
	<script type="text/javascript">
		var container,win,ctr_width,ctr_height,ctr_left,ctr_top;
		var win_left,win_top,win_width,win_height,x_offset=5,y_offset=5;
		function log(msg){
			console.log(msg);			
		}
		function moveWin(){	
		    win_left=parseInt(win.style.left); 		
		    win_top=parseInt(win.style.top);
			if(win_left<ctr_left||win_left>(ctr_left+ctr_width-win_width)){
				x_offset=-1*x_offset;
			}
			if(win_top<ctr_top||win_top>(ctr_top+ctr_height-win_height)){
				y_offset=-1*y_offset;
			}			
			win.style.left=(win_left+x_offset)+"px";
			win.style.top=(win_top+y_offset)+"px";	
			setTimeout('moveWin()',100);
		}
		function init(win_t,con_t){
			win=win_t;
			container=con_t;
			ctr_width=container.clientWidth;		
			ctr_height=container.clientHeight;			
			win_width=win.clientWidth;
			win_height=win.clientHeight;	
			ctr_left=container.offsetLeft
			ctr_top=container.offsetTop;			
			win.style.left=ctr_left+"px";
			win.style.top=ctr_top+"px";
		}
		window.οnlοad=function(){
			init(document.getElementById("win"),document.getElementById("container"));
			moveWin();
		}		
	</script>	
	<style>
		.win{
			position:absolute;
			width: 100px;
			height: 100px;
		    left:0;
		    top:0;
		    border:1px solid red;
		    width:300px;
		    height:250px;
		    background-color: #222222;	
		}
		#container{
			margin: 0 auto; 
			background-color: red;		
			width: 600px;	
			height: 1000px;
		}
	</style>
</head>
<body>
	<div id="container">
		<div id="win" class="win">
			win

		</div>
	</div>

</body>
</html>

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值