【问题标题】:JQuery Animate Z-Index?JQuery 动画 Z 索引?
【发布时间】:2013-01-15 18:52:44
【问题描述】:

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

【问题讨论】:

    标签: javascript jquery jquery-animate z-index animate.css


    【解决方案1】:

    您的选择器错误:$('.pic2 .pic3 .pic4 .pic5') 查找 .pic2 的后代。

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

    $('.pic1').css('zindex',2).siblings().css('zindex',1);
    

    【讨论】:

    • 小问题 - 一切正常,但在页面加载后第一次单击项目时,右门打开,然后闪烁回到中间并再次打开。关于为什么会这样的任何线索?它只发生在第一次点击页面时
    • 如果没有更完整的代码(包括 HTML 和 CSS)图片,真的不能说。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-05
    • 2016-09-21
    相关资源
    最近更新 更多