jQuery實現(xiàn)頁面滾動時層智能浮動定位實例探討
更新時間:2013年03月29日 18:04:35 作者:
在博客或者微博上面也會見到這個效果,所以客戶就也想要這樣的效果了,接下來為大家詳細介紹下浮動定位實現(xiàn)的過程,感興趣的朋友可以參考下哈
各位兄弟可能碰到定位的問題,特別是在博客或者微博上面也會見到這個效果,于是產品人員在策劃的時候就會要人家那種效果,,,而苦逼的我們需要去實現(xiàn),實現(xiàn)實現(xiàn)。。。。。沒辦法,誰讓我們是攻城師呢,攻吧:
效果圖如下,滾動條下拉的時候黑色的塊TOP為0;固定顯示:

代碼如下:
<!DOCTYPE html>
<html >
<head>
<title>jQuery實現(xiàn)頁面滾動時層智能浮動定位</title>
<meta name="description" content="" />
<script type="text/javascript" src="http://jt.875.cn/js/jquery.js"></script>
<style type="text/css">
*{ margin:0; padding:0;}
.top{height:100px; background:#ccc;text-align:center; line-height:100px; font-size:40px;}
body { font:12px/1.8 Arial; color:#666; height:2000px;}
.float{width:200px; height:200px; border:1px solid #ffecb0; background-color:#000;position:absolute; right:10px; top:150px;}
</style>
</head>
<body>
<div class="top">導航啊導航啊導航</div>
<div class="float" id="float"></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>
</div>
</body>
</html>
效果圖如下,滾動條下拉的時候黑色的塊TOP為0;固定顯示:

代碼如下:
復制代碼 代碼如下:
<!DOCTYPE html>
<html >
<head>
<title>jQuery實現(xiàn)頁面滾動時層智能浮動定位</title>
<meta name="description" content="" />
<script type="text/javascript" src="http://jt.875.cn/js/jquery.js"></script>
<style type="text/css">
*{ margin:0; padding:0;}
.top{height:100px; background:#ccc;text-align:center; line-height:100px; font-size:40px;}
body { font:12px/1.8 Arial; color:#666; height:2000px;}
.float{width:200px; height:200px; border:1px solid #ffecb0; background-color:#000;position:absolute; right:10px; top:150px;}
</style>
</head>
<body>
<div class="top">導航啊導航啊導航</div>
<div class="float" id="float"></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>
</div>
</body>
</html>
相關文章
jQuery數(shù)據(jù)檢索中根據(jù)關鍵字快速定位GridView指定行的實現(xiàn)方法
這篇文章主要介紹了jQuery數(shù)據(jù)檢索中根據(jù)關鍵字快速定位GridView指定行的實現(xiàn)方法,涉及jQuery結合asp.net操作頁面元素及樣式的相關技巧,需要的朋友可以參考下2016-06-06
jQuery支持動態(tài)參數(shù)將函數(shù)綁定到事件上的方法
這篇文章主要介紹了jQuery支持動態(tài)參數(shù)將函數(shù)綁定到事件上的方法,實例分析了兩種支持動態(tài)參數(shù)的函數(shù)綁定技巧,需要的朋友可以參考下2015-03-03
jQuery實現(xiàn)動態(tài)加載select下拉列表項功能示例
這篇文章主要介紹了jQuery實現(xiàn)動態(tài)加載select下拉列表項功能,結合實例形式較為詳細的分析了jQuery動態(tài)加載select下拉列表項的步驟與相關操作技巧,需要的朋友可以參考下2018-05-05

