使用 z-index 将重叠 css3 过渡带到前面
Bring an overlapping css3 transition to the front with z-index
我正在创建一个投资组合页面来显示一些图像。当您将鼠标悬停在缩略图上时,我使用 CSS3 过渡将缩略图展开为全尺寸图像,但其他拇指位于展开元素的前面。我尝试使用 z-index 将它们强制到后面,但它不起作用。有没有办法在展开的时候把过渡元素带到顶层?这是我编写的代码的精简版本。当您将鼠标悬停在红色框上时,我希望绿色框留在展开的红色后面。
CSS:
* {
box-sizing: border-box;
}
.row:after {
content: "";
clear: both;
display: block;
}
[class*="col-"] {
float: left;
padding: 15px;
}
.col-1 {width: 8.33%;}
.col-2 {width: 16.66%;}
.col-3 {width: 25%;}
.col-4 {width: 33.33%;}
.col-5 {width: 41.66%;}
.col-6 {width: 50%;}
.col-7 {width: 58.33%;}
.col-8 {width: 66.66%;}
.col-9 {width: 75%;}
.col-10 {width: 83.33%;}
.col-11 {width: 91.66%;}
.col-12 {width: 100%;}
.grow1{
background: red;
background-size: contain;
background-repeat: no-repeat;
width: 100px;
height: 50px;
-webkit-transition: width 1s, height 1s;
transition: width 1s, height 1s;
z-index: 1;
}
.grow1:hover{
background-size: contain;
width:150px;
height:70px;
z-index: 2;
}
.grow2{
background: green;
background-size: contain;
background-repeat: no-repeat;
width: 100px;
height: 50px;
-webkit-transition: width 1s, height 1s;
transition: width 1s, height 1s;
z-index: 1;
}
.grow2:hover{
background-size: contain;
width:150px;
height:70px;
z-index: 2;
}
HTML:
<div class="row">
<div class="col-4"></div>
<div class="col-2">
<div class="grow1"></div>
</div>
<div class="col-2">
<div class="grow2"></div>
</div>
<div class="col-4"></div>
这里还有 Fiddle link:https://jsfiddle.net/timmyll/8nef4v88/1/
谢谢!
这在 z-index
中是可能的,但它只适用于绝对定位的元素。将 position: absolute;
添加到两个框并使用 .grow1:hover, .grow2:hover { z-index: 5; }
将悬停框置于最前面。
当您的鼠标离开第一个框时,第二个框将在第一个框的前面,而过渡仍然是 运行。我想不出一个纯粹的 CSS 解决方法来避免这种情况。
尝试添加 position: absolute;
我已经在此处更新了您的代码,您应该相应地添加 position
的属性
即top: ?px
left: ?px
.grow1{
position: absolute;
background: red;
background-size: contain;
background-repeat: no-repeat;
width: 100px;
height: 50px;
-webkit-transition: width 1s, height 1s;
transition: width 1s, height 1s;
z-index: 1;
}
.grow1:hover{
background-size: contain;
width:150px;
height:70px;
z-index: 999;
}
.grow2{
position: absolute;
background: green;
background-size: contain;
background-repeat: no-repeat;
width: 100px;
height: 50px;
-webkit-transition: width 1s, height 1s;
transition: width 1s, height 1s;
z-index: 2;
}
.grow2:hover{
background-size: contain;
width:150px;
height:70px;
z-index: 1;
}
添加一个转换,它将起作用
.grow1:hover{
transform: translateZ(0px);
}
为了让它在悬停时继续工作,我添加了一个过渡:变换。
为了证明第二个元素也有效,我在其中做了一个 translateX
* {
box-sizing: border-box;
}
.row:after {
content: "";
clear: both;
display: block;
}
[class*="col-"] {
float: left;
padding: 15px;
}
.col-1 {width: 8.33%;}
.col-2 {width: 16.66%;}
.col-3 {width: 25%;}
.col-4 {width: 33.33%;}
.col-5 {width: 41.66%;}
.col-6 {width: 50%;}
.col-7 {width: 58.33%;}
.col-8 {width: 66.66%;}
.col-9 {width: 75%;}
.col-10 {width: 83.33%;}
.col-11 {width: 91.66%;}
.col-12 {width: 100%;}
.grow1{
background: red;
background-size: contain;
background-repeat: no-repeat;
width: 100px;
height: 50px;
-webkit-transition: width 1s, height 1s;
transition: width 1s, height 1s, transform 1s;
z-index: 1;
}
.grow1:hover{
background-size: contain;
width:150px;
height:70px;
z-index: 2;
transform: translateZ(0px);
}
.grow2{
background: green;
background-size: contain;
background-repeat: no-repeat;
width: 100px;
height: 50px;
-webkit-transition: width 1s, height 1s;
transition: width 1s, height 1s, transform 1s;
z-index: 1;
}
.grow2:hover{
background-size: contain;
width:150px;
height:70px;
z-index: 2;
transform: translateX(-50px);
}
<div class="row">
<div class="col-4"></div>
<div class="col-2">
<div class="grow1"></div>
</div>
<div class="col-2">
<div class="grow2"></div>
</div>
<div class="col-4"></div>
我正在创建一个投资组合页面来显示一些图像。当您将鼠标悬停在缩略图上时,我使用 CSS3 过渡将缩略图展开为全尺寸图像,但其他拇指位于展开元素的前面。我尝试使用 z-index 将它们强制到后面,但它不起作用。有没有办法在展开的时候把过渡元素带到顶层?这是我编写的代码的精简版本。当您将鼠标悬停在红色框上时,我希望绿色框留在展开的红色后面。
CSS:
* {
box-sizing: border-box;
}
.row:after {
content: "";
clear: both;
display: block;
}
[class*="col-"] {
float: left;
padding: 15px;
}
.col-1 {width: 8.33%;}
.col-2 {width: 16.66%;}
.col-3 {width: 25%;}
.col-4 {width: 33.33%;}
.col-5 {width: 41.66%;}
.col-6 {width: 50%;}
.col-7 {width: 58.33%;}
.col-8 {width: 66.66%;}
.col-9 {width: 75%;}
.col-10 {width: 83.33%;}
.col-11 {width: 91.66%;}
.col-12 {width: 100%;}
.grow1{
background: red;
background-size: contain;
background-repeat: no-repeat;
width: 100px;
height: 50px;
-webkit-transition: width 1s, height 1s;
transition: width 1s, height 1s;
z-index: 1;
}
.grow1:hover{
background-size: contain;
width:150px;
height:70px;
z-index: 2;
}
.grow2{
background: green;
background-size: contain;
background-repeat: no-repeat;
width: 100px;
height: 50px;
-webkit-transition: width 1s, height 1s;
transition: width 1s, height 1s;
z-index: 1;
}
.grow2:hover{
background-size: contain;
width:150px;
height:70px;
z-index: 2;
}
HTML:
<div class="row">
<div class="col-4"></div>
<div class="col-2">
<div class="grow1"></div>
</div>
<div class="col-2">
<div class="grow2"></div>
</div>
<div class="col-4"></div>
这里还有 Fiddle link:https://jsfiddle.net/timmyll/8nef4v88/1/
谢谢!
这在 z-index
中是可能的,但它只适用于绝对定位的元素。将 position: absolute;
添加到两个框并使用 .grow1:hover, .grow2:hover { z-index: 5; }
将悬停框置于最前面。
当您的鼠标离开第一个框时,第二个框将在第一个框的前面,而过渡仍然是 运行。我想不出一个纯粹的 CSS 解决方法来避免这种情况。
尝试添加 position: absolute;
我已经在此处更新了您的代码,您应该相应地添加 position
的属性
即top: ?px
left: ?px
.grow1{
position: absolute;
background: red;
background-size: contain;
background-repeat: no-repeat;
width: 100px;
height: 50px;
-webkit-transition: width 1s, height 1s;
transition: width 1s, height 1s;
z-index: 1;
}
.grow1:hover{
background-size: contain;
width:150px;
height:70px;
z-index: 999;
}
.grow2{
position: absolute;
background: green;
background-size: contain;
background-repeat: no-repeat;
width: 100px;
height: 50px;
-webkit-transition: width 1s, height 1s;
transition: width 1s, height 1s;
z-index: 2;
}
.grow2:hover{
background-size: contain;
width:150px;
height:70px;
z-index: 1;
}
添加一个转换,它将起作用
.grow1:hover{
transform: translateZ(0px);
}
为了让它在悬停时继续工作,我添加了一个过渡:变换。
为了证明第二个元素也有效,我在其中做了一个 translateX
* {
box-sizing: border-box;
}
.row:after {
content: "";
clear: both;
display: block;
}
[class*="col-"] {
float: left;
padding: 15px;
}
.col-1 {width: 8.33%;}
.col-2 {width: 16.66%;}
.col-3 {width: 25%;}
.col-4 {width: 33.33%;}
.col-5 {width: 41.66%;}
.col-6 {width: 50%;}
.col-7 {width: 58.33%;}
.col-8 {width: 66.66%;}
.col-9 {width: 75%;}
.col-10 {width: 83.33%;}
.col-11 {width: 91.66%;}
.col-12 {width: 100%;}
.grow1{
background: red;
background-size: contain;
background-repeat: no-repeat;
width: 100px;
height: 50px;
-webkit-transition: width 1s, height 1s;
transition: width 1s, height 1s, transform 1s;
z-index: 1;
}
.grow1:hover{
background-size: contain;
width:150px;
height:70px;
z-index: 2;
transform: translateZ(0px);
}
.grow2{
background: green;
background-size: contain;
background-repeat: no-repeat;
width: 100px;
height: 50px;
-webkit-transition: width 1s, height 1s;
transition: width 1s, height 1s, transform 1s;
z-index: 1;
}
.grow2:hover{
background-size: contain;
width:150px;
height:70px;
z-index: 2;
transform: translateX(-50px);
}
<div class="row">
<div class="col-4"></div>
<div class="col-2">
<div class="grow1"></div>
</div>
<div class="col-2">
<div class="grow2"></div>
</div>
<div class="col-4"></div>