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

代碼如下:
<!DOCTYPE html>
<html >
<head>
<title>jQuery實(shí)現(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">導(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>
效果圖如下,滾動條下拉的時候黑色的塊TOP為0;固定顯示:

代碼如下:
復(fù)制代碼 代碼如下:
<!DOCTYPE html>
<html >
<head>
<title>jQuery實(shí)現(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">導(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)文章
jQuery實(shí)現(xiàn)簡單的手風(fēng)琴效果
這篇文章主要為大家詳細(xì)介紹了jQuery實(shí)現(xiàn)簡單的手風(fēng)琴效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-06-06jQuery遍歷DOM的父級元素、子級元素和同級元素的方法總結(jié)
借助jQuery我們可以輕松地堆DOM元素進(jìn)行向上、向下遍歷以及同級的遍歷,本文我們即來整理jQuery遍歷DOM的父級元素、子級元素和同級元素的方法總結(jié):2016-07-07jquery select 設(shè)置默認(rèn)選中的示例代碼
本篇文章主要是對jquery select 設(shè)置默認(rèn)選中的示例代碼進(jìn)行了介紹,需要的朋友可以過來參考下,希望對大家有所幫助2014-02-02瘋狂Jquery第一天(Jquery學(xué)習(xí)筆記)
之前一直學(xué)習(xí),現(xiàn)在終于有時間來整理一下文檔了。以下文章都是自己學(xué)習(xí)Jquery 的筆記,希望能留下痕跡,也希望能幫助到您2012-05-05