CSS 转换不起作用?
我在这里想做的是当这个 div 悬停在上面时进行转换。
一切都很好,只是过渡没有任何作用。我尝试过将过渡属性与 transition: all
、transition: opacity
和 transition: background
一起使用。但它们都不起作用。
display
属性正在执行此操作。因为当我把它拿出来时,它就起作用了。我还能怎样解决这个问题?因为我显然想保留 display
属性,因为它适用于所有浏览器,无论新旧浏览器。
这就是我目前所得到的:
.matrix-overlay {
position: absolute;
width: 100%;
height: 100%;
background: rgb(0,0,0); /* fallback */
background: rgba(0,0,0,0);
color: #fff;
display: none;
-webkit-transition: background 2s;
-o-transition: background 2s;
-moz-transition: background 2s;
transition: background 2s;
}
a:hover .matrix-overlay {
display: block;
background: rgba(0,0,0,0.5);
}
我不介意是否使用不透明度或背景或其他任何东西来控制褪色,我只是不知道还能做什么。
What I'm trying to do here is get a transition when this div is hovered over.
Everything works fine, its just the transition that does nothing. I've tried using the transition properties with transition: all
, transition: opacity
and transition: background
. But none of them work.
It's the display
property that's doing it. Because when I take that out, it works. How else can I get around this? Because I obviously want to keep the display
property, as it works on all browsers, young and old.
Here's what I've got at the moment:
.matrix-overlay {
position: absolute;
width: 100%;
height: 100%;
background: rgb(0,0,0); /* fallback */
background: rgba(0,0,0,0);
color: #fff;
display: none;
-webkit-transition: background 2s;
-o-transition: background 2s;
-moz-transition: background 2s;
transition: background 2s;
}
a:hover .matrix-overlay {
display: block;
background: rgba(0,0,0,0.5);
}
I don't mind if I'm using opacity or background or whatever to control the fading, I just don't know what else to do.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
CSS 过渡似乎不支持 显示属性(另请参阅 这个所以问题)。
考虑到这一点,您有多种选择。您最初可以在预转换中将宽度/高度设置为 0,或者将元素偏移到页面之外(例如
margin-left: -9999px;
),或者将不透明度设置为 0。你的情况,我可能会用这个:
Looks like the display property isn't supported with CSS transitions (also see this SO question).
With that in mind, you have several options. You could initially set the width/height to 0 in your pre-transition, or offset the element off the page (something like
margin-left: -9999px;
), or set the opacity to 0.In your case, I would probably use this:
这就是我最终所做的:
Here's what I ended up doing:
您可以使用
clip
而不是显示,因为该元素是绝对定位的工作示例
例如,
我放入了古怪的 IE7 代码剪辑代码以获取信息,尽管 IE 无论如何都不进行转换,如果您愿意,您可以向其提供显示代码,但这种方式可以使它们保持不变;)
you could use
clip
instead of display, as the element is absolutely positionedWorking Example
e.g.
I put in the quirky IE7 code clip code for information, although IE doesn't do transitions anyway and you could feed it the display codes if you wanted to, but this way keeps them the same ;)
您可以尝试以下操作:
同时使用
pointer-events:none
和opacity:0
与display:none
几乎具有相同的效果,但是现在过渡应该可以正常进行。You can try the following:
Using
pointer-events:none
andopacity:0
together will have nearly the same effect asdisplay:none
, but now the transition should work fine.