你想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