我需要一个侧边栏,当用户在页面上滚动时,该边栏会与固定位置保持一致。我遇到了很多解决方案,都非常笨重,复杂或太长。我需要它简单而高效。我想它并使它:包含的粘性边栏
var length = $('#container').height() - $('#stick').offset().top - parseFloat($('#stick').css('marginTop').replace(/auto/, 0));
$(window).scroll(function() {
var scroll = $(this).scrollTop();
if (scroll < $('#container').offset().top) {
$('#stick').removeAttr("style");
}
else if (scroll > length) {
$('#stick').css('position', 'absolute');
}
else {
$('#stick').css({"position":"fixed", "top":"0", "right":"0"});
}
});
我的Remy Sharp screencast的帮助和waypointarts blog post
当#container的获得在视口的末端#stick停止滚动做这个(“固定”的位置被移除),问题是它消失了,并没有保持在该位置的绝对位置,这种行为会干扰用户。
如何让#stick侧栏完全定位在#container的底部而不是消失?你是否也认为我的代码可以完善?
我是业余的,并开始使用jquery1个月现在,所以你会发现很多在这里的错误...
谢谢。
当足够的浏览器支持它时,可以使用['position:sticky'](https://developer.mozilla.org/en-US/docs/Web/CSS/position#Sticky_positioning) – Oriol 2014-10-16 21:27:33
哇太棒了。我不知道这个位置可用。感谢您指出它。目前还不是一个可靠的选项:http://caniuse.com/#search=sticky – 2014-10-16 22:32:15