Javascript 跳绳/跳绳落锚

Javascript 跳绳/跳绳落锚,javascript,jquery,html,css,wordpress,Javascript,Jquery,Html,Css,Wordpress,我有一个下拉锚,可以从一整页视差图像底部的向下箭头链接到页面上的另一部分。抛锚: <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-positi

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

<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>
欢迎干草叉咕噜咕噜咕噜咕噜咕噜咕噜咕噜咕噜咕噜咕噜咕噜咕噜咕噜咕噜咕噜咕噜咕噜咕噜咕噜咕噜咕噜咕噜咕咕噜咕噜咕噜。Sriracha打乱了kickstarter的手指Stache布边倒过来。古希腊木棍、金福克班卓班米冥想陈词滥调3狼月单源咖啡病毒博客宝丽来弹出

单击箭头可以工作,但会平滑滚动到下拉锚定,它会滚动到正确的位置,然后无明显原因向下移动

下面是我用来实现平滑滚动的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>

$(文档).ready(函数(){
//添加平滑滚动到所有链接
$(“a.arrow”)。在('click',函数(事件){
//在覆盖默认行为之前,请确保this.hash具有值
如果(this.hash!==“”){
//防止默认锚点单击行为
event.preventDefault();
//存储散列
var hash=this.hash;
//使用jQuery的animate()方法添加平滑页面滚动
//可选数字(800)指定滚动到指定区域所需的毫秒数
$('html,body')。设置动画({
scrollTop:$(散列).offset().top-140
},800,函数(){
//完成滚动后,将哈希(#)添加到URL(默认单击行为)
window.location.hash=散列;
});
}//如果结束,则结束
});
});
您可以在此处看到正在发生的问题:

联合国网站:演示 PW查看站点:密码


有什么想法吗

您的问题是由以下原因引起的:

    // Add hash (#) to URL when done scrolling (default click behavior)
    window.location.hash = hash;
更改
位置时
这种方式会触发浏览器的相应反应

建议将其替换为以下内容:

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

这很有效。非常感谢。我还在学习Javascript。你能解释一下你提供的代码片段是做什么的吗?我很想知道为什么它会起作用。请考虑下面的文章: