【问题标题】:relative positioning with transition effect具有过渡效果的相对定位
【发布时间】:2015-08-09 01:59:16
【问题描述】:

我有六个带标题的框。我希望标题在鼠标悬停事件中向上滑动。这样做时我面临两个问题。

  1. 如果我使用 position:absolute,图形标题将失去中心对齐。

  2. 如果我使用 position:relative,图形标题会居中对齐,但无法设置动画。

所以总的来说,我想将图形标题居中对齐,并且应该从下到上进行动画处理。

我尝试了以下方法:

Left: 50%;
Right: 50%;
margin-right: 50%;
margin-left 50%;

但这会导致页面居中对齐,但我希望标题对齐框而不是页面的中心。

这是一个例子

a:hover img{
	-webkit-transform: translateY(-20px);
	-moz-transform: translateY(-20px);
	-ms-transform: translateY(-20px);
	transform: translateY(-20px);
}
a:hover .caption{
	display: inline;
	opacity: 1;
	-webkit-transform: translateY(-10px);
	-moz-transform: translateY(-10px);
	-ms-transform: translateY(-10px);
	transform: translateY(-10px);
	-webkit-transition: -webkit-transform 0.4s, opacity 0.1s;
	-moz-transition: -moz-transform 0.4s, opacity 0.1s;
	transition: transform 0.4s, opacity 0.1s;
}
figure{
	overflow: hidden;
}

figure img{
	height: 105px;
	width: 120px;
	padding: 20px 0px 0px 0px;
	display: flex;
	-webkit-transition: -webkit-transform 0.4s;
	-moz-transition: -moz-transform 0.4s;
	transition: transform 0.4s;
}

.caption{
	display: none;
	font-size: 1.4em;
	color: #fff;
	position: relative;
	-webkit-transform: translateY(100%);
	-moz-transform: translateY(100%);
	-ms-transform: translateY(100%);
	transform: translateY(100%);
	-webkit-transition: -webkit-transform 0.4s, opacity 0.1s 0.3s;
	-moz-transition: -moz-transform 0.4s, opacity 0.1s 0.3s;
	transition: transform 0.4s, opacity 0.1s 0.3s;
}
a{
	text-decoration:none;
	outline:none;
	color: $fff;
}


.btn-row{
	margin: 30px 0px 0px 0px;
	display: inline-flex;
}

.box-btn{
	height: 150px;
	width: 150px;
	border-radius: 10px;
	border: 1px solid #bdc1c4;
	background-color: $white;
	margin: 0px 10px 0px 10px;
	outline: none;
}
<center><br>
 <div class="btn-row">
	<a href="domainSearch.html" class="link">
		<figure class="box-btn">
			<img src="style/img/university.jpg" class="img">
			<figcaption class="caption">xyz</figcaption>
		</figure>
	</a>
	<a href="domainSearch.html" class="link">
		<figure class="box-btn">
    		<img src="style/img/university.jpg" class="img">
			<figcaption class="caption">xyz</figcaption>
		</figure>
	</a>
	<a href="domainSearch.html" class="link">
		<figure class="box-btn">
			<img src="style/img/university.jpg" class="img">
			<figcaption class="caption">abcdefghijkl </figcaption>
		</figure>
	</a>
</div>
<div class="btn-row">
	<a href="domainSearch.html" class="link">
		<figure class="box-btn">
			<img src="style/img/university.jpg" class="img">
			<figcaption class="caption">short text</figcaption>
		</figure>
	</a>
    <a href="domainSearch.html" class="link">
		<figure class="box-btn">
			<img src="style/img/university.jpg" class="img">
			<figcaption class="caption">long text coming here
            </figcaption>
		</figure>
 		</a>
  		<a href="domainSearch.html" class="link">
  			<figure class="box-btn">
  				<img src="style/img/university.jpg" class="img">
  				<figcaption class="caption">defghi</figcaption>
  			</figure>
  		</a>
  	</div>
  </center>

【问题讨论】:

标签: css css-position css-transitions


【解决方案1】:

您的问题是 display 属性。您的动画是在display: none; 时制作的,这并不是很有用。只需删除此 display 定义,动画就会正常工作。

(我在 sn-p 中将字幕的颜色更改为黑色以查看它们)

a:hover img{
	-webkit-transform: translateY(-20px);
	-moz-transform: translateY(-20px);
	-ms-transform: translateY(-20px);
	transform: translateY(-20px);
}
a:hover .caption{
	opacity: 1;
	-webkit-transform: translateY(-10px);
	-moz-transform: translateY(-10px);
	-ms-transform: translateY(-10px);
	transform: translateY(-10px);
	-webkit-transition: -webkit-transform 0.4s, opacity 0.1s;
	-moz-transition: -moz-transform 0.4s, opacity 0.1s;
	transition: transform 0.4s, opacity 0.1s;
}
figure{
	overflow: hidden;
}

figure img{
	height: 105px;
	width: 120px;
	padding: 20px 0px 0px 0px;
	display: flex;
	-webkit-transition: -webkit-transform 0.4s;
	-moz-transition: -moz-transform 0.4s;
	transition: transform 0.4s;
}

.caption{
	font-size: 1.4em;
	color: #000;
	position: relative;
	-webkit-transform: translateY(100%);
	-moz-transform: translateY(100%);
	-ms-transform: translateY(100%);
	transform: translateY(100%);
	-webkit-transition: -webkit-transform 0.4s, opacity 0.1s 0.3s;
	-moz-transition: -moz-transform 0.4s, opacity 0.1s 0.3s;
	transition: transform 0.4s, opacity 0.1s 0.3s;
}
a{
	text-decoration:none;
	outline:none;
	color: $fff;
}


.btn-row{
	margin: 30px 0px 0px 0px;
	display: inline-flex;
}

.box-btn{
	height: 150px;
	width: 150px;
	border-radius: 10px;
	border: 1px solid #bdc1c4;
	background-color: $white;
	margin: 0px 10px 0px 10px;
	outline: none;
}
<center><br>
 <div class="btn-row">
	<a href="domainSearch.html" class="link">
		<figure class="box-btn">
			<img src="style/img/university.jpg" class="img">
			<figcaption class="caption">xyz</figcaption>
		</figure>
	</a>
	<a href="domainSearch.html" class="link">
		<figure class="box-btn">
    		<img src="style/img/university.jpg" class="img">
			<figcaption class="caption">xyz</figcaption>
		</figure>
	</a>
	<a href="domainSearch.html" class="link">
		<figure class="box-btn">
			<img src="style/img/university.jpg" class="img">
			<figcaption class="caption">abcdefghijkl </figcaption>
		</figure>
	</a>
</div>
<div class="btn-row">
	<a href="domainSearch.html" class="link">
		<figure class="box-btn">
			<img src="style/img/university.jpg" class="img">
			<figcaption class="caption">short text</figcaption>
		</figure>
	</a>
    <a href="domainSearch.html" class="link">
		<figure class="box-btn">
			<img src="style/img/university.jpg" class="img">
			<figcaption class="caption">long text coming here
            </figcaption>
		</figure>
 		</a>
  		<a href="domainSearch.html" class="link">
  			<figure class="box-btn">
  				<img src="style/img/university.jpg" class="img">
  				<figcaption class="caption">defghi</figcaption>
  			</figure>
  		</a>
  	</div>
  </center>

【讨论】:

    猜你喜欢
    • 2011-12-21
    • 1970-01-01
    • 1970-01-01
    • 2011-12-24
    • 2013-06-22
    • 2017-01-12
    • 1970-01-01
    • 2020-05-18
    • 2012-10-30
    相关资源
    最近更新 更多