【问题标题】:Increase transparent background opacity on hover?增加悬停时的透明背景不透明度?
【发布时间】:2014-02-21 23:39:58
【问题描述】:

当用户将鼠标悬停在我的 div 上时,我希望我的背景图像的不透明度从 0.1 增加到 0.5。

HTML

<div id="list">
    <div class="line_one">om nom nom nom...</div>
    <div class="line_two">18 foods to make you incredibly hungry</div>
</div>

CSS

#list {
  display:block;
  position: relative; 
  -webkit-transition: all 1s ease; 
  -moz-transition: all 1s ease; 
  -o-transition: all 1s ease; 
  -ms-transition: all 1s ease; 
  transition: all 1s ease;
}

#list::after {
  content: "";
  background: url('test.jpg');
  opacity: 0.1;
  top: 0;
  left: 0;
  bottom: 0;
  right: 0;
  position: absolute;
  z-index: -1;
}

必须有一种方法可以在没有 Javascript 的情况下做到这一点。有什么想法吗?

【问题讨论】:

  • 发帖时请注意实时预览;)
  • 该死,在我有机会修复它之前就解决了。以后会做的。
  • 还有一个提示,我希望更多人知道 XD 在发布 CSS 块时,将 &lt;!-- language: lang-css --&gt;(具有精确间距)放在代码块之前的一行,这将允许正确的语法突出显示.
  • 啊,很高兴知道。在任何地方的说明中都没有看到。下次会做。

标签: html css opacity


【解决方案1】:

当然有。

#list::after {
    transition: opacity 1s ease;
}
#list:hover::after {
    opacity: 0.5;
}

【讨论】:

  • 完美!我没有意识到 ::after 需要在 :hover 之后,而不是之前。这就是我的问题。 SO 让我在 10 分钟内接受这个答案。
  • 别忘了在投票箭头下面打勾,你得积分,他也得。
  • @Night 为时过早 - 问题必须在 15 分钟前提出 ;)
猜你喜欢
  • 1970-01-01
  • 2017-03-20
  • 2015-03-07
  • 2019-03-06
  • 1970-01-01
  • 2018-04-07
  • 2011-10-24
  • 2018-01-23
相关资源
最近更新 更多