【问题标题】:css transition opacity of the background color背景颜色的css过渡不透明度
【发布时间】:2013-08-23 13:55:05
【问题描述】:

寻找线索,如何使用背景颜色的不透明度与过渡?

我正在使用rgba() 函数,但过渡在悬停时不起作用。

.bx{
  background:rgba(255,0,0,0.5);
  width:100px; height:100px;
  position:relative;
  transition: opacity .5s ease-out;
  -moz-transition: opacity .5s ease-out;
  -webkit-transition: opacity .5s ease-out;
  -o-transition: opacity .5s ease-out;
}

.bx:hover{
  background:rgba(255,0,0,1);
}

.t{
  position:absolute; 
  bottom:0px;
}

HTML

<div class="bx"><div class="t">text</div></div>

任何想法,我如何为.bx 使用过渡?

【问题讨论】:

    标签: html css css-transitions


    【解决方案1】:

    其实opacityrgba()是完全不同的。

    由于您使用rgba() 作为background 属性的背景颜色,因此您需要使用background 作为转换属性,如下所示:

    .bx {
      background: rgba(255,0,0,0.5);
      width:100px; height:100px;
      position: relative;
    
      -webkit-transition: background .5s ease-out;
      -moz-transition: background .5s ease-out;
      -o-transition: background .5s ease-out;
      transition: background .5s ease-out;
    }
    
    .bx:hover {
      background: rgba(255,0,0,1);
    }
    

    JSBin Demo.

    【讨论】:

    • 同意。不透明度尚未更改,因此过渡不会附加任何内容。
    • TIL;值得注意的是,对背景进行动画处理比对不透明度进行动画处理要昂贵得多(因为它会导致重绘)。当您有许多元素同时或在低功率设备上进行动画处理时会很明显!
    猜你喜欢
    • 2019-08-14
    • 2013-02-05
    • 1970-01-01
    • 2017-08-11
    • 1970-01-01
    • 2013-10-20
    • 1970-01-01
    • 2012-06-26
    • 1970-01-01
    相关资源
    最近更新 更多