【问题标题】:Disable scroll offset on mobile devices在移动设备上禁用滚动偏移
【发布时间】:2015-02-18 14:25:33
【问题描述】:

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

怎么做?

【问题讨论】:

    标签: javascript jquery mobile scroll offset


    【解决方案1】:

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

    例子:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-04-14
      相关资源
      最近更新 更多