你想position: fixed, top: 0
之間切換和position: absolute, top: <the current scroll position>
,因爲用戶滾動經過#expand元素的「頂部」。下面的代碼使用一個(不太)全局變量來跟蹤這個開關應該發生的位置。如果你願意,你可以使用$("#expand").data(...)
或其他東西,但你明白了。
(function() {
var expandY = 0;
$("a[name=somelink]").click(function (e) {
e.preventDefault(); // Keep from following link.
expandY = $(window).scrollTop();
$("#expand").css({position: "fixed", top: 0});
});
$(window).scroll(function() {
var $expand = $("#expand");
if($expand.css("position") === "fixed") {
if($(window).scrollTop() > expandY) {
$expand.css({position: "absolute", top: expandY});
}
}
else {
if($(window).scrollTop() < expandY) {
$expand.css({position: "fixed", top: 0});
}
}
});
})();
這裏的小提琴:
http://jsfiddle.net/rkp5x/3
你有一個可以演示的要求有關係嗎? – malkassem
我試過 var position = $('#expand')[0] .scrollTop; \t \t \t \t \t \t如果(位置<200) \t \t \t { \t \t \t $( '#擴大')動畫({scrollTop的:200}, '慢'); \t \t \t} 我學習jQuery的,如果對不起我完全關閉。 – aVC
@malkassem我在我的本地機器上工作。我會盡快提出一個演示頁面 – aVC