如何使滚动到顶部动作更流畅

时间:2019-04-04 10:35:53

标签: javascript jquery

当我调用“滚动到顶部动作”时,我的代码执行从慢到快的运动,是否有更好的解决方案来解决平滑问题?

我的代码:

(function () {
    var top_link = '';
    top_link = $(".top");
    var pos = top_link.offset();

    $(window).scroll(function () {

        if ($(this).scrollTop() > 150) {
            top_link.fadeIn();
        } else if ($(this).scrollTop() <= 150) {
            top_link.fadeOut();
        }
    });
})();

$(".top").click(function () {
    $("html, body").animate({
        scrollTop: 0
    }, 600);
    return false;
});

1 个答案:

答案 0 :(得分:1)

请更新您的代码:

(function () {
    var top_link = '';
    top_link = $(".top");
    var pos = top_link.offset();

    $(window).scroll(function () {

        if ($(this).scrollTop() > 50) {
            top_link.fadeIn('slow');
        } else if ($(this).scrollTop() <= 50) {
            top_link.fadeOut('slow');
        }
    });
})();

$(".top").click(function () {
    $("html, body").animate({
        scrollTop: 0
    }, 500);
    return false;
});

谢谢

相关问题