首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >悬停对象时的动画-适合:包含<img>

悬停对象时的动画-适合:包含<img>
EN

Stack Overflow用户
提问于 2021-08-09 11:37:50
回答 1查看 98关注 0票数 0

在鼠标悬停时,当img没有使用object-fit: contain时,img下面的动画span标签工作得很好,如下所示:

代码语言:javascript
复制
body {
  height: 100%;
  width: 100%;
  margin: 0
}

.container {
  max-width: 600px;
  position: relative;
  text-align: center;
  width: 100%;
}

.product {
  position: relative;
  width: 150px;

}

img.content {
  background: white;
  height: auto;
  margin: 8%;
  position: relative;
  width: 84%;
  vertical-align: middle;
  z-index: 5000;
}

.product:hover .effect-1,
.product:hover .effect-2 {
  display: block;
}

.effect-1,
.effect-2 {
  border-radius: 30%;
  display: none;
  mix-blend-mode: multiply;
  height: 84%;
  opacity: 1;
  position: absolute;
  width: 84%;
  z-index: 3000;
}

.effect-1 {
  animation: rotate 1.8s linear infinite;
  background: cyan;
}

.effect-2 {
  animation: rotate 1.2s linear reverse infinite;
  background: #e7a9ff;
}

.placeholder {
  width: 84%;
  height: auto;
  visibility: hidden;
}

@keyframes rotate {
  0% {
    top: 0;
    left: 8%;
  }
  25% {
    top: 8%;
    left: 0%;
  }
  50% {
    top: 16%;
    left: 8%;
  }
  75% {
    top: 8%;
    left: 16%;
  }
  100% {
    top: 0;
    left: 8%;
  }
}
代码语言:javascript
复制
<body>
    <div class="container">
      <p>Hover image please</p>
      <div class="product">
          <img class="content" src="http://www.petsworld.in/blog/wp-content/uploads/2014/09/Golden-Retriever-Puppies-in-basket.jpg">
          <span class="effect-1"><img class="placeholder" src="http://www.petsworld.in/blog/wp-content/uploads/2014/09/Golden-Retriever-Puppies-in-basket.jpg"></span>
          <span class="effect-2"><img class="placeholder" src="http://www.petsworld.in/blog/wp-content/uploads/2014/09/Golden-Retriever-Puppies-in-basket.jpg"></span>
      </div>
    </div>
</body>

但是当img使用object-fit:包含动画跨度时,会占据整个区域:

代码语言:javascript
复制
body {
  height: 100%;
  margin: 0;
}

.container {
  max-width: 600px;
  position: relative;
  text-align: center;
  width: 100%;
  height: 100%;
  min-height: 700px;
}

.product {
  height: 100%;
  position: absolute;
}
.content {
  background: white;
  margin: 8%;
  position: relative;
  width: 84%;
  height: 100%;
  vertical-align: middle;
  z-index: 5000;
  object-fit: contain;
}

.product:hover .effect-1,
.product:hover .effect-2 {
  display: block;
}

.effect-1,
.effect-2 {
  border-radius: 30%;
  display: none;
  mix-blend-mode: multiply;
  height: 84%;
  opacity: 1;
  position: absolute;
  width: 84%;
  z-index: 3000;
}

.effect-1 {
  animation: rotate 1.8s linear infinite;
  background: cyan;
}

.effect-2 {
  animation: rotate 1.2s linear reverse infinite;
  background: #e7a9ff;
}

.placeholder {
  width: 84%;
  height: auto;
  visibility: hidden;
}

@keyframes rotate {
  0% {
    top: 0;
    left: 8%;
  }
  25% {
    top: 8%;
    left: 0%;
  }
  50% {
    top: 16%;
    left: 8%;
  }
  75% {
    top: 8%;
    left: 16%;
  }
  100% {
    top: 0;
    left: 8%;
  }
}
代码语言:javascript
复制
<body>
    <div class="container">
      <div class="product">
        <span class="effect-1"><img class="placeholder" src="http://www.petsworld.in/blog/wp-content/uploads/2014/09/Golden-Retriever-Puppies-in-basket.jpg"></span>
        <span class="effect-2"><img class="placeholder" src="http://www.petsworld.in/blog/wp-content/uploads/2014/09/Golden-Retriever-Puppies-in-basket.jpg"></span>
          <img class="content" src="http://www.petsworld.in/blog/wp-content/uploads/2014/09/Golden-Retriever-Puppies-in-basket.jpg">
      </div>
    </div>
</body>

在使用object-fit: contain时,如何使这些悬停效果仅应用于图像周围的区域(而不是整个区域)?图像必须使用object-fit保持垂直居中。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-08-10 14:33:22

这是你想要的吗?图像在动画div之间居中。

在您给出的第二个示例中,您的图像较大的原因是因为您在那里更改了CSS。您已经更改了.container.product等的高度/宽度值,因此子元素显示得更大,因为它们继承了这些值。

我已经更改了.container中的max-widthmin-height以减小整体大小。并且.content的宽度应该小于effect div的宽度。

代码语言:javascript
复制
html {
  width: 100%;
  height: 100%;
}

body {
  width: 100%;
  height: 100%;
  margin: 0;
}

.container {
  max-width: 300px;
  /* This is new */
  position: relative;
  text-align: center;
  width: 100%;
  height: 100%;
  min-height: 300px;
  /* This is new */
}

.product {
  height: 100%;
  position: absolute;
  display: flex;
  flex-direction: column;
  justify-content: center;
}

.content {
  display: flex;
  align-self: center;
  background: white;
  margin: 0 auto;
  position: relative;
  width: 65%;
  /* This is new */
  object-fit: contain;
  /* This is new */
}

.product:hover .effect-1,
.product:hover .effect-2 {
  display: flex;
}

.effects {
  position: absolute;
}

.effect-1,
.effect-2 {
  border-radius: 30%;
  display: flex;
  mix-blend-mode: multiply;
  height: 84%;
  opacity: 1;
  position: absolute;
  width: 84%;
  z-index: 3000;
  visibility: visible;
}

.effect-1 {
  animation: rotate 1.8s linear infinite;
  background: cyan;
}

.effect-2 {
  animation: rotate 1.2s linear reverse infinite;
  background: #e7a9ff;
}

.placeholder {
  width: 84%;
  height: auto;
  visibility: hidden;
  object-fit: contain;
  display: flex;
  margin: 0 auto;
  align-self: center;
  position: relative;
}

@keyframes rotate {
  0% {
    top: 0;
    left: 8%;
  }
  25% {
    top: 8%;
    left: 0%;
  }
  50% {
    top: 16%;
    left: 8%;
  }
  75% {
    top: 8%;
    left: 16%;
  }
  100% {
    top: 0;
    left: 8%;
  }
}
代码语言:javascript
复制
<body>
  <div class="container">
    <div class="product">
      <span class="effects">
            <img class="placeholder" src="http://www.petsworld.in/blog/wp-content/uploads/2014/09/Golden-Retriever-Puppies-in-basket.jpg">
            <span class="effect-1"></span>
      <span class="effect-2"></span>
      </span>
      <img class="content" src="http://www.petsworld.in/blog/wp-content/uploads/2014/09/Golden-Retriever-Puppies-in-basket.jpg">
    </div>
  </div>
</body>

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68711424

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档