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

代碼如下:
<!DOCTYPE html>
<html >
<head>
<title>jQuery實(shí)現(xiàn)頁面滾動(dòng)時(shí)層智能浮動(dòng)定位</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">導(dǎo)航啊導(dǎo)航啊導(dǎo)航</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>
效果圖如下,滾動(dòng)條下拉的時(shí)候黑色的塊TOP為0;固定顯示:

代碼如下:
復(fù)制代碼 代碼如下:
<!DOCTYPE html>
<html >
<head>
<title>jQuery實(shí)現(xiàn)頁面滾動(dòng)時(shí)層智能浮動(dòng)定位</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">導(dǎo)航啊導(dǎo)航啊導(dǎo)航</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>
相關(guān)文章
分享一個(gè)自己動(dòng)手寫的jQuery分頁插件
本文主要是將自己動(dòng)手些jquery分頁插件的思路和步驟分享給大家,本分頁插件功能很簡單,但是卻很實(shí)用,不想其他插件似的,功能一大堆。好了,廢話不多說,還是看正文吧2014-08-08
jQuery數(shù)據(jù)檢索中根據(jù)關(guān)鍵字快速定位GridView指定行的實(shí)現(xiàn)方法
這篇文章主要介紹了jQuery數(shù)據(jù)檢索中根據(jù)關(guān)鍵字快速定位GridView指定行的實(shí)現(xiàn)方法,涉及jQuery結(jié)合asp.net操作頁面元素及樣式的相關(guān)技巧,需要的朋友可以參考下2016-06-06
jQuery支持動(dòng)態(tài)參數(shù)將函數(shù)綁定到事件上的方法
這篇文章主要介紹了jQuery支持動(dòng)態(tài)參數(shù)將函數(shù)綁定到事件上的方法,實(shí)例分析了兩種支持動(dòng)態(tài)參數(shù)的函數(shù)綁定技巧,需要的朋友可以參考下2015-03-03
jQuery實(shí)現(xiàn)動(dòng)態(tài)加載select下拉列表項(xiàng)功能示例
這篇文章主要介紹了jQuery實(shí)現(xiàn)動(dòng)態(tài)加載select下拉列表項(xiàng)功能,結(jié)合實(shí)例形式較為詳細(xì)的分析了jQuery動(dòng)態(tài)加載select下拉列表項(xiàng)的步驟與相關(guān)操作技巧,需要的朋友可以參考下2018-05-05
jQuery對象和DOM對象的相互轉(zhuǎn)化實(shí)現(xiàn)代碼
jQuery對象就是通過jQuery包裝DOM對象后產(chǎn)生的對象。jQuery對象是jQuery獨(dú)有的,其可以使用jQuery里的方法,但是不能使用DOM的方法;例如$("#img").attr("src","test.jpg"); 這里的$("#img")就是jQuery對象;2010-03-03

