首页 > 解决方案 > 当我将鼠标悬停在图像上时动画不起作用

问题描述

我正在尝试在图像上的右侧动画中添加扫描,这样当您将图像悬停时,它会向右扫描,并且在悬停时也会显示文本。我可以通过按钮找到它,但我不知道如何去做。这是我到目前为止的代码:

.portfolio-box {
  position: relative;
  overflow: hidden;
  z-index: 1;
}

.portfolio-box img {
  width: 300px; /*changed this from 100% for the q*/
  height:auto;
  /*object-fit: contain;*/
}

.portfolio-mask {
  position: absolute;
  display: flex;
  justify-content: center;
  align-items: center;
  opacity: 0;
  background: #060606;
  top: 0;
  left: 0;
  bottom: 0;
  right: 0;
  height: 100%;
  width: 100%;
  transform: scaleX(0);
  transform-origin: 0 50%;
  transition: transform 0.5s ease-out;
}

.portfolio-mask:hover:after {
  transform: scaleX(1);
}

.portfolio-mask:hover {
  opacity: .85;
  color: white;
}
<div class="portfolio-box">
  <img src="https://upload.wikimedia.org/wikipedia/commons/4/45/A_small_cup_of_coffee.JPG" alt="Coffee">
  <div class="portfolio-mask">
    <p class="portfolio-text">Design Mock up</p>
  </div>
</div>

标签: htmlcssanimation

解决方案


我认为根据您的描述,您正在尝试做的是一个transform. 为此,只需translateX向 css 添加一个转换(您想要的大小)。

希望这可以帮助。

.portfolio-box {
  position: relative;
  overflow: hidden;
  z-index: 1;
}

.portfolio-box img {
  width: 300px; /*changed this from 100% for the q*/
  height:auto;
  /*object-fit: contain;*/
}
.portfolio-box img.animate:hover{
  transform: translateX(5em);
}
.portfolio-mask {
  position: absolute;
  display: flex;
  justify-content: center;
  align-items: center;
  opacity: 0;
  background: #060606;
  top: 0;
  left: 0;
  bottom: 0;
  right: 0;
  height: 100%;
  width: 100%;
  transform: scaleX(0);
  transform-origin: 0 50%;
  transition: transform 0.5s ease-out;
}

.portfolio-mask:hover:after {
  transform: scaleX(1);
}

.portfolio-mask:hover {
  opacity: .85;
  color: white;
}
<div class="portfolio-box">
  <img class="animate"  src="https://upload.wikimedia.org/wikipedia/commons/4/45/A_small_cup_of_coffee.JPG" alt="Coffee">
  <div class="portfolio-mask">
    <p class="portfolio-text">Design Mock up</p>
  </div>
</div>


推荐阅读