js控制div层的显示跟随滚动条滚动而滚动

原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 、作者信息和本人声明。否则将追究法律责任。
作者: 永恒の_☆ 地址: http://blog.csdn.net/chenghui0317/article/details/9004716


js代码如下:

<!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>
<title>无标题页</title>
  <style>
  
  .header_wrap {
    background: none repeat scroll 0 0 #488FCE;
    box-shadow: 0 1px 3px rgba(0, 0, 0, 0.3);
    display: block;
    left: 0;
    position: fixed;
    top: 0;
    width: 100%;
    z-index: 10;
}
ul li{
	float:left;
	margin:10px;
	padding:0px;
}
  </style>
</head>
<body style="height:1500px">

<header class="header_wrap">
	<div class="nav">
	<ul>
		<li class=""><a href="http://www.phpwind.net/index.php?m=bbs&c=forumlist">首页</a></li>
		<li class="current"><a href="http://www.phpwind.net/index.php?m=bbs">新帖</a></li>
		<li class=""><a href="http://www.phpwind.net/index.php?m=like&c=like">喜欢</a></li>
		<li class=""><a href="http://www.phpwind.net/index.php?m=tag">话题</a></li>
		<li class=""><a href="http://open.phpwind.com/" target="_blank">云平台</a></li>
		<li class=""><a href="http://www.phpwind.com/index.php?m=downloads&a=downloadsphpwind" target="_blank">下载</a></li>
	</ul>
	</div>
</header>
<!--右边-->
<div id="divRight">
	<style type="text/css">
	#divMsgInfo{
		position:absolute;
		width:90px;
		height:300px;
		font-size:9pt;
	}
	</style>
	<script type="text/javascript">
		var lastScrollY=0;
		function heartBeat(){ 
			diffY=document.documentElement.scrollTop; 
			percent=0.1*(diffY-lastScrollY); 
			if(percent>0)percent=Math.ceil(percent); 
			else percent=Math.floor(percent); 
			document.getElementById("divMsgInfo").style.top=parseInt(document.getElementById("divMsgInfo").style.top)+percent+"px";
			lastScrollY=lastScrollY+percent; 
		}
		var timeright = window.setInterval("heartBeat()",1);
	</script>

	<div id="divMsgInfo"  runat="server" style=" right:10px; top:120px;">
		<img src ="left.gif" />
		<div style="float:right; color:White"><a οnclick="clearInterval(timeright);document.getElementById('divRight').innerHTML = ''" style="color:White; cursor:pointer">关闭</a></div>
	</div>
</div>



<!--左边-->
<div id="divLeftFloat">
	<style type="text/css">  
	#divMsgLeftInfo{  
		position:absolute;  
		width:90px; 
		height:318px;   
		font-size:9pt;  
	}   
	</style>     
	<script type="text/javascript">   
		var lastScrollYLeft=0;  
		function heartBeatLeft(){    
			diffYLeft=document.documentElement.scrollTop;    
			percent=0.1*(diffYLeft-lastScrollYLeft);    
			if(percent>0)
				percent=Math.ceil(percent);     
			else percent=Math.floor(percent);     
			document.getElementById("divMsgLeftInfo").style.top=parseInt(document.getElementById("divMsgLeftInfo").style.top)+percent+"px";     
			lastScrollYLeft=lastScrollYLeft+percent;    
		}   
		var timeleft = window.setInterval("heartBeatLeft()",1);  
	</script>     
	<div id="divMsgLeftInfo" style=" left:10px; top:120px;">
		<a href="http://www.ispeak.cn" target="_blank">
		<img border="0" src ="/Files/index/FloatImg/20101112115620528353991.png" /></a>               
		<div style="float:right; color:White">
			<a οnclick="clearInterval(timeleft);document.getElementById('divLeftFloat').innerHTML = ''" style="color:White; cursor:pointer">关闭</a>
		</div>    
	</div>  
</div>

<!--底部-->
<div id="divBottomFloat">
	<style type="text/css">
	#divMsgBottomInfo{
		position:absolute;
		/*background:#eee;*/
		width:330px;
		height:188px;
		font-size:9pt;
		/* border-color:#442200;*/
		/* border-color:#000000;*/
	}
	</style>
	<script type="text/javascript">
		var lastScrollYBottom=0;
		function heartBeatBottom(){ 
			var isIE = document.all && window.external;
			bodyScrollTop = document.documentElement.scrollTop || document.body.scrollTop;
			if(isIE){
				docHeight = document.documentElement.clientHeight || document.body.clientHeight;
			}else{
				docHeight = (document.body.clientHeight > document.documentElement.clientHeight)?document.documentElement.clientHeight:document.body.clientHeight;
			}
			diffYBottom=(docHeight - parseInt(document.getElementById("divMsgBottomInfo").offsetHeight,10)) + parseInt(bodyScrollTop, 10);
			document.getElementById("divMsgBottomInfo").style.top=diffYBottom+"px";
		}
		var timebottom=window.setInterval("heartBeatBottom()",1);
	</script>
	<div id="divMsgBottomInfo"  runat="server" style=" right:10px; top:500px;">
		<div style="float:right; color:White"><a οnclick="clearInterval(timebottom);document.getElementById('divBottomFloat').innerHTML = ''" style="color:White; cursor:pointer">关闭</a></div>
		<img src ="/images/tmp2.jpg" style="width:330px; height:170px" />   
	</div>
</div>
</body>
</html>


  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
一、遇顶固定的例子 我一直以为是某个div随屏幕滚动,遇顶则固定,离开浏览器顶部又还原这样的例子其实不少,其实它的名字叫“的智能浮动效果”。目前我们在国内的商业网站上就常常看到这样的效果了。例如淘宝网的搜索结果页的排序水平条,在默认状态时,该工具条是跟随页面滚动的,如下图: 而当我们下拉滚动条,随着页面向下滚动,当此工具条接触到浏览器的上边缘时,这时就会独立出来固定在顶部,不跟随页面滚动滚动了,如下图: 类似的例子效果我们在别的网站上都有看到过,例如腾讯微博首页上,当我们下拉屏幕浏览最新微博时,工具条也会出现这个效果,如下图: 这个效果看上去方便,贴心,也许还带推动广告的促销呢。原理其实很简单,本文展示两种方法。 二、智能浮动效果的实现原理 它分两种状态,一是默认状态,二是浮动固顶状态。 默认状态就是默认状态,什么也不用做,保持原有的CSS就好。无论有没有对它做定位,做了absolute也好,没做也好,都行。 关键是当浏览器屏幕滚动时,该对象div要移除浏览器界面视区的时候,是要修改它的position属性,让它浮动在窗口的上沿显示就行了。最好的position属性是fixed,可以在IE6+和其他浏览器浮动平滑的固定定位,由于IE6老大哥不支持fixed属性,所以可以另外赋予它absolute属性。当然也会产生副作用——滚动不平滑。不过也无所谓了,在微软公布不再修补更新XP漏洞,在中国360安全卫士的帮助下虽然大家仍然使用XP,但IE的用户已经是少之又少了。 那接下来,如何判断当前div与浏览器窗口的上边缘接触了呢?遇到浏览器顶部了呢? 当浮动div与浏览器窗口上边缘接触的一瞬间,其页面垂直偏移值与页面的滚动高度是一致的,所以我们就用这个进行判断。那如何获得页面上元素距离页面的垂直距离呢? 这里则是使用javaScript库实现。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值