JS右侧悬浮框

Js右侧悬浮框

让一个div始终悬浮在右下角
在这里插入图片描述

<!DOCTYPE html>
<html lang="en">
<head>    
	<meta charset="UTF-8">    
	<title>Document</title>
	<style>    
		#div1{    
			  width: 100px;        
			  height: 150px;        
			  background: red;        
			  position: absolute;        
			  bottom: 0px;        
			  right: 0px;   
		}
	</style>
	<script>    
	window.onscroll = function(){        
		var oDiv = document.getElementById('div1');       
		 var scrollTop =document.documentElement.scrollTop||document.body.scrollTop;//浏览器兼容        
		 startmove(document.documentElement.clientHeight - oDiv.offsetHeight + scrollTop)                // document.documentElement.clientHeight 页面可视区高度   
	}            
	var timer = null;    
	
	//悬浮框缓冲运动
	
	function startmove(iTarget){        
		var oDiv = document.getElementById('div1');        
		clearInterval(timer);       
		timer = setInterval(function(){                        
			var speed = (iTarget-oDiv.offsetTop)/4;            
			speed = speed>0?Math.ceil(speed):Math.floor(speed);           
			 if(oDiv.offsetTop == iTarget){               
			 	 clearInterval(timer);          
			 }
		         else{                
		         	oDiv.style.top = oDiv.offsetTop +speed+'px';            
		         }
                },30)
         }                         
     </script>
</head>
<body style="height:2000px">    
	<div id="div1"></div>
</body>
</html>

对联悬浮

让div悬浮在右侧中间

在这里插入图片描述
只需要更改start move()函数中的数据

startmove(parseInt((document.documentElement.clientHeight - oDiv.offsetHeight )/2)+ scrollTop)//对联悬浮
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值