【问题标题】:how to create hover animation on a div如何在div上创建悬停动画
【发布时间】:2019-03-26 20:07:20
【问题描述】:

我想创建一个 div,它有一个背景图像和一个透明的背景颜色,上面有一些文本。悬停时,透明背景颜色应向 div 底部滑出,如图所示:

https://i.ibb.co/pJFPvFB/Screenshot-2019-03-26-Zeplin-Project.png

左块是默认的。右块为悬停状态。

我修改了这个sn-p:https://www.w3schools.com/howto/tryit.asp?filename=tryhow_css_image_overlay_fade

我将提供的样式修改为:

<style>
.container {
  position: relative;
  width: 50%;
}

.image {
  display: block;
  width: 100%;
  height: auto;
}

.overlay {
  position: absolute;
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
  height: 100%;
  width: 100%;
  opacity: 0;
  transition: .5s ease;
  background-color: #008CBA;
}

.container .overlay {
  opacity: 0.75;
}

.container:hover .overlay {
  opacity: 0;
}

.text {
  color: white;
  font-size: 20px;
  position: absolute;
  top: 50%;
  left: 50%;
  -webkit-transform: translate(-50%, -50%);
  -ms-transform: translate(-50%, -50%);
  transform: translate(-50%, -50%);
  text-align: center;
}
</style>

编辑: 我的问题

我试图在我的设备上实现一个简单的滑出动画,如我提供的图像所示。看到这个:https://i.ibb.co/pJFPvFB/Screenshot-2019-03-26-Zeplin-Project.png

我尝试过这样的事情 - https://www.w3schools.com/howto/tryit.asp?filename=tryhow_css_image_overlay_fade

我将他们提供的 css 编辑为我上面提供的 css。

请看我提供的图片。我想实现这一目标。

【问题讨论】:

  • 您好,您能在问题中解释一下您目前遇到的问题吗?
  • 嗨,你能分享整个代码,以便我们能够提供帮助
  • @DKyleo 我已经编辑了问题

标签: css


【解决方案1】:

试试这个。

.container {
  position: relative;
  width: 50%;
}

.image {
  display: block;
  width: 100%;
  height: auto;
}

.overlay {
  position: absolute;
  bottom: 0;
  left: 0;
  right: 0;
  height: 100%;
  width: 100%;
  transition: .5s ease;
  background-color: #008CBA;
}

.container:hover .overlay {
  transition: .5s ease;
  height: 0;
}

.text {
  color: white;
  font-size: 20px;
  position: absolute;
  top: 50%;
  left: 50%;
  -webkit-transform: translate(-50%, -50%);
  -ms-transform: translate(-50%, -50%);
  transform: translate(-50%, -50%);
  text-align: center;
}
<div class="container">
  <img src="https://i.ibb.co/pJFPvFB/Screenshot-2019-03-26-Zeplin-Project.png" alt="Avatar" class="image">
  <div class="overlay">
    <div class="text">Hello World</div>
  </div>
</div>

希望这段代码可以帮助你。

【讨论】:

  • 嗨@Pullata,这行得通。但是,我不希望整个覆盖层滑出。大约 5px 的叠加层仍应保留在底部。如果您看到我提供的图像,您就会明白我的意思。谢谢。
  • 我修好了。谢谢。高度:15px;
【解决方案2】:

你好希望这是你正在寻找的东西

Try this fiddle

.container:hover .overlay {
 animation-name: slideDown;
-webkit-animation-name: slideDown;
 animation-duration: 1s;    
-webkit-animation-duration: 1s;
 animation-timing-function: ease;   
-webkit-animation-timing-function: ease;
opacity: 1 !important;              
}

【讨论】:

  • 将叠加背景更改为“背景颜色:rgba(0, 140, 186, .6);”在上面的小提琴中
  • 这与我想要的正好相反。悬停状态和默认状态应该交换。即默认情况下,叠加层将在那里。悬停时,叠加层将消失。
【解决方案3】:

希望这就是你想要的

.box {
width: 200px; height: 100px;
background-size: 100% 200%;
background-image: linear-gradient(to bottom, red 50%, black 50%);
-webkit-transition: background-position 1s;
-moz-transition: background-position 1s;
transition: background-position 1s;}

http://jsfiddle.net/jxgrtmvy

【讨论】:

    猜你喜欢
    • 2014-05-10
    • 2018-06-29
    • 1970-01-01
    • 1970-01-01
    • 2021-06-29
    • 2014-09-07
    • 2017-02-09
    • 2014-09-07
    • 2012-08-11
    相关资源
    最近更新 更多