在我的页面中,我有不同的菜单项触发不同的相应图片。将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-index
pic1 以返回顶部。
我是 Javascript/JQuery 的新手,所以如果我遗漏了一些明显的东西,请原谅我