简体   繁体   English

LESS和IE9过滤器:没有SVG梯度兼容性吗?

[英]LESS and IE9 filter:none for svg gradient compatibility?

I'm developing a site using the LESS preprocessor and was wondering if it's absolutely necessary to include every single class or element to which I'm applying a gradient background in the conditional filter override to make IE9 behave... 我正在使用LESS预处理器开发站点,并且想知道是否绝对有必要在条件过滤器替代中包含我要为其应用渐变背景的每个类或元素,以使IE9发挥作用。

<!--[if gte IE 9]>
  <style type="text/css">
    .the-first-gradient-class{ filter: none; }
    .the-second-gradient-class{ filter: none; }

    ...

    .the-last-gradient-class{ filter: none; }
  </style>
<![endif]-->

Since LESS is handling the gradients, I'm not adding a specific 'gradient' class to my outputted HTML, and doing that is kind of a pain (and I don't really want to deal with that). 由于LESS正在处理渐变,因此我没有在输出的HTML中添加特定的“渐变”类,这样做有点痛苦(而且我真的不想处理)。 Is there anything preventing me from just saying something like this and just being done with it?? 有什么阻止我只是说这样的话而已而已吗?

<!--[if gte IE 9]>
  <style type="text/css">
    * { filter: none; }
  </style>
<![endif]-->

Your idea will not work 你的想法行不通

The point of the conditional statement is to override IE9 when a gradient has been defined with an svg background as well as a filter gradient for older versions of IE ( see for example this SO question ). 当已经定义了带有svg背景的渐变以及用于IE较旧版本的filter渐变时,条件语句的重点是覆盖IE9( 例如,参见此SO问题 )。

To get the IE9 to use the svg properly, it needs to be set to override the filter set for the earlier versions. 为了使IE9正确使用svg ,需要将其设置为覆盖早期版本的过滤器集。 That normally is done by using the cascade of the css to reset the filter, hence why the same selector names are used in the conditional comment which follows the original css defining the gradient. 通常,这是通过使用CSS的级联来重置过滤器来完成的,因此,为什么要在条件注释中使用相同的选择器名称,而条件注释遵循定义梯度的原始CSS。 The later IE9 css will automatically be used because it is the same selector but comes later in the cascade. 更高版本的IE9 css将被自动使用,因为它是相同的选择器,但在级联中出现得更晚。

A second way to do it would be to have the selector for the IE9 conditional be of higher specificity than any applied gradient selector. 第二种方法是让IE9条件选择器的特异性高于任何应用的梯度选择器。 However, since you are just using a variety of selectors with LESS, this would be challenging, but not insurmountable. 但是,由于您仅使用带有LESS的各种选择器,因此这将具有挑战性,但并非不可克服。

So why your idea does not work 那为什么你的想法不起作用

The * selector is of 0 specificity . *选择器的特异性为0 It affects all elements, but not with any power. 它影响所有元素,但不影响任何功能。 So it cannot override any other selector in the cascade, nor by brute force. 因此,它不能覆盖级联中的任何其他选择器,也不能强行使用。 Essentially, in your case, it would do nothing other than set elements that already do not have filters defined to have a filter: none set on them, keeping all other filter code in place for those that are defined (including your gradient code). 本质上,在您的情况下,除了设置尚未定义要具有filter: none器的过滤器的设置元素外,它什么都不做filter: none在它们上未设置任何元素,将所有其他filter代码保留在已定义的那些位置(包括梯度代码)。

Most likely success for a work around 解决问题的最可能成功方法

Since you don't want to set a single class on the various elements with a gradient (the easiest way to probably handle it), and are instead dealing with a variety of selectors generated by LESS that may include a gradient, your going to have to handle it by expanding the CSS for those selectors. 由于您不想在带有渐变的各种元素上设置单个类(可能最简单的方法),而是要处理由LESS生成的可能包含渐变的各种选择器,因此通过扩展这些选择器的CSS来处理它。 There are a variety of ways this could be done, some that would probably still be able to feed it into a conditional so other browsers do not see it, but one example (that would be picked up by all browsers) would be to plan on setting a class on the html when it is in IE9, and then do this: 有很多种方法可以完成,有些可能仍然可以将其提供给有条件的,因此其他浏览器看不到它,但是一个示例(所有浏览器都会采用)是计划的。在IE9中在html上设置一个类,然后执行以下操作:

LESS

.someClass {
  filter: gradientFilter;
  .ie9resetFilter();
}

.someOtherClass {
  filter: gradientFilter;
  .ie9resetFilter();
}

.ie9resetFilter() {
  html.ie9 & {
    filter: none;
  }
}

CSS Output CSS输出

.someClass {
  filter: gradientFilter;
}
html.ie9 .someClass {
  filter: none;
}
.someOtherClass {
  filter: gradientFilter;
}
html.ie9 .someOtherClass {
  filter: none;
}

If you are not using filters for anything else in IE9 then... 如果您没有将过滤器用于IE9中的其他任何内容,则...

If you don't care about overriding every single instance of a filter call (no matter what type of filter it is) for IE9, then you could set an id on the html or body element, and do an overload selection using conditional comments (using the fact that "Repeated occurrances of the same simple selector are allowed and do increase specificity." ). 如果您不关心为IE9覆盖过滤器调用的每个实例(无论它是哪种类型的过滤器),那么可以在htmlbody元素上设置一个id ,然后使用条件注释进行重载选择(使用“允许同一简单选择器重复出现并增加特异性的事实。” )。 So let's say you have an id of "myID" set on the body , then you could do this in a conditional to almost guarantee an override of any filter for IE9: 假设您在body上设置了一个id为“ myID”,则可以在有条件的情况下执行此操作,以确保几乎覆盖IE9的任何filter

<!--[if gte IE 9]>
  <style type="text/css">
    #myID#myID#myID#myID * { filter: none; } /* specificity is 400 */
  </style>
<![endif]-->

I am assuming you don't have any normal selectors strings in your LESS that would have a specificity equal to four id's (I hope not anyway). 我假设您的LESS中没有任何普通的选择器字符串,它们的特异性等于四个id(我希望无论如何都不要)。

This is far from ideal, but is a workaround to avoid individual selector overriding. 这远非理想,但是一种避免单个选择器被覆盖的解决方法。

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

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