简体   繁体   English

CSS过渡不起作用?

[英]CSS Transitions Not Working?

What I'm trying to do here is get a transition when this div is hovered over. 我想在这里做的是在这个div徘徊时获得转换。

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 . 我尝试过渡属性使用transition: alltransition: opacitytransition: background But none of them work. 但它们都不起作用。

It's the display property that's doing it. 这是display属性正在做它。 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. 因为我显然希望保留display属性,因为它适用于所有浏览器,无论年轻还是年老。

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. 我不介意我使用不透明度或背景或任何控制褪色的东西,我只是不知道还能做什么。

Looks like the display property isn't supported with CSS transitions (also see this SO question). 看起来CSS转换不支持display属性 (也见这个 SO问题)。

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. 您最初可以在转换前将宽度/高度设置为0,或者将元素偏离页面(类似于margin-left: -9999px; ),或将不透明度设置为0。

In your case, I would probably use this: 在你的情况下,我可能会使用这个:

.matrix-overlay {
    position: absolute;
    width: 100%;
    height: 100%;
    background: rgb(0,0,0); /* fallback */
    background: rgba(0,0,0,0);
    color: #fff;
    display: block;
    margin-left: -9999px; /* hide element off the page */
    -webkit-transition: background 2s;
    -o-transition: background 2s;
    -moz-transition: background 2s;
    transition: background 2s;
}

a:hover .matrix-overlay {
    margin-left: 0; /* reset element position */
    background: rgba(0,0,0,0.5);
}

Here's what I ended up doing: 这是我最终做的事情:

    .matrix-overlay {
    position: absolute;
    width: 100%;
    height: 100%;
    background: rgb(0,0,0); /* fallback */
    background: rgba(0,0,0,0.7);
    color: #fff;
    -ms-filter: "progid:DXImageTransform.Microsoft.Alpha(Opacity=0)";
    filter: alpha(opacity=0);
    opacity: 0;
    -webkit-transition: opacity 0.2s ease 0s;
    -o-transition: opacity 0.2s ease 0s;
    -moz-transition: opacity 0.2s ease 0s;
    transition: opacity 0.2s ease 0s;
}

a:hover .matrix-overlay {
    -ms-filter: "progid:DXImageTransform.Microsoft.Alpha(Opacity=100)";
    filter: alpha(opacity=100);
    opacity: 1;
    -webkit-transition: opacity 0.15s ease 0s;
    -o-transition: opacity 0.15s ease 0s;
    -moz-transition: opacity 0.15s ease 0s;
    transition: opacity 0.15s ease 0s;
}

You can try the following: 您可以尝试以下方法:

.matrix-overlay {
    position: absolute;
    width: 100%;
    height: 100%;
    background: rgb(0,0,0); /* fallback */
    background: rgba(0,0,0,0.7);
    color: #fff;
    opacity: 0;
    pointer-events:none;
    transition: opacity 0.2s ease 0s;
}

a:hover .matrix-overlay {
    pointer-events:auto;
    opacity: 1;
    transition: opacity 0.15s ease 0s;
}

Using pointer-events:none and opacity:0 together will have nearly the same effect as display:none , but now the transition should work fine. 使用pointer-events:noneopacity:0一起将具有与display:none几乎相同的效果,但现在转换应该正常工作。

you could use clip instead of display, as the element is absolutely positioned 您可以使用clip而不是显示,因为元素是绝对定位的

Working Example 工作实例

eg 例如

.matrix-overlay {
    ...other properties, not display ...
    clip: rect(1px 1px 1px 1px); /* IE7 */
    clip: rect(1px,1px,1px,1px);

}

a:hover .matrix-overlay {
    ..other properties, not display ...
     clip: rect(auto); /* IE7 */
     clip: auto;
}

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 ;) 我输入了古怪的IE7代码剪辑代码以获取信息,虽然IE无论如何也不会进行转换,如果你愿意,你可以将显示代码提供给它,但这种方式使它们保持不变;)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM