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

时间:2015-02-18 14:25:34

标签: javascript jquery mobile scroll offset

我有一点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,因为菜单栏已隐藏。

怎么做?

1 个答案:

答案 0 :(得分:0)

您可以在此使用window.innerWidth。通常移动窗口不超过1024px。在我这样做的过程中,我将从800px开始。

示例:

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