为什么这两个jquery函数不能一起工作?

时间:2015-06-05 14:59:34

标签: javascript jquery

我写了这两个简单的滚动函数

$(".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的新人,我不明白为什么这两个功能不能同时工作?我尝试了几种语法技巧,但遗憾的是它们都没有工作

1 个答案:

答案 0 :(得分:1)

删除return语句并阻止锚点的默认行为。试试这个。

$(".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"
    });
});