简体   繁体   English

如何将 CSS class 更改为<use> SVG里面的孩子?</use>

[英]How to change CSS class to an <use> children inside a SVG?

I am making and animation the objetive is change the xlink:href inside a SVG.我正在制作 animation 的目标是更改 SVG 内的xlink:href (this is for change a shape), and change class respect to their position inside. (这是为了改变一个形状),并把 class 相对于他们的 position 里面改变。

This is my SVG这是我的 SVG

<svg viewBox="-20 -20 600 200" id="main">
  <defs id="test">
    <rect width="80" height="80" id="circle" fill="red" class="first" />
    <rect width="80" height="80" id="square" fill="pink" class="second" />
    <rect width="80" height="80" id="cross" fill="blue" class="third" />
  </defs>

  <g id="load-area">
    <use x="0" xlink:href="#circle" />
    <use x="100" xlink:href="#square" />
    <use x="200" xlink:href="#cross" />
  </g>
</svg>

The class in every rect element, has a different animation-delay according to position (first execute at 0s, second at 2s, third at 4s and so on).每个rect元素中的 class 根据 position 具有不同的animation-delay (第一次在 0 秒执行,第二次在 2 秒执行,第三次在 4 秒执行,依此类推)。

With JS I change every <use> at #load-area使用 JS,我在#load-area更改每个<use>

main.children['load-area'].children[0].setAttribute("xlink:href", getFigure(random()));

And it works, the shape changes but, suppose when it gets three times the id #cross then all elements have third CSS class.它起作用了,形状发生了变化,但是,假设当它得到三倍的 id #cross时,所有元素都有third CSS class。

I need change CSS class inside every children of <use> , How can I do that?我需要在<use>的每个孩子中更改CSS class ,我该怎么做?

Below an element tree:在元素树下面:

在此处输入图像描述

I get all <use> with: main.children['load-area'].children but it does not have child element, as I show u below:我得到所有<use>main.children['load-area'].children但它没有子元素,如下所示:

在此处输入图像描述

You can solve this using CSS variables that you combine with nth-child selector and you no more need the classes.您可以使用 CSS 变量来解决这个问题,这些变量与nth-child选择器结合使用,您不再需要这些类。

Here is a basic example这是一个基本示例

 rect { animation:change 3s var(--d,0s) infinite; } @keyframes change { 0% { opacity:1; } 33%,100% { opacity:0; } } #load-area > use:nth-child(1) {--d:0s} #load-area > use:nth-child(2) {--d:1s} #load-area > use:nth-child(3) {--d:2s} /*#load-area > use:nth-child(N) {--d:Xs}*/
 <svg viewBox="-20 -20 600 200" id="main"> <defs id="test"> <rect width="80" height="80" id="circle" fill="red" /> <rect width="80" height="80" id="square" fill="pink" /> <rect width="80" height="80" id="cross" fill="blue" /> </defs> <g id="load-area"> <use x="0" xlink:href="#circle" /> <use x="100" xlink:href="#square" /> <use x="200" xlink:href="#cross" /> </g> </svg> <svg viewBox="-20 -20 600 200" id="main"> <g id="load-area"> <use x="0" xlink:href="#square" /> <use x="100" xlink:href="#circle" /> <use x="200" xlink:href="#cross" /> </g> </svg>

If the number is unknown or very big you can easily use a JS loop:如果数字未知或非常大,您可以轻松使用 JS 循环:

 var e = document.querySelectorAll('#load-area use'); for(var i=0;i<e.length;i++) { e[i].style.setProperty('--d',i+"s"); }
 rect { animation:change 3s var(--d,0s) infinite; } @keyframes change { 0% { opacity:1; } 33%,100% { opacity:0; } }
 <svg viewBox="-20 -20 600 200" id="main"> <defs id="test"> <rect width="80" height="80" id="circle" fill="red" /> <rect width="80" height="80" id="square" fill="pink" /> <rect width="80" height="80" id="cross" fill="blue" /> </defs> <g id="load-area"> <use x="0" xlink:href="#circle" /> <use x="100" xlink:href="#square" /> <use x="200" xlink:href="#cross" /> </g> </svg>

document.getElementsByTagName("use")[0].setAttribute("xlink:href", "#circle");

element.setAttribute(attributename, attributevalue) element.setAttribute(属性名,属性值)

Here is how you can change element attributes这是更改元素属性的方法

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

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