繁体   English   中英

在移动设备上禁用滚动偏移

[英]Disable scroll offset on mobile devices

我有一些js问题。 这是我的代码

$(document).on("scroll", onScroll);

$('a[href^="#"]').on('click', function (e) {
    e.preventDefault();
    $(document).off("scroll");

    $('a').each(function () {
        $(this).removeClass('active');
    })
    $(this).addClass('active');

    var target = this.hash,
        menu = target;
    $target = $(target);
    $('html, body').stop().animate({
        'scrollTop': $target.offset().top - 53}, 1000, 'swing', function () {
        window.location.hash = target;
        $(document).on("scroll", onScroll);
    });
});

如您所见,我在目标上方设置了53px的偏移量。 53px是我固定的菜单栏的高度

我想在移动设备上将此偏移量设置为0,因为菜单栏是隐藏的。

怎么做?

您可以在此上使用window.innerWidth 通常,移动窗口不超过1024px。 在执行此操作时,我将从800px开始。

范例:

if( window.innerWidth > 801 ){
    // offset code to 53 on not mobile
}else{
    // offset code to 0 on mobile
} 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM