我是 jQuery 新手。我在同一页面上有两个功能:
- 平滑滚动到 ID
- 另一个在用户滚动设定距离后显示“返回顶部”元素。
这些功能独立运行,但是当我如下所示组合它们时,“返回顶部”功能不起作用。
我想我遗漏了一些明显的东西,需要一些帮助。 谢谢!
更新:这个 fiddle 显示了问题:
back to top jsfiddle 如果禁用平滑滚动 block ,返回顶部功能将起作用。
jQuery(document).ready(function(){
//smooth scrolling
$('a[href^="#"]').on('click',function (e) {
e.preventDefault();
var target = this.hash,
$target = $(target);
$('html, body').stop().animate({
'scrollTop': $target.offset().top -150}, 900, 'swing', function () {
window.location.hash = target;});
});
// Show or hide the back to top footer button
$(window).scroll(function() {
if ($(this).scrollTop() > 200) {
$('.go-top').fadeIn(200);
} else {
$('.go-top').fadeOut(200);
}
});
// Animate the scroll to top
$('.go-top').click(function(event) {
event.preventDefault();
$('html, body').animate({scrollTop: 0}, 900);
});
});
最佳答案
关于javascript - 滚动顶部 : Smooth scroll to ID interferes with back to top,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23409858/