动画的jQuery条件

kev*_*vin 5 javascript jquery if-statement conditional-statements

我是 javascript 和 jQuery 的新手,所以这个问题可能看起来很愚蠢,但我找不到任何例子或文档。

我得到了这个功能,用于一些颜色动画滚动和滚动,效果很好:

$(".box_nav").hover(function(){
            jQuery(this).stop(true, false);
            $(this).animate({ backgroundColor: "#fff"}, 300 ); },
        function() {
            jQuery(this).stop(true, false);
            $(this).animate({ backgroundColor: "#000"}, 300 ); }
);
Run Code Online (Sandbox Code Playgroud)

事实上,如果我最近添加了一个也可以正常工作的样式表更改按钮:

$(".black-white").click(function(){
        $("link").attr("href", "<?php bloginfo("template_url"); ?>/css/styles-black-white.css");
        $(".wp-polls-loading").css({ display:"none"});
        return false;
    });

    $(".grey-white").click(function(){
        $("link").attr("href", "<?php bloginfo("template_url"); ?>/css/styles-grey-white.css");
        $(".wp-polls-loading").css({ display:"none"});
        return false;
    });
Run Code Online (Sandbox Code Playgroud)

关键是我想在我的翻转菜单上创建一个条件,以便我也可以切换它的颜色。

所以我尝试了一些这样的事情:

/////////////////////////////////////////////////////////////////////////////
    //Stylesheet change
    $(".black-white").click(function(){
        $("link").attr("href", "<?php bloginfo("template_url"); ?>/css/styles-black-white.css");
        $(".wp-polls-loading").css({ display:"none"});
        var tt = "black";
        return false;
    });

    $(".grey-white").click(function(){
        $("link").attr("href", "<?php bloginfo("template_url"); ?>/css/styles-grey-white.css");
        $(".wp-polls-loading").css({ display:"none"});
        var tt = "grey";
        return false;
    });

    /////////////////////////////////////////////////////////////////////////////
    //Over menu
    if (tt == "black"){
        $(".box_nav").hover(function(){
            jQuery(this).stop(true, false);
            $(this).animate({ backgroundColor: "#fff"}, 300 ); },
        function() {
            jQuery(this).stop(true, false);
            $(this).animate({ backgroundColor: "#000"}, 300 ); }
        );
    }else {
        $(".box_nav").hover(function(){
            jQuery(this).stop(true, false);
            $(this).animate({ backgroundColor: "#000"}, 300 ); },
        function() {
            jQuery(this).stop(true, false);
            $(this).animate({ backgroundColor: "#e2e2e2"}, 300 ); }
        );
    }
Run Code Online (Sandbox Code Playgroud)

但当然不去。唯一有点作用的是,如果我更改 if () 或其他任何内容中的“黑色”,它会在第二个翻转样式中表现良好。

任何的想法 ?

Gra*_*aza 2

您在本地声明变量“tt”,然后尝试通过事物的外观全局访问它。无法确定,因为我们无法看到代码的整个结构,但如果您进行更改:

var tt = ....
Run Code Online (Sandbox Code Playgroud)

window.tt = ....
Run Code Online (Sandbox Code Playgroud)

它可能会起作用。如果您可以将其范围限制在方法或闭包内,我不建议将其设置为全局变量,但执行上述操作至少可以证明这是否是问题所在。