Anchor tags not working in chrome when using #

前端 未结 9 2217
一个人的身影
一个人的身影 2020-12-01 15:50

Here is the code I am using on my page,

  • Sound
  • (in a menu which appears on

    9条回答
    •  一生所求
      2020-12-01 16:35

      The workaround posted didn't work for me, however after days of searching this finally worked like a charm so I figured it was worth sharing:

       $(function() {
             $('a[href*="#"]:not([href="#"])').click(function() {
               if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') && location.hostname == this.hostname) {
                 var target = $(this.hash);
                 target = target.length ? target : $('[name=' + this.hash.slice(1) +']');
                 if (target.length) {
                   $('html, body').animate({
                     scrollTop: target.offset().top
                   }, 1000);
                   return false;
                 }
               }
             });
           });
      

    提交回复
    热议问题