简体   繁体   English

单击更改其他元素的Z索引

[英]Changing Z-Index Of Another Element On Click

in the code below I am trying to figure out how can I change "content"s z-index relatively to clicked "menu-item". 在下面的代码中,我试图找出如何相对于单击“菜单项”来更改“内容”的z-index。 I managed how to do this for menu items, but cannot find solution for the rest. 我设法对菜单项执行此操作,但是找不到其余的解决方案。 In simple words I need to click #m1 and set Z-Index 10 for #c1 and so on. 简单来说,我需要单击#m1并为#c1设置Z-Index 10,依此类推。

HTML 的HTML

<div id="content" class="container">
    <div id="c1" class="content">content1</div>
    <div id="c2" class="content">content2</div>
    <div id="c3" class="content">content3</div>
    <div id="c4" class="content">content4</div>
</div>
<div id="menu" class="container">
    <div id="m1" class="menu-item"></div>
    <div id="m2" class="menu-item"></div>
    <div id="m3" class="menu-item"></div>
    <div id="m4" class="menu-item"></div>
</div>

CSS 的CSS

/*global*/
.container{
    position: absolute;
    width: 300px;
    height: 100px;
}
/*content*/
.content{
    position: absolute;
    height: 100%;
    width: 75%;
    right: 0;
    background: #354458;
    text-align: center;
    color: #fff;
    line-height: 95px;
}
/*menu*/
.menu-item{
    position: absolute;
    width: 25%;
    height: 100%;
    background: green;
    cursor: pointer;
    transition: left 200ms ease-in-out;

}
.menu-item.closed{
    left: 0 !important;
}
#m1{
    left:0;
    background: #DB3340;

}
#m2{
    left: 25%; 
    background: #E8B71A;
}
#m3{
    left: 50%; 
    background: #1FDA9A;
}
#m4{
    left: 75%; 
    background: #28ABE3;
}

JQuery jQuery查询

$(document).ready(function(){
    var menu = $('.menu-item');

    menu.click(function(){
        $(this).siblings(menu).css('z-index', "initial");
        $(this).css('z-index', 11);
    });
    menu.click(function(){
        menu.toggleClass("closed");
    });
});

Working example: http://jsfiddle.net/8a3vqy5v/ 工作示例: http : //jsfiddle.net/8a3vqy5v/

You could get the index of the clicked .menu-item element and then select the corresponding .content element using the index with the .eq() method : 您可以获取单击的.menu-item元素的索引,然后使用带有.eq()方法的索引来选择相应的.content元素:

Updated Example 更新示例

var $menu = $('.menu-item');

$menu.click(function() {
  $(this).css('z-index', 11).siblings($menu).add('.content').css('z-index', '');

  if (!$(this).hasClass('closed')) {
    $('.content').eq($(this).index()).css('z-index', 10);
  }
  $menu.toggleClass("closed");
});

But since that creates a weird transition bug, you could use the code from this example instead. 但是,由于这会产生怪异的过渡错误,因此您可以使用此示例中的代码。 It essentially listens to the transitionend event. 它本质上侦听transitionend事件。

No need to register multiple click events for the same element, they serve for your case same as a single binding. 无需为同一元素注册多个click事件,它们在您的情况下的作用与单个绑定相同。

You can hide and show the background content based on the menu index as follows within the click event, you can check the code snippet for full code: 您可以在click事件中根据菜单索引隐藏和显示背景内容,如下所示,您可以检查代码段中的完整代码:

var index = $(this).index();
$('.content').hide();
$('.content').eq(index).show();

Snippet : 片段:

 $(document).ready(function(){ var menu = $('.menu-item'); menu.click(function(){ $(this).siblings(menu).css('z-index', "initial"); $(this).css('z-index', 11); menu.toggleClass("closed"); var index = $(this).index(); $('.content').hide(); $('.content').eq(index).show(); }); }); 
 /*global*/ .container{ position: absolute; width: 300px; height: 100px; } /*content*/ .content{ position: absolute; height: 100%; width: 75%; right: 0; background: #354458; text-align: center; color: #fff; line-height: 95px; } /*menu*/ .menu-item{ position: absolute; width: 25%; height: 100%; background: green; cursor: pointer; transition: left 200ms ease-in-out; } .menu-item.closed{ left: 0 !important; } #m1{ left:0; background: #DB3340; } #m2{ left: 25%; background: #E8B71A; } #m3{ left: 50%; background: #1FDA9A; } #m4{ left: 75%; background: #28ABE3; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="content" class="container"> <div id="c1" class="content">content1</div> <div id="c2" class="content">content2</div> <div id="c3" class="content">content3</div> <div id="c4" class="content">content4</div> </div> <div id="menu" class="container"> <div id="m1" class="menu-item"></div> <div id="m2" class="menu-item"></div> <div id="m3" class="menu-item"></div> <div id="m4" class="menu-item"></div> </div> 

if there is a fixed number of elements (ie: 4) you could simply throw 4 rules, one for each : 如果有固定数量的元素(即4个),则可以简单地抛出4条规则,每条规则一个:

$('#m1').click(function() {
    $('.content').css('z-index', '');
    $('#c1').css('z-index', '11');
});
$('#m2').click(function() {
    $('.content').css('z-index', '');
    $('#c2').css('z-index', '11');
});
$('#m3').click(function() {
    $('.content').css('z-index', '');
    $('#c3').css('z-index', '11');
});
$('#m4').click(function() {
    $('.content').css('z-index', '');
    $('#c4').css('z-index', '11');
});

if you want a single rule that would apply to any #mx - #cx couple, you could also try to get the id of the element clicked as a string and manipulate the string in order to replace the first letter with a 'c' 如果您想要一条适用于任何#mx #cx对的规则,则还可以尝试获取单击为字符串的元素的ID,并操纵该字符串,以将第一个字母替换为'c'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM