单击时在div处切换z-index

时间:2015-03-26 15:50:32

标签: javascript jquery css

我试图遍历一个绝对定位且具有z索引的子代。当单击一个按钮时,我希望z-index增加,所以它几乎就像一个循环。所以在每次点击时,我希望当前可见的div具有较低的z索引,因此一次只能看到一个div。



$(function() {
    $('button').click(function(){
        //.three sholed take the place of .one and .two should jump up one step. i want this to be repeatable.
    });
});

body {
    margin:0;
    padding:0;
}
.holder {
    width: 100px;
    height:100px;
    border:1px solid #000000;
}

.holder div {
    width: 100px;
    height: 100px;
    position:absolute;
    top:0;
    left:0;
}

.one {
    background:red;
    z-index:1;
}
.two {
    background:green;
    z-index:2;
}
.three {
    background: blue;
    z-index:3;
}

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<div class="holder">
    <div class="one">1</div>
    <div class="two">2</div>
    <div class="three">3</div>
</div>
<button>click</button>
&#13;
&#13;
&#13;

4 个答案:

答案 0 :(得分:1)

只需使用.css:

$( “选择”)的CSS( “z索引”, “1”);

$(document).ready(function(){
    var start = 2;
    $('button').click(function(){
        $('.holder > div').eq(start).hide();
        start--;
        $('.holder > div').eq(start).show();
        if(start<0){start=2;}
    });
});
body {
    margin:0;
    padding:0;
}
.holder {
    width: 100px;
    height:100px;
    border:1px solid #000000;
}

.holder div {
    width: 100px;
    height: 100px;
    position:absolute;
    top:0;
    left:0;
}

.one {
    background:red;
    z-index:1;
}
.two {
    background:green;
    z-index:2;
}
.three {
    background: blue;
    z-index:3;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="holder">
    <div class="one">1</div>
    <div class="two">2</div>
    <div class="three">3</div>
</div>
<button>click</button>

答案 1 :(得分:1)

使用jquery将一个类添加到具有所需索引的元素/点击

$("button").click(function(){
  $(".current").removeClass("current").next().addClass("current")
});
body {
    margin:0;
    padding:0;
}
.holder {
    width: 100px;
    height:100px;
    border:1px solid #000000;
}

.holder div {
    width: 100px;
    height: 100px;
    position:absolute;
    top:0;
    left:0;
    z-index:0; /*init z-index*/
}
.holder div.current{z-index:1;} /*only this DIV will be visible*/
.one {
    background:red;
}
.two {
    background:green;
}
.three {
    background: blue;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="holder">
    <div class="one current">1</div>
    <div class="two">2</div>
    <div class="three">3</div>
</div>
<button>click</button>

如果你想循环它

$("button").click(function(){
  if($(".holder >div:last-child").hasClass("current")){
     $(".holder >div:last-child").removeClass("current");
     $(".holder >div:first-child").addClass("current");
  }else{
     $(".current").removeClass("current").next().addClass("current");
  }
});
body {
    margin:0;
    padding:0;
}
.holder {
    width: 100px;
    height:100px;
    border:1px solid #000000;
}

.holder div {
    width: 100px;
    height: 100px;
    position:absolute;
    top:0;
    left:0;
    z-index:0; /*init z-index*/
}
.holder div.current{z-index:1;} /*only this DIV will be visible*/
.one {
    background:red;
}
.two {
    background:green;
}
.three {
    background: blue;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="holder">
    <div class="one current">1</div>
    <div class="two">2</div>
    <div class="three">3</div>
</div>
<button>click</button>

答案 2 :(得分:0)

我做了类似这样的事情来伪造带有淡化动画的gif:

您只需在onclick处理程序上调整它,而不是setInterval

    function fakeGif(container, speed) {
        $(container).find('.active').css('z-index', 3);
        setInterval(function() {
            var $active = $(container).find('.active');
            var $next = ($active.next().length > 0) ? $active.next() : $(container).find('.gif').first();
            $next.css('z-index',2);//move the next image up the pile
            $active.fadeOut(1000,function(){//fade out the top image
                $active.css('z-index',1).fadeIn(1000).removeClass('active');//reset the z-index and unhide the image
                $next.css('z-index',3).addClass('active');//make the next image the top one
            });
        }, speed);
    }


        $('.gif--container').each(function(){
            var time = $(this).attr('data-gif-time');
            var container = $(this);
            $(this).find('.gif').first().addClass('active');
            fakeGif(container,time);
        });

和Html这个很简单:

<div class="gif--container" data-gif-time="3000">
    <div class="gif image:parent fl">
        <img src="whatever1.jpg" />
    </div>
    <div class="gif image:parent fl">
        <img src="whatever2.jpg" />
    </div>
    <div class="gif image:parent fl">
        <img src="whatever3.jpg" />
    </div>
</div>

答案 3 :(得分:-1)

我觉得这很简单的jquery让很多不同的员工非常有用:

$(document).ready(function () {
            $('.click-here').click(function () {
                $('.box1').toggleClass("z-index")
            });
        }); 

你基本上说点击任何一个元素都有类&#34;点击这里&#34;将在你想要的任何元素上添加一个类(在这种情况下&#34; box1&#34;)(并在下一次单击时删除该类)。

对于您的问题,您有 fiddle ,希望它能为您提供帮助