【问题标题】:get anchor #id urls back with jquery使用 jquery 获取锚点 #id 网址
【发布时间】:2013-06-26 10:03:22
【问题描述】:

我有一个使用 jquery 平滑滚动到锚点的固定菜单,效果很好 - 但是当我使用脚本时,锚链接不再出现在 url 中。有什么想法吗?

网站是http://www.julianvanmil.com

听到我正在使用的代码;

<script>
jQuery(document).ready(function($) {

    $(".scroll").click(function(event){     
        event.preventDefault();
        $('html,body').animate({scrollTop:$(this.hash).offset().top}, 400);
    });
});
$(function() {
    var bar = $('.logo');
    var top = bar.css('top');
    $(window).scroll(function() {
        if($(this).scrollTop() > 700) {
            bar.stop().animate({'top' : '35px'}, 300);
        } else {
            bar.stop().animate({'top' : top}, 300);
        }
    });
});
</script>

谢谢

【问题讨论】:

标签: jquery html anchor


【解决方案1】:

尝试将哈希与 JS...

$(".scroll").click(function(event){     
    event.preventDefault();
    $('html,body').animate({scrollTop:$(this.hash).offset().top}, 400);
    window.location.hash = "hash";
});

【讨论】:

    【解决方案2】:

    event.preventDefault(); 阻止了默认操作,即将哈希附加到 URL,然后滚动。

    尝试在event.preventDefault(); 之后添加:location.hash = this.hash

    【讨论】:

      【解决方案3】:

      试试这个,它的工作原理:

      $('a[href^="#"]').click(function(event){     
          event.preventDefault();
          $('html,body').animate({scrollTop:$(this.hash).offset().top}, 800);
          var target_anchor = this.hash;
          setTimeout(function(){
              window.location.hash = target_anchor;
          }, 800);
      });
      

      【讨论】:

        猜你喜欢
        • 2011-04-02
        • 1970-01-01
        • 2021-02-04
        • 2012-01-25
        • 2015-10-07
        • 1970-01-01
        • 2014-10-10
        • 1970-01-01
        • 2017-06-20
        相关资源
        最近更新 更多