【问题标题】:Smooth javascript scrolling with a fixed header带有固定标题的平滑 javascript 滚动
【发布时间】:2022-01-11 23:16:51
【问题描述】:

美好的一天。在网站上实现了 javascript 的平滑滚动。一切正常,页面本身严格滚动到所需部分的开头。但是由于我有一个固定的标题,但它占用了部分审查。问题是如何把这个header的高度加到滚动条上

js

  const anchors = document.querySelectorAll('.scroll-href')

  anchors.forEach(item => {
    item.addEventListener('click', (e) => {
      e.preventDefault()

      const blockID = item.dataset.scroll

      document.getElementById(blockID).scrollIntoView({
        behavior: 'smooth',
        block: 'start'
      })

    })
  })

hmtl

<header class="header">
 Some content
</header>

<a href="#" data-scroll="reviews-section">Scroll to reviews</a>
<a href="#" data-scroll="services-section">Scroll to services</a>
<a href="#" data-scroll="cases-section">Scroll to cases</a>
<a href="#" data-scroll="footer-section">Scroll to footer</a>

<section class="reviews" id="reviews-section">Some content</section>
<section class="services" id="services-section">Some content</section>
<section class="reviews" id="cases-section">Some content</section>
<section class="reviews" id="footer-section">Some content</section>

css

.header
  padding: 10px 0px
  position: fixed
  top: 0
  left: 0
  width: 100%
  background-color: #050505
  z-index: 10

链接到站点https://mpleader.pro/(移动设备上可能存在的错误)

不滚动的部分视图

带滚动的剖面视图

【问题讨论】:

标签: javascript html css sass


【解决方案1】:

为您的部分设置scroll-margin-top

section{
 scroll-margin-top: 10px; /* normally that would be equal to your header's height */
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-05-31
    • 1970-01-01
    • 1970-01-01
    • 2019-09-26
    • 2016-10-11
    • 1970-01-01
    • 1970-01-01
    • 2017-02-14
    相关资源
    最近更新 更多