简体   繁体   English

使 div 聚焦并更改 z-index

[英]Making divs focus and change z-index

So basically we have a concept picture: http://imgur.com/a/Z38Fy所以基本上我们有一个概念图: http : //imgur.com/a/Z38Fy

Each of these window's is a div element on the site that on click should get on top.这些窗口中的每一个都是站点上的一个 div 元素,单击时应位于顶部。 Let's say we click on window #2, that means that window 2 is on top now and window 1 is behind it.假设我们单击窗口 #2,这意味着窗口 2 现在在顶部,而窗口 1 在其后面。 This is literally how the Windows operating system individual windows work.这就是 Windows 操作系统各个窗口的工作方式。

Is this possible using jQuery and javascript?这可以使用 jQuery 和 javascript 吗?

Is this what you are looking for?这是您要找的吗?

Set the z-index when click on a div, and set the z-index of the others to something lower设置z-index上一个div单击时,与设置z-index别人的东西下

 $("div").click(function() { $("div").not(this).css("z-index", "1") $(this).css("z-index", "2") })
 div { position: absolute; height: 100px; width: 100px; border: 1px solid #000; background-color:white; } .one {} .two { top: 40px; left: 100px; } .three { top: 70px; left: 40px; }
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="one">1</div> <div class="two">2</div> <div class="three">3</div>

A quick example一个简单的例子

 $(".window").on("click", function() { $(".window").css("z-index", 0); $(this).css("z-index", 1); });
 .window { width: 250px; height: 250px; } .window:nth-child(1) { background-color: lightblue; position: absolute; left: 20px; top: 20px; } .window:nth-child(2) { background-color: purple; position: absolute; left: 100px; top: 60px; } .window:nth-child(3) { background-color: darkgreen; position: absolute; left: 180px; top: 20px; }
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="container"> <div class="window">W1</div> <div class="window">W2</div> <div class="window">W3</div> </div>

As Carsten Løvbo Andersen answered, yes!正如 Carsten Løvbo Andersen 回答的那样,是的! it is posible, and he gave us an example that works using jQuery and javascript.这是可能的,他给了我们一个使用 jQuery 和 javascript 的例子。

I just want to point out that it can be done by using css and html only, what answer the title of this question "Making divs focus and change z-index".我只想指出,它可以通过仅使用 css 和 html 来完成,这个问题的标题“使 divs 聚焦并更改 z-index”的答案是什么。

See modified Carsten Løvbo Andersen example:参见修改后的 Carsten Løvbo Andersen 示例:

 .container { position: absolute; height: 100px; width: 100px; border: 1px solid #000; background-color: white; z-index: 0; } .container:focus { z-index: 1; } .one {} .two { top: 40px; left: 100px; } .three { top: 70px; left: 40px; }
 <div class="container one" tabIndex="0">1</div> <div class="container two" tabIndex="0">2</div> <div class="container three" tabIndex="0">3</div>

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

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