gpt4 book ai didi

jquery - 如何将 'easing' 添加到 animate/scrolltop

转载 作者:行者123 更新时间:2023-12-03 22:00:20 26 4
gpt4 key购买 nike

我正在使用以下函数创建滚动动画来锚定链接:

$('a').click(function(){
$('html, body').animate(
{scrollTop: $( $.attr(this, 'href') ).offset().top},
500 );
return false;
});

我想添加缓动。但是,当我在“500”之后添加“easing”时,它会破坏脚本:

$('a').click(function(){
$('html, body').animate(
{scrollTop: $( $.attr(this, 'href') ).offset().top},
500, easing );
return false;
});

知道我做错了什么吗?

最佳答案

首先您需要包含 jQuery.UI 脚本,然后您的代码应如下所示:

$('a').click(function(){
var top = $('body').find($(this).attr('href')).offset().top;
$('html, body').animate({
scrollTop: top
},500, 'easeOutExpo');

return false;
});

供您引用:

宽松

The remaining parameter of .animate() is a string naming an easing function to use. An easing function specifies the speed at which the animation progresses at different points within the animation. The only easing implementations in the jQuery library are the default, called swing, and one that progresses at a constant pace, called linear. More easing functions are available with the use of plug-ins, most notably the jQuery UI suite.

<小时/>

为什么你的代码不起作用:

  1. 因为您使用了动画方法范围内的 this 并且对 bodyhtml 对象的引用。
  2. 因为easing不是一种方法。是一个字符串类型的动画属性所以你需要将其写为字符串,例如:'easeOutExpo'"easeOutExpo"

关于jquery - 如何将 'easing' 添加到 animate/scrolltop,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16680045/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com