【发布时间】:2020-04-27 14:30:19
【问题描述】:
我正在使用带有自定义动画的 jQuery animate 函数来触发我网站上的平滑页面滚动。我在代码末尾使用 event.preventDefault() 函数调用,并使用此选项页面滚动,但它不会更新 URL。当我删除此函数调用时,它会更新 URL,但事先页面会闪烁然后滚动。我也尝试过不使用自定义动画,但它的行为仍然相同。有解决办法吗?
$(function () {
$.extend($.easing,
{
easeInOutExpo: function (x, t, b, c, d) {
t /= d / 2;
if (t < 1) {
return c / 2 * Math.pow(2, 10 * (t - 1)) + b;
}
t--;
return c / 2 * (-Math.pow(2, -10 * t) + 2) + b;
}
}
);
$('a.page-scroll').on('click', function (event) {
var $anchor = $(this);
$('html, body').stop().animate({
scrollTop: $($anchor.attr('href')).offset().top
}, 1600, 'easeInOutExpo');
event.preventDefault();
});
});
【问题讨论】:
-
你有一些 html 来匹配你的 javascript 吗?
-
嗨,理查德,感谢您抽出宝贵时间。你特别需要什么?我的 HTML 文件中没有直接为这个函数指定任何内容。
-
只是一些您正在使用此代码的示例 html。 - 所以我可以尝试查看并复制它,然后可能会找到解决方案
-
只需克隆这个 repo:github.com/z0le12/stack-overflow
标签: javascript jquery scroll jquery-animate