为什么此代码在Google Chrome中效果很好,但拒绝Safari(在Mac和iPhone上)



代码有助于将用户的屏幕平滑地降低到锚点,并从上方有必要的凹痕,因此固定在顶部的菜单不会掩盖它。

<script>
      $(document).ready(function(){
            $('a[href^="#"]').bind('click.smoothscroll',function (e) {
                e.preventDefault();
                var target = this.hash,
                $target = $(target);
                $calculus = ( $target.offset().top - 75 );
            $target_to = $calculus.toFixed();
                $('html, body').stop().animate({
                    'scrollTop': ($target_to+'px')
                }, 900, 'swing');
            });
        });
</script>

我找到了在所有平台上都起作用的最佳解决方案:

 $(document).ready(function(){
    $('a[href^="#"], *[data-href^="#"]').on('click', function(e){
        e.preventDefault();
        var t = 1000;
        var d = $(this).attr('data-href') ? $(this).attr('data-href') : $(this).attr('href');
        $('html,body').stop().animate({ scrollTop: $(d).offset().top - 80 }, t);
    });
});

最新更新