gpt4 book ai didi

jquery - 当我有一个固定的标题时,如何给粘性条顶部偏移

转载 作者:太空宇宙 更新时间:2023-11-04 06:20:24 26 4
gpt4 key购买 nike

我找到了一个 codepen 示例,我正在寻找唯一的问题是如何为粘性侧边栏提供 70px 的顶部偏移

  var stickySidebar = $('.sticky');

if (stickySidebar.length > 0) {
var stickyHeight = stickySidebar.height(),
sidebarTop = stickySidebar.offset().top;
}

// on scroll move the sidebar
$(window).scroll(function () {
if (stickySidebar.length > 0) {
var scrollTop = $(window).scrollTop();

if (sidebarTop < scrollTop) {
stickySidebar.css('top', scrollTop - sidebarTop);

// stop the sticky sidebar at the footer to avoid overlapping
var sidebarBottom = stickySidebar.offset().top + stickyHeight,
stickyStop = $('.main-content').offset().top + $('.main-content').height();
if (stickyStop < sidebarBottom) {
var stopPosition = $('.main-content').height() - stickyHeight;
stickySidebar.css('top', stopPosition);
}
}
else {
stickySidebar.css('top', '0');
}
}
});

$(window).resize(function () {
if (stickySidebar.length > 0) {
stickyHeight = stickySidebar.height();
}
});

https://codepen.io/cheryllaird/pen/Ddcxr/

最佳答案

我在顶部添加了 70px 偏移量,当粘条到达顶部时,这里是更新的代码段。

https://codepen.io/anon/pen/jRMXaG

关于jquery - 当我有一个固定的标题时,如何给粘性条顶部偏移,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55569261/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com