我一直在尝试创建一个类似于mydomaine.com的侧边栏,它通过无限滚动动态创建一个粘性侧边栏。问题是当我的侧边栏生成时,旧的侧边栏不会粘在前一个容器的底部,所以它们都跳起来或者新的侧边栏没有附加到文档,这里是代码,我一直在在我周围绞尽脑汁:
(function($) {
var element = $('.follow-scroll'),
originalY = element.offset().top;
// Space between element and top of screen (when scrolling)
var topMargin = 20;
// Should probably be set in CSS; but here just for emphasis
element.css('position', 'relative');
$(window).on('scroll', function(event) {
var scrollTop = $(window).scrollTop();
element.stop(false, false).animate({
top: scrollTop < originalY
? 0
: scrollTop - originalY + topMargin
}, 300);