【问题标题】:How to make smoother scroll-to-top action如何使滚动到顶部的动作更流畅
【发布时间】:2019-04-04 10:35:53
【问题描述】:

当我调用滚动到顶部操作时,我的代码会从慢到快移动,有没有更好的解决方案来修复平滑度?

我的代码:

(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;
});

【问题讨论】:

标签: javascript jquery


【解决方案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;
});

谢谢

【讨论】:

  • 当您向下滚动图标时甚至不会显示此内容
  • 请查看更新
  • 不工作开始慢然后开始加速到顶部
  • 这两个条件只需将50改为150
  • 这将和我之前不工作之前的一样
猜你喜欢
  • 1970-01-01
  • 2017-12-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多