将鼠标悬停在单个元素CSS

时间:2018-03-12 15:43:41

标签: html css

我希望通过一次悬停将两个类 col-md-3 overlay-text 悬停在一起。将鼠标悬停在图像上时,我想触发第二次悬停,在本例中为文本。

  

如果CSS以某种方式支持if / else语句,我会写if    col-md-3 悬停,然后悬停叠加文字

.overlay-text {
  background-color: #79b13c;
  color: white;
  z-index: 50;
  position: absolute;
  bottom: 0px;
  width: 270px;
  text-align: center;
  font-size: 1.4rem;
  font-weight: 600;
  -webkit-transition: background-color 0.5s ease-in-out, font-size 0.5s;
  -moz-transition: background-color 0.5s ease-in-out, font-size 0.5s;
  -o-transition: background-color 0.5s ease-in-out, font-size 0.5s;
  transition: background-color 0.5s ease-in-out, font-size 0.5s;
}

.overlay-text:hover {
  content: "";
  background-color: #328ba6;
  cursor: pointer;
  transition: ease-in-out 0.5s, font-size 0.5s;
  line-height: 2;
  font-size: 1.5rem;
}

.overlay-text:hover::after {
  transition: ease-in-out 1s, font-size 1s;
}

.col-md-3 {
  -moz-box-sizing: border-box;
  -webkit-box-sizing: border-box;
  box-sizing: border-box;
  overflow: hidden;
  position: relative;
  padding-left: 5px;
  padding-right: 5px;
}

.col-md-3 img {
  -moz-transition: all 0.3s;
  -webkit-transition: all 0.3s;
  transition: all 0.3s;
}

.col-md-3 img:hover {
  -webkit-transform: translate3d(0, -10%, 0);
  transform: translate3d(0, -10%, 0);
  cursor: pointer;
}

.overlay-text-blank {
  text-align: center;
  font-size: 1.8rem;
  font-weight: 600;
  vertical-align: middle;
}
<div class="container">
  <div class="row tiles">
    <div class="col-md-3">
      <img alt="text" src="https://www.codeproject.com/KB/GDI-plus/ImageProcessing2/img.jpg" class="img-fluid">
      <div class="overlay-text">Custom text</div>
    </div>
  </div>
</div>

FiddleJS演示https://jsfiddle.net/57nvbfud/5/

1 个答案:

答案 0 :(得分:2)

这里有两个选项:

首先将hover更改为img:hover + .overlay-text

第二个是更改hover中的col-md-3:hover

&#13;
&#13;
.overlay-text {
	background-color: #79b13c;
	color: white;
	z-index: 50;
	position: absolute;
	bottom: 0px;
	width: 270px;
	text-align: center;
	font-size: 1.4rem;
	font-weight: 600;
	-webkit-transition: background-color 0.5s ease-in-out, font-size 0.5s;
	-moz-transition: background-color 0.5s ease-in-out, font-size 0.5s;
	-o-transition: background-color 0.5s ease-in-out, font-size 0.5s;
	transition: background-color 0.5s ease-in-out, font-size 0.5s;
}

.col-md-3:hover .overlay-text{
	content:"";
	background-color: #328ba6;
	cursor: pointer;
	transition: ease-in-out 0.5s, font-size 0.5s;
	line-height: 2;
	font-size: 1.5rem;
}

.col-md-3:hover .overlay-text::after{
	transition: ease-in-out 1s, font-size 1s;
}

.col-md-3 {
	-moz-box-sizing: border-box;
	-webkit-box-sizing: border-box;
	box-sizing: border-box;
	overflow: hidden;
	position: relative;
	padding-left: 5px;
	padding-right: 5px;
}

.col-md-3 img {
	-moz-transition: all 0.3s;
	-webkit-transition: all 0.3s;
	transition: all 0.3s;
}

.col-md-3:hover img{
	-webkit-transform: translate3d(0,-10%,0);
	transform: translate3d(0,-10%,0);
	cursor: pointer;
}

.overlay-text-blank {
	text-align: center;
	font-size: 1.8rem;
	font-weight: 600;
	vertical-align: middle;
}
&#13;
<div class="container">
  <div class="row tiles">
    <div class="col-md-3">
      <img alt="text" src="https://www.codeproject.com/KB/GDI-plus/ImageProcessing2/img.jpg" class="img-fluid">
      <div class="overlay-text">Custom text</div>
    </div>
  </div>
</div>
&#13;
&#13;
&#13;