html - 由于父容器 DIV 背景属性,CSS 过渡不起作用

标签 html css css-transitions

由于容器 DIV 的背景属性,我遇到了 CSS 过渡无法正常工作的问题。这是我在网上找到的一段代码,但是找不到我所在的页面,所以我不能回去问他们。它的目的是做一个普通按钮,但是当鼠标悬停在上面时,会滑出两个段落(一个在顶部,一个在底部)。它自己工作得很好,但是当我把它放到一个容器 DIV 中,并在容器上放置一个背景色时,它就不再工作了。我对它进行了 Firebug,发现当我禁用 BG 属性时,它可以工作。疯狂的是,过渡是边缘的,与 BG 无关。请耐心等待,这是我的第一个问题,我将尝试正确插入代码...

#container
{
	background: #cf6;
	display: inline-block;
	height: 500px;
	margin: 0 auto;
	text-align: center;
	width: 700px
}

.download-button
{
	margin: 50px auto;
	width: 100px;
}

.download-button a
{
	background: #003f87;
	background: -moz-linear-gradient(top, #003f87 0%, #3063a5 100%);
	background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,#003f87), color-stop(100%,#3063a5));
	background: -webkit-linear-gradient(top, #003f87 0%,#3063a5 100%);
	background: -o-linear-gradient(top, #003f87 0%,#3063a5 100%);
	background: -ms-linear-gradient(top, #003f87 0%,#3063a5 100%);
	background: linear-gradient(top, #003f87 0%,#3063a5 100%);
	filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#003f87', endColorstr='#3063a5',GradientType=0 );
	color: white;
	display: block;
	font: 17px/50px Helvetica, Verdana, sans-serif;
	height: 50px;
	text-align: center;
	text-decoration: none;
	text-transform: uppercase;
	width: 200px;
}

.download-button a, 
.download-button p
{
	-webkit-border-radius: 10px;
	-moz-border-radius: 10px;
	border-radius: 10px;
	-webkit-box-shadow: 2px 2px 8px rgba(0,0,0,0.2);
	-moz-box-shadow: 2px 2px 8px rgba(0,0,0,0.2);
	box-shadow: 2px 2px 8px rgba(0,0,0,0.2);
}

.download-button p
{
	background: #222;
	color: #fff;
	display: block;
	font: 12px/45px Helvetica, Verdana, sans-serif;
	height: 40px;
	margin: -50px 0 0 10px;
	position: absolute;
	text-align: center;
	-webkit-transition: margin 0.5s ease;
	-moz-transition: margin 0.5s ease;
	-o-transition: margin 0.5s ease;
	-ms-transition: margin 0.5s ease;
	transition: margin 0.5s ease;
	width: 180px; 
	z-index: -1;
}

.download-button:hover .bottom
{
	margin: -10px 0 0 10px;
}

.download-button:hover .top
{
	line-height: 35px;
	margin: -80px 0 0 10px;
}

.download-button a:active
{
	background: #003f87;
	background: -moz-linear-gradient(top,  #003f87 36%, #3063a5 100%);
	background: -webkit-gradient(linear, left top, left bottom, color-stop(36%,#003f87), color-stop(100%,#3063a5));
	background: -webkit-linear-gradient(top,  #003f87 36%,#3063a5 100%);
	background: -o-linear-gradient(top,  #003f87 36%,#3063a5 100%);
	background: -ms-linear-gradient(top,  #003f87 36%,#3063a5 100%);
	background: linear-gradient(top,  #003f87 36%,#3063a5 100%);
	filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#003f87', endColorstr='#3063a5',GradientType=0 );
}

.download-button:active .bottom
{
	margin: -20px 0 0 10px;
}

.download-button:active .top
{
	margin: -70px 0 0 10px;
}
<div id="container">

<div class="download-button">
	<a href="#">Download</a>
	<p class="top">click to begin</p>
	<p class="bottom">1.2MB .zip</p>
</div>

</div>

最佳答案

像这样尝试: Demo

.download-button a {
    z-index: 100;
    position:relative;
}

.download-button p {  
    z-index: 0;
}

<p>标签边距有 negative z-index 值,这就是它落后于 #container 的原因bg,现在我改变了z-index值为正并为 <a> 赋予位置更多z-index值,以显示在前面

关于html - 由于父容器 DIV 背景属性,CSS 过渡不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30432329/

相关文章:

javascript - 使用javascript为div设置border-left和border-right

javascript - 如何使用javascript获取超出其容器的元素的宽度?

javascript - 使用 jQuery 操作 DOM 时代码不同步?

html - 如何让跨度类出现在标签下方

python - 值包含空格的属性的 Css 选择器

css - 如何使用高度变换 css3 进行叠加

css3 slideup 向下滑动

javascript - 这个javascript代码在没有for循环的情况下运行正常但没有它。任何帮助,我已经尝试了一切

javascript - 根据点击为 HTML <area> 着色

html - 垂直对齐导航菜单中的链接