使用z-index将重叠的css3过渡到前面

时间:2015-05-30 16:28:31

标签: html css3 css-transitions 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>

这里也是小提琴链接:https://jsfiddle.net/timmyll/8nef4v88/1/

谢谢!

3 个答案:

答案 0 :(得分:0)

z-index可以实现,但它只适用于绝对定位元素。将position: absolute;添加到两个框中,然后使用.grow1:hover, .grow2:hover { z-index: 5; }将悬停的框放在前面。

当鼠标离开第一个框时,第二个框将位于第一个框的前面,而转换仍在运行。我无法想到一个纯粹的CSS解决方法来避免这种情况。

答案 1 :(得分:0)

尝试添加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;
}

答案 2 :(得分:0)

添加转换,它将起作用

.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>