Mat*_*ell 0 javascript jquery z-index jquery-animate animate.css
在我的页面中,我有不同的菜单项,可以触发不同的相应图片.将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;
});
});
Run Code Online (Sandbox Code Playgroud)
问题是,z-index每个菜单项似乎只有一次工作.如果你最初点击#andrew,它会将pic1带到顶部,如果你点击#bugz它会将pic2带到顶部.但是,如果#andrew再次单击它会在.css(z-index)更改之前和之后为代码设置动画,但不会更改z-index以使pic1返回到顶部.
我是Javascript/JQuery的新手所以请原谅我,如果我遗漏了一些明显的东西
你的选择器是错误的:$('.pic2 .pic3 .pic4 .pic5')寻找的后代.pic2.
您可以使用逗号分隔这些类而不是空格,但使用该.siblings方法更简单:
$('.pic1').css('zindex',2).siblings().css('zindex',1);
Run Code Online (Sandbox Code Playgroud)