Styling the same SVG <object> different ways

前端 未结 4 1308
抹茶落季
抹茶落季 2021-01-05 06:05

I want to have a series of the same SVG file on a page with different colours. I\'m aware that the best method of getting the SVG into the page without bloating the code, an

4条回答
  •  误落风尘
    2021-01-05 07:04

    
    document.addEventListener("DOMContentLoaded", function() {
      //attribute name
      const ATTR_NAME = "data-src";
      //base64 encoded brocken image icon
      const ERROR_PLACEHOLDER =
        "";
      let target = document.querySelectorAll(`[${ATTR_NAME}]`);
      //unsorted list
      let _filesList = [];
      target.forEach(function(item) {
        _filesList.push(item.getAttribute(ATTR_NAME).replace(/\\/g, "/"));
      });
      //sorted list
      let filesList = _filesList.filter(function(item, pos) {
        return _filesList.indexOf(item) == pos;
      });
      //ajax request
      filesList.forEach(function(item) {
        let ajax = new XMLHttpRequest();
        ajax.open("GET", item, true);
    
        ajax.onload = function() {
          document.querySelectorAll(`[${ATTR_NAME}="${item}"]`).forEach(item => {
            if (this.status >= 200 && this.status < 400) {
              // Success!
              item.innerHTML = this.response;
            } else {
              // Error!
              item.innerHTML = ERROR_PLACEHOLDER;
            }
          });
        };
    
        ajax.send();
      });
    });
    
    .icon path{
      fill:#000;
    }
    

提交回复
热议问题