现在的淘宝啊,京东啊等很多平台都用到了一个技术,就是当页面下拉时,某个div会一直悬浮在页面顶端。具体代码如下
<p>jQuery实现页面滚动时层智能浮动定位</p>
<!DOCTYPE
html>
<html>
<head>
<meta http-equiv="Content-Type"
content="text/html; charset=utf-8"
/>
<title>jQuery实现页面滚动时层智能浮动定位</title>
<style
type="text/css">
*{ margin:0; padding:0;}
body { font:12px/1.8 Arial;
color:#666;height: 2000px}
.go-back{ text-align:center; border-top:1px dashed
#ccc; padding:10px; margin-top:20px; font-size:40px;}
.wrap{border:1px dashed
#ccc; background:#f8f8f8; padding:20px;}
.float{width:80px; padding:10px;
border:1px solid #ffecb0; background-color:#fffee0; -moz-box-shadow:1px 1px 2px
rgba(0,0,0,.2); -webkit-box-shadow:1px 1px 2px rgba(0,0,0,.2); box-shadow:1px
1px 2px rgba(0,0,0,.2); position:absolute; right:10%;
top:131px;}
</style>
<script type="text/javascript"
src="jquery.js"></script>
</head>
<body>
<div
class="wrap">
<div class="float"
id="float">哥赖这儿了</div>
</div>
<script
type="text/javascript">
$.fn.smartFloat = function() {
var position = function(element) {
var top = element.position().top, pos =
element.css("position");
$(window).scroll(function() {
var scrolls = $(this).scrollTop();
if (scrolls > top)
{
if (window.XMLHttpRequest) {
element.css({
position:
"fixed",
top:
0
});
} else
{
element.css({
top: scrolls
});
}
}else
{
element.css({
position: pos,
top: top
});
}
});
};
return $(this).each(function()
{
position($(this));
});
};
//绑定
$("#float").smartFloat();
</script>
</body>
</html>
原文:http://www.cnblogs.com/lightinblack/p/3633275.html