我写了这两个简单的滚动函数
$(".scrollTo").click(function() {
var t = $(this).attr("href");
return $("html, body").animate({
scrollTop: $(t).offset().top - 100
}, {
duration: 1e3,
easing: "easeInOutQuint"
})
});
$("a[href='#top']").click(function() {
return $("html, body").animate({
scrollTop: 0
}, {
duration: 1e3,
easing: "easeInOutQuint"
})
});
但是除非我删除第一个,否则第二个函数永远不会工作。我是 js 新手,我不明白为什么这两个函数不能同时工作?我尝试了几种语法技巧,但不幸的是它们都不起作用
最佳答案
删除return
语句并防止 anchor 的默认行为。试试这个。
$(".scrollTo").click(function() {
var t = $(this).attr("href");
$("html, body").animate({
scrollTop: $(t).offset().top - 100
}, {
duration: 1e3,
easing: "easeInOutQuint"
});
});
$("a[href='#top']").click(function(e) {
e.preventDefault();
$("html, body").animate({
scrollTop: 0
}, {
duration: 1e3,
easing: "easeInOutQuint"
});
});
关于javascript - 为什么这两个 jquery 函数不能一起工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30669750/