javascript - 跳过/跳跃抛 anchor

标签 javascript jquery html css wordpress

我有一个下拉 anchor ,它从整页视差图像底部的向下箭头链接到页面上的另一个部分。抛 anchor :

<section id="first" class="big-image parallax lander" style="background-image: url(&quot;https://wagstaffsandbx.wpengine.com/wp-content/uploads/2017/06/Homepage-Photo-1.jpg&quot;); background-position: 50% -7.2px;" data-stellar-background-ratio="0.2" data-stellar-vertical-offset="50">

    <a href="#welcome" class="arrow"><img class="down-arrow" src="https://wagstaffsandbx.wpengine.com/wp-content/uploads/2017/06/down-arrow.png" alt="down-arrow"></a>

</section>

去向:

<section id="welcome" class="text-block"><h1>welcome</h1><p>Pitchfork mumblecore stumptown, intelligentsia wolf put a bird on it man bun wayfarers organic actually sartorial. Sriracha disrupt kickstarter fingerstache selvage pour-over. Paleo ugh lumbersexual, kinfolk banjo banh mi meditation cliche 3 wolf moon single-origin coffee viral blog polaroid pop-up.</p>
</section>

单击箭头有效,但会平滑滚动到放置 anchor ,它滚动到正确的位置,然后无缘无故向下移动。

这是我用来实现平滑滚动的 jQuery。你会看到有一个 140px 的导航偏移量:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
  // Add smooth scrolling to all links
  $("a.arrow").on('click', function(event) {

    // Make sure this.hash has a value before overriding default behavior
    if (this.hash !== "") {
      // Prevent default anchor click behavior
      event.preventDefault();

      // Store hash
      var hash = this.hash;

      // Using jQuery's animate() method to add smooth page scroll
      // The optional number (800) specifies the number of milliseconds it takes to scroll to the specified area
      $('html, body').animate({
        scrollTop: $(hash).offset().top -140
      }, 800, function(){

        // Add hash (#) to URL when done scrolling (default click behavior)
        window.location.hash = hash;
      });
    } // End if
  });
});
</script>

您可以在此处查看实际问题:https://wagstaffsandbx.wpengine.com/test-stellar/

联合国看网站:demo PW看网站:密码

有什么想法吗?

最佳答案

你的问题是这样引起的:

    // Add hash (#) to URL when done scrolling (default click behavior)
    window.location.hash = hash;

以这种方式改变location会触发浏览器的相应 react 。

建议用以下内容替换它:

if (typeof (history.pushState) !== 'undefined') {
    var obj = {title: $('head title').text(), URL: hash};
    history.pushState(obj, obj.title, obj.URL);
}

关于javascript - 跳过/跳跃抛 anchor ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44465720/

相关文章:

javascript - 链接重定向到错误页面

javascript - js。如何隐藏不是 onclick 的选项?需要像显示 :none

javascript - 如何在不同的 id 上更改具有相同输出的显示 css 切换开关?

javascript - 单击时将元素设置为隐藏

html - 禁止游标?

javascript - 当 JS 压缩器重命名变量时,如何通过 eval() 设置变量?

javascript - 说 nodeJS 建立在 V8 引擎上是什么意思?

html - 元视口(viewport)标签似乎被完全忽略或没有效果

javascript - 仅应用jquery mobile 页面的一部分?

javascript - 在 React 组件中动态创建元素