【问题标题】:How to add easing to smooth scrolling?如何为平滑滚动添加缓动?
【发布时间】:2020-05-13 20:44:17
【问题描述】:

我有这个脚本,我想在其中添加一些缓动以使整个滚动效果更平滑一点:

$('a[href*="#"]')
  .not('[href="#"]')
  .not('[href="#0"]')
  .click(function(event) {
    if (
      location.pathname.replace(/^\//, '') == this.pathname.replace(/^\//, '') 
      && 
      location.hostname == this.hostname
    ) {
      var target = $(this.hash);
      target = target.length ? target : $('[name=' + this.hash.slice(1) + ']');
      if (target.length) {
        event.preventDefault();
        $('html, body').animate({
          scrollTop: target.offset().top}, 1000,
          function() {
          var $target = $(target);
          $target.focus();
          if ($target.is(":focus")) {
            return false;
          } else {
            $target.attr('tabindex','-1');
            $target.focus(); // Set focus again
          };
        });
      }
    }
  });

所以我选择了这一行并添加了'easeOutExpo',但不幸的是它不起作用。知道我做错了什么吗?

scrollTop: target.offset().top}, 1000, 'easeOutExpo',

https://jsfiddle.net/3gb4s2af/1/

【问题讨论】:

    标签: javascript jquery smooth-scrolling


    【解决方案1】:

    请在 jQuery 脚本之后加入下面提到的行。 这对我有用。

    <script src='http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.5/jquery-ui.min.js'>
    

    或者: 从here下载自定义jQueryUi

    【讨论】:

    • 我已经包含了 jQuery 和 jQueryUI。还是不行
    • rijn251 我检查了您发送的代码,显然代码在动画部分之前中断,有关更多信息,如果您尝试控制台记录它,则无法克隆“目标”元素
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-05
    • 2013-01-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多