繁体   English   中英

如何跳过此js的边距顶部?

[英]How do I skip the margin top on this js?

我一直在努力解决这个问题; 如何在向下滚动时使.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" );        
}    
});

在float-div类中添加top:0可以轻松解决此问题。

.float-div {
position: absolute;
background: red;
top: 0;
}

更新的小提琴: http : //jsfiddle.net/7KSTs/

这是我的新答案,这是一个小提琴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" ); 
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM