我一直在努力解决这个问题;向下滚动时如何让 .float-div 不离开顶部?我希望它在滚动时固定在窗口的顶部。如果你拿走 .top-entry 它将正常工作。但是如何在不删除 .top-entry 的情况下修复它呢?
http://jsfiddle.net/loktar/Kjc6k/2/
var $scrollingDiv = $(".float-div");
$(window).scroll(function(){
var y = $(this).scrollTop(),
$postEntry = $('.post-entry'),
maxY = $postEntry.offset().top + $postEntry.height(),
scrollHeight = $scrollingDiv.height();
if(y< maxY-scrollHeight ){
$scrollingDiv
.stop()
.animate({"marginTop": ($(window).scrollTop()) + "px"}, "slow" );
}
});
最佳答案
这是我的新答案,这是一个 fiddle http://jsfiddle.net/Kjc6k/48/ 希望这就是您要找的:)
var $scrollingDiv = $(".float-div");
$(window).scroll(function(){
var y = $(this).scrollTop(),
$topEntry = $('.top-entry').height();
if( y > $topEntry){
$scrollingDiv
.stop()
.animate({"margin-top": y + "px"}, "slow" );
}
else
$scrollingDiv
.stop()
.animate({"margin-top": "80px"}, "slow" );
});
关于javascript - 如何跳过此 js 的边距顶部?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19071750/