页面“anchor"滚动链接不滚动到正确的位置,只有在移动版本

On-Page "anchor" scroll links don't scroll to the right place only on the mobile version

本文关键字:滚动 位置 版本 移动 anchor quot 页面 链接      更新时间:2023-09-26

我使用锚标记来滚动我的网站。

只有当我使用手机查看页面时(或使用较小的宽度打开浏览器),它才会滚动大约100px。

我在.js文件中改变了这一点:

  $.localScroll({
        offset:{left: 0, top:-100 }
    });

(现在可以在手机上正常工作,但不能在桌面浏览器上正常工作)

来自:

  $.localScroll({
        offset:{left: 0, top:-184 }
    });

(在手机上不行)

是什么导致了这个问题?最好的解决方案是什么?

可能的解决方案不知道你的代码或了解移动代码的不同之处:

var problemIfSmallerThanThis = 1200; // Your "problem"-width where the error appears
var docWidth = $(document).width();
if(docWidth > problemIfSmallerThanThis ){
$.localScroll({
        offset:{left: 0, top:-184 }
    });
}else{
$.localScroll({
        offset:{left: 0, top:-100 }
    });
}

这只是一个WORKAROUND