如何在达到宽度<480像素时设置平滑滚动偏移

时间:2017-06-07 14:08:08

标签: javascript jquery html css jquery-ui

$(document).ready(function () {
    $("a.smooth-scroll").click(function (event) {
        event.preventDefault();
        var section = $(this).attr("href"); //get or return section id #about etc

        var section = $(this).attr("href");
        $("html,body").animate({
            scrollTop: $(section).offset().top - 64
        }, 1250, "easeInOutExpo");
    });


});

这是平滑滚动的代码。它在桌面屏幕上正常工作但是当谈到移动设备时,我需要设置offset()。top-64到offset()。top-50。怎么做?我尝试使用调整大小功能,但无法正常工作。

1 个答案:

答案 0 :(得分:1)

尝试检查当前窗口大小,并根据您的宽度设置值50或64.

$(document).ready(function () {
$("a.smooth-scroll").click(function (event) {
    event.preventDefault();
    var section = $(this).attr("href"); //get or return section id #about etc

    var section = $(this).attr("href");
    var value = ($(window).width() < 480) ? 50 : 64);
    $("html,body").animate({
        scrollTop: $(section).offset().top - value
    }, 1250, "easeInOutExpo");
});