简体   繁体   English

如何为平滑滚动添加缓动?

[英]How to add easing to smooth scrolling?

I have this script where I'd like to add some easing to make the whole scrolling effect a little smoother:我有这个脚本,我想在其中添加一些缓动以使整个滚动效果更平滑一点:

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

So I picked this line and added 'easeOutExpo' , but unfortunately it didn't work.所以我选择了这条线并添加了'easeOutExpo' ,但不幸的是它没有用。 Any idea what I'm doing wrong?知道我做错了什么吗?

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

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

Please include the line mentioned below after jQuery script.请在 jQuery 脚本之后包含下面提到的行。 this worked for me.这对我有用。

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

OR: Download custom jQueryUi from here或者:从这里下载自定义 jQueryUi

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM