鼠标移动时文字消失

时间:2016-10-31 16:47:25

标签: html css

当我将鼠标移到图像上时,文本会弹出,但是当我移动鼠标后,它会消失。我不知道为什么会这样,而且真的令人沮丧。有没有人知道这里可能会发生什么?



.hvr-bounce-to-right {
  display: inline-block;
  vertical-align: middle;
  -webkit-transform: perspective(1px) translateZ(0);
  transform: perspective(1px) translateZ(0);
  box-shadow: 0 0 1px transparent;
  position: relative;
  -webkit-transition-property: color;
  transition-property: color;
  -webkit-transition-duration: 0.5s;
  transition-duration: 0.5s;
}
.imgWrap {
  position: relative;
}
.imgDescription {
  position: absolute;
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
  color: #fff;
  visibility: hidden;
  opacity: 0;
}
.imgWrap:hover .imgDescription {
  visibility: visible;
  opacity: 1;
}
.hvr-bounce-to-right:before {
  content: "";
  position: absolute;
  z-index: 1;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: #2D2D2D;
  -webkit-transform: scaleX(0);
  transform: scaleX(0);
  -webkit-transform-origin: 0 50%;
  transform-origin: 0 50%;
  -webkit-transition-property: transform;
  transition-property: transform;
  -webkit-transition-duration: 0.5s;
  transition-duration: 0.5s;
  -webkit-transition-timing-function: ease-out;
  transition-timing-function: ease-out;
  opacity: 0.1;
}
.hvr-bounce-to-right:hover,
.hvr-bounce-to-right:focus,
.hvr-bounce-to-right:active {
  color: white;
}
.hvr-bounce-to-right:hover:before,
.hvr-bounce-to-right:focus:before,
.hvr-bounce-to-right:active:before {
  -webkit-transform: scaleX(1);
  transform: scaleX(1);
  -webkit-transition-timing-function: cubic-bezier(0.52, 1.64, 0.37, 0.66);
  transition-timing-function: cubic-bezier(0.52, 1.64, 0.37, 0.66);
}

<!doctype html>
<html>

<head>
  <meta charset="utf-8">
  <title>Untitled Document</title>
  <link href="cows.css" rel="stylesheet" type="text/css">
</head>

<body>
  <h1>Hello</h1>
  <div class="hvr-bounce-to-right">
    <div class="imgWrap">
      <p class="imgDescription">This is some hovertext</p>
      <img class="cow" src="cow.jpg" alt="cow">
    </div>
  </div>
</body>

</html>
&#13;
&#13;
&#13;

CSS文档

&#13;
&#13;
@charset "utf-8";

/* CSS Document */

.hvr-bounce-to-right {
  display: inline-block;
  vertical-align: middle;
  -webkit-transform: perspective(1px) translateZ(0);
  transform: perspective(1px) translateZ(0);
  box-shadow: 0 0 1px transparent;
  position: relative;
  -webkit-transition-property: color;
  transition-property: color;
  -webkit-transition-duration: 0.5s;
  transition-duration: 0.5s;
}
.imgWrap {
  position: relative;
}
.imgDescription {
  position: absolute;
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
  color: #fff;
  visibility: hidden;
  opacity: 0;
  /*remove comment if you want a gradual transition between states
  -webkit-transition: visibility opacity 0.2s;
  */
}
.imgWrap:hover .imgDescription {
  visibility: visible;
  opacity: 1;
}
.hvr-bounce-to-right:before {
  content: "";
  position: absolute;
  z-index: 1;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: #2D2D2D;
  -webkit-transform: scaleX(0);
  transform: scaleX(0);
  -webkit-transform-origin: 0 50%;
  transform-origin: 0 50%;
  -webkit-transition-property: transform;
  transition-property: transform;
  -webkit-transition-duration: 0.5s;
  transition-duration: 0.5s;
  -webkit-transition-timing-function: ease-out;
  transition-timing-function: ease-out;
  opacity: 0.1;
}
.hvr-bounce-to-right:hover,
.hvr-bounce-to-right:focus,
.hvr-bounce-to-right:active {
  color: white;
}
.hvr-bounce-to-right:hover:before,
.hvr-bounce-to-right:focus:before,
.hvr-bounce-to-right:active:before {
  -webkit-transform: scaleX(1);
  transform: scaleX(1);
  -webkit-transition-timing-function: cubic-bezier(0.52, 1.64, 0.37, 0.66);
  transition-timing-function: cubic-bezier(0.52, 1.64, 0.37, 0.66);
}
&#13;
&#13;
&#13;

3 个答案:

答案 0 :(得分:2)

这是一个z-index问题。

尝试

.imgDescription {
  position: absolute;
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
  color: #fff;
  visibility: hidden;
  opacity: 0;
  /*remove comment if you want a gradual transition between states
  -webkit-transition: visibility opacity 0.2s;
  */
  z-index: 1;
}

.hvr-bounce-to-right {
  display: inline-block;
  vertical-align: middle;
  -webkit-transform: perspective(1px) translateZ(0);
  transform: perspective(1px) translateZ(0);
  box-shadow: 0 0 1px transparent;
  position: relative;
  -webkit-transition-property: color;
  transition-property: color;
  -webkit-transition-duration: 0.5s;
  transition-duration: 0.5s;
}
.imgWrap {
  position: relative;
}
.imgDescription {
  position: absolute;
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
  color: #fff;
  visibility: hidden;
  opacity: 0;
  /*remove comment if you want a gradual transition between states
  -webkit-transition: visibility opacity 0.2s;
  */
  z-index: 1;
}
.imgWrap:hover .imgDescription {
  visibility: visible;
  opacity: 1;
}
.hvr-bounce-to-right:before {
  content: "";
  position: absolute;
  z-index: 1;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: #2D2D2D;
  -webkit-transform: scaleX(0);
  transform: scaleX(0);
  -webkit-transform-origin: 0 50%;
  transform-origin: 0 50%;
  -webkit-transition-property: transform;
  transition-property: transform;
  -webkit-transition-duration: 0.5s;
  transition-duration: 0.5s;
  -webkit-transition-timing-function: ease-out;
  transition-timing-function: ease-out;
  opacity: 0.1;
}
.hvr-bounce-to-right:hover,
.hvr-bounce-to-right:focus,
.hvr-bounce-to-right:active {
  color: white;
}
.hvr-bounce-to-right:hover:before,
.hvr-bounce-to-right:focus:before,
.hvr-bounce-to-right:active:before {
  -webkit-transform: scaleX(1);
  transform: scaleX(1);
  -webkit-transition-timing-function: cubic-bezier(0.52, 1.64, 0.37, 0.66);
  transition-timing-function: cubic-bezier(0.52, 1.64, 0.37, 0.66);
}
<h1>Hello</h1>
<div class="hvr-bounce-to-right">
  <div class="imgWrap">
    <p class="imgDescription">This is some hovertext</p>
    <img class="cow" src="http://www.placebacon.net/400/200?image=0" alt="cow">
  </div>
</div>

答案 1 :(得分:1)

问题是你的选择器。

而不是:

.imgWrap:hover .imgDescription {
  visibility: visible;
  opacity: 1;
}

使用此:

.hvr-bounce-to-right:hover .imgDescription {
  visibility: visible;
  opacity: 1;
}

代码段:

&#13;
&#13;
.hvr-bounce-to-right {
  display: inline-block;
  vertical-align: middle;
  -webkit-transform: perspective(1px) translateZ(0);
  transform: perspective(1px) translateZ(0);
  box-shadow: 0 0 1px transparent;
  position: relative;
  -webkit-transition-property: color;
  transition-property: color;
  -webkit-transition-duration: 0.5s;
  transition-duration: 0.5s;
}
.imgWrap {
  position: relative;
}
.imgWrap img {
  vertical-align: middle;
}
.imgDescription {
  position: absolute;
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
  color: #fff;
  visibility: hidden;
  opacity: 0;
  /*remove comment if you want a gradual transition between states
  -webkit-transition: visibility opacity 0.2s;
  */
}
.hvr-bounce-to-right:hover .imgDescription {
  visibility: visible;
  opacity: 1;
}
.hvr-bounce-to-right:before {
  content: "";
  position: absolute;
  z-index: 1;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: #2D2D2D;
  -webkit-transform: scaleX(0);
  transform: scaleX(0);
  -webkit-transform-origin: 0 50%;
  transform-origin: 0 50%;
  -webkit-transition-property: transform;
  transition-property: transform;
  -webkit-transition-duration: 0.5s;
  transition-duration: 0.5s;
  -webkit-transition-timing-function: ease-out;
  transition-timing-function: ease-out;
  opacity: 0.1;
}
.hvr-bounce-to-right:hover,
.hvr-bounce-to-right:focus,
.hvr-bounce-to-right:active {
  color: white;
}
.hvr-bounce-to-right:hover:before,
.hvr-bounce-to-right:focus:before,
.hvr-bounce-to-right:active:before {
  -webkit-transform: scaleX(1);
  transform: scaleX(1);
  -webkit-transition-timing-function: cubic-bezier(0.52, 1.64, 0.37, 0.66);
  transition-timing-function: cubic-bezier(0.52, 1.64, 0.37, 0.66);
}
&#13;
<h1>Hello</h1>
<div class="hvr-bounce-to-right">
  <div class="imgWrap">
    <p class="imgDescription">This is some hovertext</p>
    <img class="cow" src="http://www.placebacon.net/400/200?image=0" alt="cow">
  </div>
</div>
&#13;
&#13;
&#13;

<强>解释

因为您使用伪元素::before作为叠加层并在其1属性中为其赋予值z-index,所以它将覆盖父级的堆栈上下文中的其他元素元素.hvr-bounce-to-right

你可以这样看。当您悬停.hvr-bounce-to-right元素时,您无法通过伪元素,因此您不是真正地悬停.imgWrap,而是伪元素。

由于.hvr-bounce-to-right是悬停的,您可以将其用作选择器并解决问题。

备注:

  • 要删除<img>元素下的不需要的空间,请使用 vertical-align: middle;

此评论:

 /*remove comment if you want a gradual transition between states
  -webkit-transition: visibility opacity 0.2s;
  */

错误,您不能将transitionvisibility等具有二进制设置的属性一起使用。它只是在两个状态(可见/隐藏)之间切换,而不是在opacity之类的值之间切换,这些值可以从0转到1。正确的值是:

transition: opacity 0.2s;

修改

  

我试图在我的悬停元素中对齐我的文字,我希望它是   中心,但似乎无法想到这样做的方法。任何   建议?

使用垂直和水平居中的transform平移方式:

el {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}

代码段:

&#13;
&#13;
.hvr-bounce-to-right {
  display: inline-block;
  vertical-align: middle;
  -webkit-transform: perspective(1px) translateZ(0);
  transform: perspective(1px) translateZ(0);
  box-shadow: 0 0 1px transparent;
  position: relative;
  -webkit-transition-property: color;
  transition-property: color;
  -webkit-transition-duration: 0.5s;
  transition-duration: 0.5s;
}
.imgWrap {
  position: relative;
}
.imgWrap img {
  vertical-align: middle;
}
.imgDescription {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  color: #fff;
  visibility: hidden;
  opacity: 0;
  margin: 0;
  /*remove comment if you want a gradual transition between states
  transition: opacity 0.2s;
  */
}
.hvr-bounce-to-right:hover .imgDescription {
  visibility: visible;
  opacity: 1;
}
.hvr-bounce-to-right:before {
  content: "";
  position: absolute;
  z-index: 1;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: #2D2D2D;
  -webkit-transform: scaleX(0);
  transform: scaleX(0);
  -webkit-transform-origin: 0 50%;
  transform-origin: 0 50%;
  -webkit-transition-property: transform;
  transition-property: transform;
  -webkit-transition-duration: 0.5s;
  transition-duration: 0.5s;
  -webkit-transition-timing-function: ease-out;
  transition-timing-function: ease-out;
  opacity: 0.1;
}
.hvr-bounce-to-right:hover,
.hvr-bounce-to-right:focus,
.hvr-bounce-to-right:active {
  color: white;
}
.hvr-bounce-to-right:hover:before,
.hvr-bounce-to-right:focus:before,
.hvr-bounce-to-right:active:before {
  -webkit-transform: scaleX(1);
  transform: scaleX(1);
  -webkit-transition-timing-function: cubic-bezier(0.52, 1.64, 0.37, 0.66);
  transition-timing-function: cubic-bezier(0.52, 1.64, 0.37, 0.66);
}
&#13;
<h1>Hello</h1>
<div class="hvr-bounce-to-right">
  <div class="imgWrap">
    <p class="imgDescription">This is some hovertext</p>
    <img class="cow" src="http://www.placebacon.net/400/200?image=0" alt="cow">
  </div>
</div>
&#13;
&#13;
&#13;

答案 2 :(得分:0)

您所要做的就是删除这两行

Visibility: hidden;

Visibility: visible;