简体   繁体   English

如何转换 z-index?

[英]How to transition z-index?

https://jsfiddle.net/vaf6nv36/1/ https://jsfiddle.net/vaf6nv36/1/

Can the balloons image slowly transition over the apple image?气球图像可以在苹果图像上缓慢过渡吗?

I think that I need more transition parameters, or I should use opacity?我想我需要更多的过渡参数,还是应该使用不透明度?

Can someone help me?有人可以帮助我吗?

HTML: HTML:

<div class="img1">

</div>

<div class="img2">
                    
</div>

CSS: CSS:

.img1, .img2{
   border: 1px solid black;
   transition: 1s;
   position: absolute;
}

.img1{
    left: 25%;
    height: 500px;
    width: 500px;
    z-index: 1;
    background-image: url(http://cdn.pcwallart.com/images/balloons-photography-vintage-wallpaper-1.jpg);
   }

.img2{
  right: 25%;
  width: 500px;
  height: 500px;
  bottom: 0;
  z-index: 2;
  background-image:  url(https://i.pinimg.com/736x/c1/7b/15/c17b150e93c4e9c50d963b076484bee7--apple-wallpaper-iphone-wallpaper.jpg);
}

.img1:hover{
       z-index: 999;
}

Although theoretically you can transition z-index , it wouldn't make much sense, ie would not result in the crossfade effect which you obviously are looking for: z-index values are integers, which - when you change them in the smallest possible steps (integers, no commas) - results in states either before the other one OR behind the other one - no transitional "half states" in between.尽管理论上您可以转换z-index ,但这没有多大意义,即不会导致您显然正在寻找的淡入淡出效果: z-index值是整数,当您以尽可能小的步骤更改它们时(整数,无逗号) - 导致状态在另一个之前或在另一个之后 - 中间没有过渡“半状态”。 If you want to do a kind of continuous crossfade between two elements, you should use a transition on opacity .如果你想在两个元素之间做一种连续的淡入淡出,你应该在opacity上使用过渡。

In your particular case, since your DIVs are not directly above each other, but only overlap each other, you can solve that by having a second DIV identical to img2 (I called its class .img3 ), but with z-index: 0 and this CSS rule:在您的特定情况下,由于您的 DIV 不是直接在彼此上方,而只是彼此重叠,您可以通过使用与img2相同的第二个DIV (我将其称为.img3 )来解决该问题,但使用z-index: 0和这个 CSS 规则:

.img1:hover + .img2 {
  opacity: 0;
}

This will fade out img2, but still show img3, which however is behind img1, creating the impression of a transition between img1 and img2.这将淡出 img2,但仍显示 img3,但它位于img1后面,从而产生 img1 和 img2 之间的过渡印象。

https://jsfiddle.net/2a2epLfv/1/ https://jsfiddle.net/2a2epLfv/1/

 .img1, .img2, .img3 { border: 1px solid black; transition: 1s; position: absolute; } .img1 { left: 20%; height: 300px; width: 300px; z-index: 1; background-image: url(http://cdn.pcwallart.com/images/balloons-photography-vintage-wallpaper-1.jpg); } .img2, .img3 { right: 20%; width: 300px; height: 300px; top: 100px; background-image: url(https://i.pinimg.com/736x/c1/7b/15/c17b150e93c4e9c50d963b076484bee7--apple-wallpaper-iphone-wallpaper.jpg); } .img2 { z-index: 2; } .img3 { z-index: 0; } .img1:hover+.img2 { opacity: 0; }
 <div class="img1"></div> <div class="img2"></div> <div class="img3"></div>

I fear z-index transition only makes the element pass step by step through every layer.我担心 z-index 过渡只会使元素逐步通过每一层。 To make a nice effect you need to combine it with opacity transition and scale / position transition.要制作出不错的效果,您需要将其与不透明度过渡和缩放/位置过渡相结合。 The fiddle to show you the idea:向你展示这个想法的小提琴:

 .img1, .img2{ border: 1px solid black; transition: 1s; position: absolute; } .img1{ left: 25%; height: 500px; width: 500px; z-index: 1; transform: scale(0.9); opacity: 0.5; background-image: url(http://cdn.pcwallart.com/images/balloons-photography-vintage-wallpaper-1.jpg); } .img2{ right: 25%; width: 500px; height: 500px; bottom: 0; z-index: 2; background-image: url(https://i.pinimg.com/736x/c1/7b/15/c17b150e93c4e9c50d963b076484bee7--apple-wallpaper-iphone-wallpaper.jpg); } .img1:hover{ animation: fronte 1s linear forwards; } @keyframes fronte { from { z-index: 0; transform: scale(0.9); opacity: 0.5; } to { z-index: 4; transform: scale(1.1); opacity: 1; } }
 <div class="img1"> </div> <div class="img2"> </div>

This is the trick I used.这是我使用的技巧。

 .minus{animation:move 2s;animation-fill-mode:forwards;animation-delay:2s;} @-webkit-keyframes move { 0 {z-index:1;opacity:1} 50% {opacity:0} 100% { z-index:-1;opacity:1} } @keyframes move { 0 {z-index:1;opacity:1} 50% {opacity:0} 100% {z-index:-1;opacity:1} } #main{background:red;width:100vw;height:100vh;position:fixed;top:0;left:0;opacity:.9} .minus{position:fixed;top:10px;left:10px;z-index:1;color:#000}
 <div id="main"></div> <div class="minus">FADE</div>

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

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