如何在Jquery中创建异常

时间:2014-04-02 15:25:36

标签: javascript jquery

首先:我是Jquery新手: - )

我已将此“平滑滚动”效果实施到我网站上的内部链接,因此当我在#(例如href)中使用href="#footer"时,会产生效果:

$(document).ready(function() {
    $('a[href^="#"]').on('click',function (e) {
        e.preventDefault();
        console.log("Denne funktion fanger #");
        console.log(event);

        var target = this.hash,
        $target = $(target);

        $('html, body').stop().animate({
            'scrollTop': $target.offset().top
        }, 800, 'swing', function () {
            window.location.hash = target;
        });
    });
});

我正在使用此脚本在页面之间淡入淡出,因此每次单击外部链接时,它都会对该给定页面产生淡入淡出效果。

$(document).ready(function() {
    $("body").css("display", "none");
});

$(document).ready(function() {
    $("body").css("display", "none");
    $("body").fadeIn(1000);
});

$(document).ready(function() {
    $("body").css("display", "none");

    $("body").fadeIn(1000);

    $("a").click(function(event){
        event.preventDefault();
        linkLocation = this.href;
        $("body").fadeOut(1000, redirectPage);      
    });

    function redirectPage() {
        window.location = linkLocation;
    }
});

但是我不希望在使用平滑滚动效果的内部链接上出现淡入淡出效果。如何在上面的脚本中编写异常,以便忽略我使用#的淡入淡出效果脚本?

谢谢!

2 个答案:

答案 0 :(得分:1)

您可以使用not()和#34; startswith"。

$("a:not([href^='#'])").click(function(event){
        event.preventDefault();
        linkLocation = this.href;
        $("body").fadeOut(1000, redirectPage);      
    });

答案 1 :(得分:0)

您可以过滤掉元素。

$("a").not("[href^='#']").on("click", function(event){
    event.preventDefault();
    linkLocation = this.href;
    $("body").fadeOut(1000, redirectPage);      
});