【发布时间】:2015-02-07 08:07:06
【问题描述】:
我以为我已经解决了这个问题,但事实证明我没有。基本上构建一个 tumblr 主题,我的代码中的某些内容与 jquery 动画滚动到顶部相冲突。我试过删除一些东西,因为我不完全确定它可能是什么,并且认为它对其他人来说可能真的很明显?
这是一个指向我的主题 http://minori-theme.tumblr.com/ 的链接和一个我正在关注的 jsfiddle,以便为平滑滚动设置动画 http://jsfiddle.net/YtJcL/1008/
我正在使用的代码如下,我只是使用标准的链接和 id,它们链接正常,因为它到达了正确的点,只是滚动不流畅。
$(document).ready(function() {
var hashTagActive = "";
$(".scroll").click(function (event) {
if(hashTagActive != this.hash) { //this will prevent if the user click several times the same link to freeze the scroll.
event.preventDefault();
//calculate destination place
var dest = 0;
if ($(this.hash).offset().top > $(document).height() - $(window).height()) {
dest = $(document).height() - $(window).height();
} else {
dest = $(this.hash).offset().top;
}
//go to destination
$('html,body').animate({
scrollTop: dest
}, 2000, 'swing');
hashTagActive = this.hash;
}
});
});
编辑:如果有人有更简单的选择,我愿意接受建议吗?
【问题讨论】:
-
你真的需要你的条件
if ($(this.hash).offset().top >吗?如果您只使用元素的 .offset().top 作为目标点,则 ScrollTop 可以正常工作 -
我没有得到您的问题,因为您的代码运行方式与您提供的链接类似,只是您将 timeInterval 设置为 2000 毫秒。滚动在两者中都很流畅 ..?
-
如果您唯一的问题是它不流畅,您可能正在寻找一个 jQuery UI 缓动:jqueryui.com/easing - 您想要哪一个? (单击它们并查看它们是如何工作的)...您需要包含 jQuery UI 才能使它们工作...
-
在小提琴中它工作正常,在我的主题中,链接在跳到顶部时工作,但没有动画发生。对不起,我不擅长 jQuery。我相信唯一的问题是它没有滚动,但我不明白为什么?
-
我将如何使用 jquery easing - 任何教程或示例,非常感谢!
标签: javascript jquery html css animation