1. 程式人生 > >js頁面滾動浮動層智慧定位(jQuery) 類似淘寶工具條

js頁面滾動浮動層智慧定位(jQuery) 類似淘寶工具條

HTML程式碼:
<div class="float" id="float"> 我是個靦腆羞澀的浮動層<img src="http://web2.qq.com/style/face/6.gif" title="含羞">,你可以輕輕地<a href="http://www.zhangxinxu.com/php/regist.php">點選我</a>(去註冊頁)。</div>JS程式碼:
$.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: "absolute",
top: top
});
}
});
};
return $(this).each(function() {
position($(this));
});
};

//繫結
$("#float").smartFloat(); <style>.demo{height:1500px;}.float{width:60px; 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>