如何转换z-index

时间:2017-09-23 22:24:02

标签: html css css3 css-transitions

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

气球图像可以在苹果图像上缓慢过渡吗?

我认为我需要更多转换参数,或者我应该使用不透明度?

有人可以帮忙吗?

HTML

 <div class="img1">

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

 </div>

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;
}

3 个答案:

答案 0 :(得分:3)

我担心z-index转换只会使元素逐步通过每一层。要获得良好的效果,您需要将其与不透明度过渡和缩放/位置过渡相结合。向你展示这个想法的小提琴:

&#13;
&#13;
.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; }
}
&#13;
<div class="img1">
</div>
<div class="img2">
</div>
&#13;
&#13;
&#13;

答案 1 :(得分:3)

虽然理论上你可以转换z-index,但它没有多大意义,也就是说不会导致您显然正在寻找的交叉渐变效果:{{1}值是整数,当你以尽可能小的步骤(整数,没有逗号)改变它们时 - 导致状态要么在另一个之前,要么在另一个之后 - 没有过渡&#34;半状态&#34;之间。如果要在两个元素之间进行某种连续交叉渐变,则应在z-index上使用转换。

在您的特定情况下,由于您的DIV不是直接在彼此之上,而是彼此重叠,您可以通过让第二个opacityDIV相同来解决这个问题(我称之为img2 {1}}),但.img3和此CSS规则:

z-index: 0

这会淡出img2,但仍会显示img3,然而 img1后面,会产生img1和img2之间转换的印象。

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

&#13;
&#13;
.img1:hover + .img2 {
  opacity: 0;
}
&#13;
.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;
}
&#13;
&#13;
&#13;

答案 2 :(得分:0)

这是我使用的技巧。

&#13;
&#13;
.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}
&#13;
<div id="main"></div>
<div class="minus">FADE</div>
&#13;
&#13;
&#13;