简体   繁体   English

如何更新使用分层选择器和 angular 组件输入变量的 css 规则

[英]How to update css rules that use hierarchical selectors with component input variable in angular

I would like to use Angular component input attribute values to modify my CSS rules;我想使用 Angular 组件输入属性值来修改我的 CSS 规则; however, my selector is hierarchical.但是,我的选择器是分层的。 Is this possible?这可能吗?

I have an SVG (country heat map) consisting of a lot of paths that represent counties.我有一个 SVG(国家热图),由许多代表县的路径组成。 While zooming into the counties, I want to apply a class that sets the fill transition on those paths to a configurable duration (to fade in and out).在放大县时,我想应用一个类,将这些路径上的填充过渡设置为可配置的持续时间(淡入和淡出)。

I am doing this by setting a class ( country--zooming ) on the parent country <g> tag as a marker and removing it after the duration.我通过在父国家<g>标记上设置一个类( country--zooming )作为标记并在持续时间后将其删除来做到这一点。 The duration is set on the paths with a CSS selector like this: .country--zooming path {} .使用 CSS 选择器在路径上设置持续时间,如下所示: .country--zooming path {}

I know how to set classes and style properties on a single/specific tag in various ways in Angular – I am doing it with country--zooming .我知道如何在 Angular 中以各种方式在单个/特定标签上设置类和样式属性——我正在使用country--zooming However, I don't know of any way to modify the duration in the CSS that has a selector that represents a hierarchy (in this case all path tags under a parent with a class of country--zooming ).但是,我不知道有什么方法可以修改具有代表层次结构的选择器的 CSS 中的持续时间(在这种情况下,父级下的所有path标签都具有country--zooming类)。

Is there any way to apply a dynamic/configurable CSS rule with a selector that represents a hierarchy?有什么方法可以使用代表层次结构的选择器来应用动态/可配置的 CSS 规则? If not, is there any other way to accomplish this?如果没有,有没有其他方法可以做到这一点?

Here's what I have so far:这是我到目前为止所拥有的:

Template:模板:

<svg>
  <g class="country">
    <path ... />
    <path ... />
    ...
  </g>
</svg>

CSS: CSS:

.country--zooming path {
  transition: fill 2000ms ease-in-out;
}

Typescript:打字稿:

let country = this.chart.nativeElement.querySelector(".country");

country.classList.add("country--zooming");

this.timer = setTimeout(() => {
  country.classList.remove("country--zooming");
}, 2000);

// set the new fill colors for paths here

This works as-is, but I want the duration to be configurable via an input property on the component, so I am looking for a way to set that duration in the CSS or to accomplish this in a way that will allow it to be configurable.这按原样工作,但我希望持续时间可通过组件上的输入属性进行配置,因此我正在寻找一种方法来在 CSS 中设置该持续时间或以允许其可配置的方式完成此操作. I don't think selecting and looping through each path to apply the rule individually is feasible since there are 3.4k+ paths, and these are short animations.我不认为选择和循环每条路径来单独应用规则是可行的,因为有 3.4k+ 条路径,而且这些都是短动画。 Any ideas would be greatly appreciated.任何想法将不胜感激。

**I should add that the reason that I am switching the CSS rule on and off is that I don't want the fill duration to to be delayed when the map is not zooming. **我应该补充一点,我打开和关闭 CSS 规则的原因是我不希望在地图未缩放时延迟填充持续时间。 That is because I am also changing the fill color on hover and right-click (selecting it), so I want the fill change to be instant in that case.那是因为我也在悬停和右键单击(选择它)时更改填充颜色,所以我希望在这种情况下填充更改是即时的。

<svg>
    <g [class.country--zooming]="isCountryZooming" [style.transition-duration]="transitionDuration">
        <path ... />
        <path ... />
    ...
    </g>
</svg>

(and) (和)

.country--zooming path {
  transition: fill inherit ease-in-out;
}

(and) (和)

this.durationTime = 2000;
this.isCountryZooming = true;
this.transitionDuration = durationTime + 'ms';

this.timer = setTimeout(() => {
    this.isCountryZooming = false;
    this.transitionDuration = 'initial';
}, this.durationTime);

// set the new fill colors for paths here

set a configurable transition-duration on the parent element and inherit it to the children elements in css.. The transition-duration of children elements shall be inherit so that it will take the value of whatever the parent element has been configured..在父元素上设置一个可配置的transition-duration并将其继承到 css 中的子元素。 子元素的transition-duration应该是继承的,以便它将采用父元素已配置的任何值。

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

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