【问题标题】:My Sticky Header Cover Content with the Smooth Scrolling Effect带有平滑滚动效果的粘滞标题封面内容
【发布时间】:2019-08-22 15:19:37
【问题描述】:

我添加了一个粘性标题和平滑滚动效果,但我不知道如何固定位置,因此它与标题大小有关。我尝试过的东西完全禁用了粘性标题。

虽然我是新手,但我尝试使用几种不同的技术,我自己可能很难做到。

<div id="container">
  <section id="sectionHome">
    <!--Header and Logo-->
    <header id="myHeader">
      <logo>
        <img src="Pictures/Marvel-logo-880x660.crop.png">
      </logo>
    </header>

    <!--The Top Navigation Menu-->
    <div id="mainNav">
      <ul>
        <li class="current"><a href="#">Home</a></li>
        <li><a href="#firstArticle">Characters</a></li>
        <li><a href="#secondArticle">Movies</a></li>
        <li><a href="#thirdArticle">More Info</a></li>
      </ul>
    </div>
  </section>
//Smooth Scrolling in Main Nav
$(document).ready(function() {
  $('#mainNav li a').click(function(e) {

    var targetHref = $(this).attr('href');

    $('html, body').animate({
      scrollTop: $(targetHref).offset().top
    }, 1000);

    e.preventDefault();
  });
});


// Sticky Header
window.onscroll = function() {
  myFunction()
}; // When the user scrolls the page
var header = document.getElementById("sectionHome"); // Get the header and top nav
var sticky = header.offsetTop; // Get the offset position of the navbar
function myFunction() { // Add the sticky class to the header when you reach its scroll position. Remove "sticky" when you leave the scroll position
  if (window.pageYOffset > sticky) {
    header.classList.add("sticky");
  } else {
    header.classList.remove("sticky");
  }
}

这是我尝试过的一件事,但它禁用了我的粘性标题:

$(document).ready(function() {

  var headerHeight = $('header').outerHeight(); // Target your header navigation here

  $('#main-nav li a').click(function(e) {

    var targetHref   = $(this).attr('href');

    $('html, body').animate({
        scrollTop: $(targetHref).offset().top - headerHeight // Add it to the calculation here
    }, 1000);

    e.preventDefault();
  });
});

我想我可以为总标题大小设置一个值并以这种方式定位它,尽管它禁用了粘性标题。我该如何正确地做到这一点?

这是我的网页: http://www.student.city.ac.uk/~aczc972

最好的问候, 丹妮尔

【问题讨论】:

  • 添加了另一种滚动到顶部的方式

标签: javascript header sticky scroll-position


【解决方案1】:

我已经添加了一个沙盒,如何使用 jQuery 来完成它,一般来说,我的站点中只有一个添加是我正在检查目标是什么,例如滚动到首页,如果是,我正在为它运行指定的代码:

if (targetHref === "#") {
  $("html, body").animate(
    { scrollTop: 0 },
    "1000"
  );
} else {
  $("html, body").animate({scrollTop: $(targetHref).offset().top},1000);
}

codesandbox.io/s/81l87w26w0

减去标题高度滚动以防止标题覆盖内容

scrollTop: $(targetHref).offset().top - 180"

【讨论】:

  • 所以当我添加代码时会发生什么,它禁用了粘性标题和我的主要幻灯片出于某种原因。你的代码工作得很好,尽管当添加到我的代码中时会发生同样的事情。我想知道我在这里缺少什么。这是网站:student.city.ac.uk/~aczc972顺便谢谢!太棒了!
  • 欢迎您,点击home Uncaught Error: Syntax error, unrecognized expression: # (kevinleary.net/jquery-syntax-error-unrecognized-expression) 时,我可以在您的页面上看到错误,请验证这是否不是导致问题的原因
  • 好吧,这来自$(targetHref).offset().top,你不能得到像$(#)这样的元素
  • 请更新上面的sn-p,以便我们查看
  • 嗨!我已经更新了它,所以它里面有你的代码。我必须更改#吗?
【解决方案2】:

您还可以滚动到页面顶部,例如:

id="home" 添加到正文并更改href in:

&lt;li class="current"&gt;&lt;a href="#"&gt;Home&lt;/a&gt;&lt;/li&gt;

home

&lt;li class="current"&gt;&lt;a href="#home"&gt;Home&lt;/a&gt;&lt;/li&gt;

应该使用您的代码

【讨论】:

    【解决方案3】:

    这不一定是最好的方法,但它是一个示例,旨在说明如何做到这一点。你不需要 jQuery 来实现这个效果,所以值得一试。

    下面的代码修复了标头,并调整了主包装器的填充以适应标头的大小。然后它在类section-link 的元素上设置侦听器。对于这些元素,点击事件将滚动到 id 对应于被点击元素的 data-section 属性的元素。

    你可以忽略这个的 css,它只是为了说明它是如何工作的。

    const padForHeader = () => {
      // find out how high the header element is
      const headerHeight = document.getElementById('top-header').clientHeight;
    
      // how much extra padding would we like?
      const headerPadding = 20;
    
      // add the two together to see how much padding we need to add
      const headerBufferSize = headerHeight + headerPadding;
    
      // set the marginTop property so that the header doesn't overlay content
      document.querySelector('.wrapper').style.marginTop = `${headerBufferSize}px`;
    };
    
    padForHeader();
    
    // when the window resizes, re-pad for the header
    window.addEventListener('resize', padForHeader);
    
    document
      .querySelectorAll('.section-link')
      .forEach(element => {
    
        // we want to scroll 'smoothly' to the element
        const scrollOptions = {
          behavior: "smooth"
        };
    
        // we can read the data attribute to find the matching element's id
        const elementIdToScrollTo = element.dataset.section;
    
        // we can use the id we found to get the corresponding element
        const elementToScrollTo = document.getElementById(elementIdToScrollTo);
    
        // we can set the onclick property to scroll to the element we found
        element.onclick = () => elementToScrollTo.scrollIntoView(scrollOptions);
      });
    .header {
      background-color: red;
      border: solid 2px grey;
      border-radius: 5px;
      font-family: arial;
      margin: 0 auto;
      position: fixed;
      top: 0;
      left: 0;
      right: 0;
      width: 97%;
    }
    
    .header>ul {
      list-style: none;
      color: rgba(250, 250, 240, 0.8);
    }
    
    .header>ul>li {
      cursor: pointer;
      display: inline-block;
      position: relative;
      top: 0px;
      transition: all 0.3s ease;
      width: 200px;
    }
    
    .header>ul>li:hover {
      color: rgba(250, 250, 240, 1);
      top: -1px;
    }
    
    .section {
      background-color: rgba(20, 20, 30, 0.2);
      height: 80vh;
      border-bottom: solid 2px black;
      padding-top: 50px;
    }
    <div class="wrapper">
      <div id="top-header" class="header sticky">
        <ul>
          <li class="section-link" data-section="1">Item 1</li>
          <li class="section-link" data-section="2">Item 2</li>
          <li class="section-link" data-section="hello-world">Item hello world</li>
        </ul>
      </div>
    
      <div id="1" class="section">
        Test 1
      </div>
      <div id="2" class="section">
        Test 2
      </div>
      <div id="hello-world" class="section">
        Test 3
      </div>
    </div>

    【讨论】:

    • 谢谢!如果这不起作用,看起来像一个替代解决方案!
    • @DanielleJ 可能以这种方式重写它会使事情更容易使用 - 至少要考虑一些事情
    猜你喜欢
    • 2018-05-16
    • 1970-01-01
    • 2017-02-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多