【问题标题】:Hover transition doesn't work悬停过渡不起作用
【发布时间】:2016-09-08 20:30:14
【问题描述】:

所以我最近一直在做一个项目,我应用到 div 的转换不起作用。不知道为什么!

这是CSS代码

.stats .box {
  background-color: rgba(255, 255, 255,0.07);
    color: white;
    padding: 40px 0;
    text-align: center;
    transition: 0.7s all ease-out;
    margin: 20px 0;

}

.stats h2 {
    margin: 25px 0 10px 0;
  font-size: 35px;
  font-family: "Montserrat";
  color: rgb(255, 255, 255);
  text-transform: uppercase;
letter-spacing: 2px;
    font-weight: 200;
}

.stats h4 {
    font-weight: 100;
    font-size: 15px;
}

.stats .box:hover {
    background-image: -moz-linear-gradient(102deg, #1ad2fd 11%, #008aff 100%);
    background-image: -webkit-linear-gradient(102deg, #1ad2fd 11%, #008aff 100%);
    background-image: -ms-linear-gradient(102deg, #1ad2fd 11%, #008aff 100%);
    transition: 0.7s all ease-out;
}

这是 HTML 代码

<div class="col-sm-6">
<div class="box">
    <h2>4326</h2>
    <h4>Lines of Code</h4>
</div>

如果您想现场观看,这里也是 CodePen http://codepen.io/PlatoCode/pen/qadvZV

谢谢

【问题讨论】:

    标签: html css css-transitions transitions


    【解决方案1】:

    过渡不支持渐变动画,你唯一能做的就是使用不透明度

    .box {
      background-color: rgba(255, 255, 255, 0.07);
      color: black;
      padding: 40px 0;
      text-align: center;
      transition: 0.7s all ease-out;
      margin: 20px 0;
      position: relative;
    }
    h2 {
      margin: 25px 0 10px 0;
      font-size: 35px;
      font-family: "Montserrat";
      text-transform: uppercase;
      letter-spacing: 2px;
      font-weight: 200;
      z-index: 2;
    }
    h4 {
      font-weight: 100;
      font-size: 15px;
      z-index: 2;
    }
    .box:after {
      content: "";
      position: absolute;
      top: 0;
      left: 0;
      right: 0;
      bottom: 0;
      transition: 0.7s all ease-out;
      opacity: 0;
      background-image: -moz-linear-gradient(102deg, #1ad2fd 11%, #008aff 100%);
      background-image: -webkit-linear-gradient(102deg, #1ad2fd 11%, #008aff 100%);
      background-image: -ms-linear-gradient(102deg, #1ad2fd 11%, #008aff 100%);
      z-index: -1;
    }
    .box:hover:after {
    opacity: 1;
      }
    <div class="col-sm-6">
      <div class="box">
        <h2>4326</h2>
        <h4>Lines of Code</h4>
      </div>

    【讨论】:

    • 但如果我将 .box 的不透明度设置为 0 则不会显示
    • 如果你想让盒子的内容显示你可以将渐变移动到一个伪元素上,比如 after 并在那里做
    • 非常感谢!它现在可以工作,但我仍然无法为包含这些元素的 div 设置背景颜色。这是它的代码笔codepen.io/PlatoCode/pen/qadvZV?editors=1100
    • 似乎您的深色背景会阻止渐变显示,因为它是 z-index:-1 但您需要否则文本会重叠。您可能必须将其拆分为两个容器,一个用于渐变,一个用于文本,然后将文本容器覆盖在渐变上。
    猜你喜欢
    • 2014-12-15
    • 1970-01-01
    • 2018-09-04
    • 2016-06-13
    • 1970-01-01
    • 1970-01-01
    • 2018-01-19
    • 1970-01-01
    • 2015-09-18
    相关资源
    最近更新 更多