How to check if a svg path has a class that matches a value in array and if so add a new class

前端 未结 1 1916
情话喂你
情话喂你 2021-01-27 07:47

I have an array and some svg path elements (I am using leaflet map). I need to check if the class of a path matches one of the values in my array and if so add a cl

相关标签:
1条回答
  • 2021-01-27 08:01

    Your bug is subtle. You are starting with an Array of Strings:

    var nationList = ["usa", "france", "italy"];
    

    Then you're calling String.prototype.includes, passing path.className as an argument.

    if (foundNations.includes(path.className)) { path.classList.add('fadeIn')
    

    In there, you're implicitly assuming that path.className is a String. But, surprise surprise, it's not a String, it's a SVGAnimatedString!

    console.log(path.className)
    > [object SVGAnimatedString] {
       animVal: "germany",
       baseVal: "germany"
      }
    

    Yes, class names for SVG elements can be modified during animations in some edge cases.

    What you probably want to do is use the baseVal property of the SVGAnimatedStrings:

    console.log(typeof path.className.baseVal)
    > "string"
    

    And now everything should work more closely to the way you expect:

    if (foundNations.includes(path.className.baseVal)) {
      path.classList.add('fadeIn')
    }
    console.log(path.className.baseVal);
    > "spain fadeIn"
    


    You have a second problem, due to another assumption. You're assuming that path.className contains just one class name, but according to the documentation, emphasis mine:

    cName is a string variable representing the class or space-separated classes of the current element.

    In fact, if you use the developer tools available in your browser to inspect the SVG elements, you'll see things like...

    <path class="Italy leaflet-interactive" stroke="#ffffff" ....></path>

    So in this case, you're assuming that the className.baseVal is going to be the string "Italy", but in reality, it takes the value "Italy leaflet-interactive".

    The approach here is to use Element.classList to iterate through the class names to see if any of them match a given group.



    Furthermore, I think that this is an instance of the XY problem. I don't think you wanted to ask

    How to check if a SVG path has a class that maches foo?

    but rather

    How to symbolize a SVG polygon in Leaflet when the feature matches foo?

    Because I think that it is way more elegant to move the checks into the style callback function, like:

    geojson = L.geoJson(statesData, {
      style: function(feature){ 
        var polygonClassName = feature.properties.name;
        if (nationList.contains(feature.properties.name)) {
            polygonClassName += ' fadeIn';
        }
        return {
          weight: 1,
          opacity: 1,
          color: '#ffffff',
          dashArray: '',
          fillOpacity: 0,
          fillColor : '#FF0080',
          className: polygonClassName
        };
      },
      onEachFeature: onEachFeature
    }).addTo(map);
    

    Leaflet offers convenient functionality like L.Path.setStyle that hides the complexity of dealing with selectors and SVG classes directly, as long as you keep references to your instances of L.Polygon around (which, in this case, you can do in the onEachFeature callback).

    0 讨论(0)
提交回复
热议问题