禁用/关闭继承的 CSS3 过渡

发布于 2024-11-19 08:38:00 字数 442 浏览 1 评论 0 原文

因此,我将以下 CSS 过渡附加到元素:

a { 
  -webkit-transition:color 0.1s ease-in, background-color 0.1s ease-in ;  
  -moz-transition:color 0.1s ease-in, background-color 0.1s ease-in;  
  -o-transition:color 0.1s ease-in, background-color 0.1s ease-in;  
  transition:color 0.1s ease-in, background-color 0.1s ease-in; 
}

有没有办法在特定元素上禁用这些继承的过渡?

a.tags { transition: none; } 

似乎没有做好这项工作。

So I have the following CSS transitions attached to an element:

a { 
  -webkit-transition:color 0.1s ease-in, background-color 0.1s ease-in ;  
  -moz-transition:color 0.1s ease-in, background-color 0.1s ease-in;  
  -o-transition:color 0.1s ease-in, background-color 0.1s ease-in;  
  transition:color 0.1s ease-in, background-color 0.1s ease-in; 
}

Is there a way to disable these inherited transitions on specific elements?

a.tags { transition: none; } 

Doesn't seem to be doing the job.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

メ斷腸人バ 2024-11-26 08:38:00

transition: none(对 Opera 进行了特定调整)

<a href="#" class="transition">Content</a>
<a href="#" class="transition">Content</a>
<a href="#" class="noTransition">Content</a>
<a href="#" class="transition">Content</a>

考虑到以下 HTML: ...和 ​​CSS:

a {
    color: #f90;
    -webkit-transition:color 0.8s ease-in, background-color 0.1s ease-in ;  
    -moz-transition:color 0.8s ease-in, background-color 0.1s ease-in;  
    -o-transition:color 0.8s ease-in, background-color 0.1s ease-in;  
    transition:color 0.8s ease-in, background-color 0.1s ease-in; 
}
a:hover {
    color: #f00;
    -webkit-transition:color 0.8s ease-in, background-color 0.1s ease-in ;  
    -moz-transition:color 0.8s ease-in, background-color 0.1s ease-in;  
    -o-transition:color 0.8s ease-in, background-color 0.1s ease-in;  
    transition:color 0.8s ease-in, background-color 0.1s ease-in; 
}
a.noTransition {
    -moz-transition: none;
    -webkit-transition: none;
    -o-transition: color 0 ease-in;
    transition: none;
}

/nW2Wt/" rel="noreferrer">JS Fiddle 演示

在 Ubuntu 11.04 上使用 Chromium 12、Opera 11.x 和 Firefox 5 进行了测试。

对 Opera 的具体修改是使用 -o-transition: color 0 escape-in​​; ,它的目标与其他 transition 规则中指定的属性相同,但设置转换时间为 0,这有效地防止了转换变得明显。使用a.noTransition选择器只是为没有转换的元素提供一个特定的选择器。


编辑注意@Frédéric Hamidi的回答< /a>,使用 all (至少对于 Opera 来说)比列出您不想要的每个单独的属性名称要简洁得多进行过渡。

更新了 JS Fiddle 演示,展示了 Opera 中 all 的使用:-o-转换:全部 0 无,在自删除 @Frédéric 的答案。

The use of transition: none seems to be supported (with a specific adjustment for Opera) given the following HTML:

<a href="#" class="transition">Content</a>
<a href="#" class="transition">Content</a>
<a href="#" class="noTransition">Content</a>
<a href="#" class="transition">Content</a>

...and CSS:

a {
    color: #f90;
    -webkit-transition:color 0.8s ease-in, background-color 0.1s ease-in ;  
    -moz-transition:color 0.8s ease-in, background-color 0.1s ease-in;  
    -o-transition:color 0.8s ease-in, background-color 0.1s ease-in;  
    transition:color 0.8s ease-in, background-color 0.1s ease-in; 
}
a:hover {
    color: #f00;
    -webkit-transition:color 0.8s ease-in, background-color 0.1s ease-in ;  
    -moz-transition:color 0.8s ease-in, background-color 0.1s ease-in;  
    -o-transition:color 0.8s ease-in, background-color 0.1s ease-in;  
    transition:color 0.8s ease-in, background-color 0.1s ease-in; 
}
a.noTransition {
    -moz-transition: none;
    -webkit-transition: none;
    -o-transition: color 0 ease-in;
    transition: none;
}

JS Fiddle demo.

Tested with Chromium 12, Opera 11.x and Firefox 5 on Ubuntu 11.04.

The specific adaptation to Opera is the use of -o-transition: color 0 ease-in; which targets the same property as specified in the other transition rules, but sets the transition time to 0, which effectively prevents the transition from being noticeable. The use of the a.noTransition selector is simply to provide a specific selector for the elements without transitions.


Edited to note that @Frédéric Hamidi's answer, using all (for Opera, at least) is far more concise than listing out each individual property-name that you don't want to have transition.

Updated JS Fiddle demo, showing the use of all in Opera: -o-transition: all 0 none, following self-deletion of @Frédéric's answer.

雨落□心尘 2024-11-26 08:38:00

如果您想禁用单个过渡属性,您可以执行以下操作:(

transition: color 0s;

因为零秒过渡与无过渡相同。)

If you want to disable a single transition property, you can do:

transition: color 0s;

(since a zero second transition is the same as no transition.)

┊风居住的梦幻卍 2024-11-26 08:38:00

删除所有过渡的另一种方法是使用 unset 关键字:

a.tags {
    transition: unset;
}

transition 属性,unset 相当于 initial,因为 transition 不是继承属性:

a.tags {
    transition: initial;
}

了解 unset< 的读者/code> 和 初始可以立即判断这些解决方案是正确的,而无需考虑 transition 的具体语法。

Another way to remove all transitions is with the unset keyword:

a.tags {
    transition: unset;
}

When used with the transition property, unset is equivalent to initial, since transition is not an inherited property:

a.tags {
    transition: initial;
}

A reader who knows about unset and initial can tell that these solutions are correct immediately, without having to think about the specific syntax of transition.

云柯 2024-11-26 08:38:00

此外,还可以通过设置属性transition-property: width, height;来设置将要转换的属性列表,更多详细信息此处

Additionally there is a possibility to set a list of properties that will get transitioned by setting the property transition-property: width, height;, more details here

老子叫无熙 2024-11-26 08:38:00

您还可以取消继承包含元素内的所有转换:

CSS:

.noTrans *{
-moz-transition: none;
-webkit-transition: none;
-o-transition: color 0 ease-in;
transition: none;
}

HTML:

<a href="#">Content</a>
<a href="#">Content</a>
<div class="noTrans">
<a href="#">Content</a>
</div>
<a href="#">Content</a>

You could also disinherit all transitions inside a containing element:

CSS:

.noTrans *{
-moz-transition: none;
-webkit-transition: none;
-o-transition: color 0 ease-in;
transition: none;
}

HTML:

<a href="#">Content</a>
<a href="#">Content</a>
<div class="noTrans">
<a href="#">Content</a>
</div>
<a href="#">Content</a>
不打扰别人 2024-11-26 08:38:00

基于 W3schools,默认转换值为:all 0s escape 0s,这应该是禁用转换的跨浏览器兼容方式。

这是一个链接:https://www.w3schools.com/cssref/css3_pr_transition.asp

Based on W3schools default transition value is: all 0s ease 0s, which should be the cross-browser compatible way of disabling the transition.

Here is a link: https://www.w3schools.com/cssref/css3_pr_transition.asp

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文