0

在我的页面中,我有不同的菜单项触发不同的相应图片。将z-index相应的图片切换到顶部。这是代码:

$(document).ready(function(){

var doorOpen = false;

$("a[href=#andrew]").click(function() {

    if (doorOpen) { // set animation duration for door close, based on actually needed to animate the door closed or not
        var duration = 1500;
    } else {
        var duration = 0;
    }

    $("#rightdoor,#leftdoor").animate(
        {"marginLeft":"0px"},
        {duration:duration,
            complete:function() {
                $('.pic2 .pic3 .pic4 .pic5').css('zIndex', 1);  //puts wrong pics in back
                $('.pic1').css('zIndex', 2);  //brings right pic into view
                $('#rightdoor').animate({  //opens doors again
                 marginLeft: "150px",
                }, 1500);
                $('#leftdoor').animate({
                 marginLeft: "-150px",
                }, 1500);
            }
        }
    );

    doorOpen = true;


});

$("a[href=#bugz]").click(function() {

    if (doorOpen) { // set animation duration for door close, based on actually needed to animate the door closed or not
        var duration = 1500;
    } else {
        var duration = 0;
    }

    $("#rightdoor,#leftdoor").animate(
        {"marginLeft":"0px"},
        {duration:duration,
            complete:function() {
                $('.pic1 .pic3 .pic4 .pic5').css('zIndex', 1);  //puts wrong pics in back
                $('.pic2').css('zIndex', 2);  //brings right pic into view
                $('#rightdoor').animate({  //opens doors again
                 marginLeft: "150px",
                }, 1500);
                $('#leftdoor').animate({
                 marginLeft: "-150px",
                }, 1500);
            }
        }
    );

    doorOpen = true;    
});
});

问题是,z-index每个菜单项似乎只有一次。如果您最初单击#andrew,它将 pic1 带到顶部,如果您单击#bugz它将 pic2 带到顶部。但是,如果#andrew再次单击,它会在更改前后对代码进行动画处理,.css(z-index)但不会更改z-indexpic1 以返回顶部。

我是 Javascript/JQuery 的新手,所以如果我遗漏了一些明显的东西,请原谅我

4

1 回答 1

5

您的选择器错误:$('.pic2 .pic3 .pic4 .pic5')寻找.pic2.

您可以使用逗号而不是空格来分隔这些类,但使用该.siblings方法更简单:

$('.pic1').css('zindex',2).siblings().css('zindex',1);
于 2013-01-15T18:55:07.807 回答