概述
非常好的一个用户体验实例。如果不明白的话就看下面的图片吧
淘宝产品列表 http://search1.taobao.com/browse/50010388/…
腾讯财经产业经济 http://finance.qq.com/industry/index.htm
如何实现?
scrollTop是什么?当元素内部的内容高度超过元素本身的高度时就会出现滚动条,随着拖动滚动条而被隐去的上面部分的高度就是scrollTop值。
这种定位层就是当页面触发onscroll事件时,取得scrollTop值并写入定位层CSS属性,使之保持位置。
下面是实例代码
复制代码 代码如下:
<!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=gb2312" />
<title>定位层</title>
<script type="text/javascript" src="http://img.uoften.com/jslib/jquery/jquery.js"></script>
<style type="text/css">
body{padding:10px;margin:0;}
#box{width:960px;margin:0 auto;}
#box1{width:250px;height:25px;position:relative;}
#box1_1{width:250px;height:25px;line-height:25px;background:#f00;color:#fff;}
p{font-size:24px;margin:10px 0;}
</style>
<script type="text/javascript">
function tools(){
var top=$(document).scrollTop();
if(($.browser.msie==true)&&($.browser.version==6.0)){
if(top>168)$("#box1_1").css({position:"absolute",top:top-168});
}else{
if(top>168)$("#box1_1").css({position:"fixed",top:"-"&top+"px"});
}
if(top<=168)$("#box1_1").css({position:"static",top:0});
}
$(function(){
window.onscroll=tools;
window.onresize=tools;
});
</script>
</head>
<body>
<div id="box">
<p>往下拖动滚动条↓<a href="http://www.uoften.com">uoften.com</a> <a href="http://www.uoften.com">脚本之家</a></p>
<p><a href="http://www.uoften.com">uoften.com</a> <a href="http://www.uoften.com">uoften.com</a> <a href="http://www.uoften.com">uoften.com</a> <a href="http://www.uoften.com">uoften.com</a> <a href="http://www.uoften.com">脚本之家</a></p>
<p><a href="http://www.uoften.com">uoften.com</a> <a href="http://www.uoften.com">uoften.com</a> <a href="http://www.uoften.com">uoften.com</a> <a href="http://www.uoften.com">uoften.com</a> <a href="http://www.uoften.com">脚本之家</a></p>
<p><a href="http://www.uoften.com">uoften.com</a> <a href="http://www.uoften.com">uoften.com</a> <a href="http://www.uoften.com">uoften.com</a> <a href="http://www.uoften.com">uoften.com</a> <a href="http://www.uoften.com">脚本之家</a></p>
<div id="box1"><div id="box1_1">这里是定位层</div></div>
<p><a href="http://www.uoften.com">uoften.com</a> <a href="http://www.uoften.com">uoften.com</a> <a href="http://www.uoften.com">uoften.com</a> <a href="http://www.uoften.com">uoften.com</a> <a href="http://www.uoften.com">脚本之家</a></p>
<p><a href="http://www.uoften.com">uoften.com</a> <a href="http://www.uoften.com">uoften.com</a> <a href="http://www.uoften.com">uoften.com</a> <a href="http://www.uoften.com">uoften.com</a> <a href="http://www.uoften.com">脚本之家</a></p>
<p><a href="http://www.uoften.com">uoften.com</a> <a href="http://www.uoften.com">uoften.com</a> <a href="http://www.uoften.com">uoften.com</a> <a href="http://www.uoften.com">uoften.com</a> <a href="http://www.uoften.com">脚本之家</a></p>
<p><a href="http://www.uoften.com">uoften.com</a> <a href="http://www.uoften.com">uoften.com</a> <a href="http://www.uoften.com">uoften.com</a> <a href="http://www.uoften.com">uoften.com</a> <a href="http://www.uoften.com">脚本之家</a></p>
<p><a href="http://www.uoften.com">uoften.com</a> <a href="http://www.uoften.com">uoften.com</a> <a href="http://www.uoften.com">uoften.com</a> <a href="http://www.uoften.com">uoften.com</a> <a href="http://www.uoften.com">脚本之家</a></p>
<p><a href="http://www.uoften.com">uoften.com</a> <a href="http://www.uoften.com">uoften.com</a> <a href="http://www.uoften.com">uoften.com</a> <a href="http://www.uoften.com">uoften.com</a> <a href="http://www.uoften.com">脚本之家</a></p>
<p><a href="http://www.uoften.com">uoften.com</a> <a href="http://www.uoften.com">uoften.com</a> <a href="http://www.uoften.com">uoften.com</a> <a href="http://www.uoften.com">uoften.com</a> <a href="http://www.uoften.com">脚本之家</a></p>
<p><a href="http://www.uoften.com">uoften.com</a> <a href="http://www.uoften.com">uoften.com</a> <a href="http://www.uoften.com">uoften.com</a> <a href="http://www.uoften.com">uoften.com</a> <a href="http://www.uoften.com">脚本之家</a></p>
<p><a href="http://www.uoften.com">uoften.com</a> <a href="http://www.uoften.com">uoften.com</a> <a href="http://www.uoften.com">uoften.com</a> <a href="http://www.uoften.com">uoften.com</a> <a href="http://www.uoften.com">脚本之家</a></p>
<p><a href="http://www.uoften.com">uoften.com</a> <a href="http://www.uoften.com">uoften.com</a> <a href="http://www.uoften.com">uoften.com</a> <a href="http://www.uoften.com">uoften.com</a> <a href="http://www.uoften.com">脚本之家</a></p>
<p><a href="http://www.uoften.com">uoften.com</a> <a href="http://www.uoften.com">uoften.com</a> <a href="http://www.uoften.com">uoften.com</a> <a href="http://www.uoften.com">uoften.com</a> <a href="http://www.uoften.com">脚本之家</a></p>
<p><a href="http://www.uoften.com">uoften.com</a> <a href="http://www.uoften.com">uoften.com</a> <a href="http://www.uoften.com">uoften.com</a> <a href="http://www.uoften.com">uoften.com</a> <a href="http://www.uoften.com">脚本之家</a></p>
<p><a href="http://www.uoften.com">uoften.com</a> <a href="http://www.uoften.com">uoften.com</a> <a href="http://www.uoften.com">uoften.com</a> <a href="http://www.uoften.com">uoften.com</a> <a href="http://www.uoften.com">脚本之家</a></p>
<p><a href="http://www.uoften.com">uoften.com</a> <a href="http://www.uoften.com">uoften.com</a> <a href="http://www.uoften.com">uoften.com</a> <a href="http://www.uoften.com">uoften.com</a> <a href="http://www.uoften.com">脚本之家</a></p>
<p><a href="http://www.uoften.com">uoften.com</a> <a href="http://www.uoften.com">uoften.com</a> <a href="http://www.uoften.com">uoften.com</a> <a href="http://www.uoften.com">uoften.com</a> <a href="http://www.uoften.com">脚本之家</a></p>
<p><a href="http://www.uoften.com">uoften.com</a> <a href="http://www.uoften.com">uoften.com</a> <a href="http://www.uoften.com">uoften.com</a> <a href="http://www.uoften.com">uoften.com</a> <a href="http://www.uoften.com">脚本之家</a></p>
<p><a href="http://www.uoften.com">uoften.com</a> <a href="http://www.uoften.com">uoften.com</a> <a href="http://www.uoften.com">uoften.com</a> <a href="http://www.uoften.com">uoften.com</a> <a href="http://www.uoften.com">脚本之家</a></p>
<p><a href="http://www.uoften.com">uoften.com</a> <a href="http://www.uoften.com">uoften.com</a> <a href="http://www.uoften.com">uoften.com</a> <a href="http://www.uoften.com">uoften.com</a> <a href="http://www.uoften.com">脚本之家</a></p>
<p><a href="http://www.uoften.com">uoften.com</a> <a href="http://www.uoften.com">uoften.com</a> <a href="http://www.uoften.com">uoften.com</a> <a href="http://www.uoften.com">uoften.com</a> <a href="http://www.uoften.com">脚本之家</a></p>
<p><a href="http://www.uoften.com">uoften.com</a> <a href="http://www.uoften.com">uoften.com</a> <a href="http://www.uoften.com">uoften.com</a> <a href="http://www.uoften.com">uoften.com</a> <a href="http://www.uoften.com">脚本之家</a></p>
<p><a href="http://www.uoften.com">uoften.com</a> <a href="http://www.uoften.com">uoften.com</a> <a href="http://www.uoften.com">uoften.com</a> <a href="http://www.uoften.com">uoften.com</a> <a href="http://www.uoften.com">脚本之家</a></p>
<p><a href="http://www.uoften.com">uoften.com</a> <a href="http://www.uoften.com">uoften.com</a> <a href="http://www.uoften.com">uoften.com</a> <a href="http://www.uoften.com">uoften.com</a> <a href="http://www.uoften.com">脚本之家</a></p>
<p><a href="http://www.uoften.com">uoften.com</a> <a href="http://www.uoften.com">uoften.com</a> <a href="http://www.uoften.com">uoften.com</a> <a href="http://www.uoften.com">uoften.com</a> <a href="http://www.uoften.com">脚本之家</a></p>
</div>
</body>
</html>
最后
以上就是贪玩奇异果为你收集整理的jquery实现的超出屏幕时把固定层变为定位层的代码的全部内容,希望文章能够帮你解决jquery实现的超出屏幕时把固定层变为定位层的代码所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复