简体   繁体   English

在页脚滚动上保持固定

[英]On footer scroll keep it fixed

I have an issue i've been working on for a day or so, i'm making an online shop, where when you visit a specific item's page, you have a fixed [add to cart] button as a footer, and when i scroll to a specific point, it should become static, check this example here: https://www.w3schools.com/howto/tryit.asp?filename=tryhow_js_sticky_header我有一个问题,我已经研究了一天左右,我正在做一个网上商店,当你访问特定项目的页面时,你有一个固定的 [添加到购物车] 按钮作为页脚,当我滚动到特定点,它应该变成静态,请在此处查看此示例: https : //www.w3schools.com/howto/tryit.asp?filename=tryhow_js_sticky_header

the only difference is that i need it to be a header, not a footer.唯一的区别是我需要它是页眉,而不是页脚。

here's my jquery code:这是我的 jquery 代码:

const myFunction = () => {
let lastScrollTop = 0

$(window).scroll(() => {
  const footerTop = $('.wrapper-footer')?.offset()?.top || null
  const container = $('.wrapper-mobile-price')
  const containerHeight = wrapperMobilePrice.height()
  const bottomOfScreen = $(window).scrollTop() + $(window).innerHeight()
  const st = $(window).scrollTop()

  if (st > lastScrollTop) {
    if (bottomOfScreen > footerTop + (containerHeight / 2)) {
      container.css({position: 'static'})
    }
  } else {
    if (bottomOfScreen + containerHeight < footerTop) {
      container.css({position: 'fixed'})
    }
  }

  lastScrollTop = st
})

} }

please help if you have any solutions, thanks!如果有解决办法请帮忙,谢谢!

Have you tried using CSS sticky positioning?您是否尝试过使用 CSS 粘性定位? You can see an example here你可以在这里看到一个例子

You should specify in which position an element should be fixed.您应该指定元素应该固定在哪个位置。 When an element reaches that position it becomes fixed.当一个元素到达那个位置时,它就固定了。

div.sticky {
  position: -webkit-sticky; /* Safari */
  position: sticky;
  top: 500px;
}

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

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