Preloader Script - Change SVG Fill Color Before Pa

2019-08-13 12:26发布

I'm using SVG icon in the preloader. I want to change the color of the icon's fill using a script. I have a small problem, the color changes only for a short while before closing preloader.

Here is my script:

jQuery(function($){
  document.querySelector(".svgicon").getSVGDocument().getElementById("loadericon").setAttribute("fill", "#FFF");
});

And my HTML:

<object class="svgicon" type="image/svg+xml" data="'. get_template_directory_uri() . '/assets/images/preloaders/icon.svg'.'"
></object>

In the icon.svg file there is added ID "loadericon" to the HTML tag.

Can anyone help me with this?

标签: jquery svg
2条回答
\"骚年 ilove
2楼-- · 2019-08-13 12:44

Yes you should be able to do that with the fill attribute, like this:

<button id="btnColor">change color</button><br>
<object id="svgContent" type="image/svg+xml" data="cat.svg#redcat"></object>

<script>
    var i = 0
    var colors = ["lime", "cyan", "red", "pink", "blue"]
    var object = document.querySelector("object");

    btnColor.addEventListener("click", function (e) {
        var svg = object.contentDocument.documentElement
        svg.getElementById("redcat").children[0].setAttribute('fill', colors[i])
        i = (i+1) % colors.length   
    })
</script>

As long as you don't have any CORS issues, here is a working sample:
https://raw.githack.com/heldersepu/hs-scripts/html/HTML/SVG/object.html

Now on that example I'm working from the SVG image that @enxaneta provided, hopefully that is close to yours, if not please provide your image.

查看更多
仙女界的扛把子
3楼-- · 2019-08-13 12:54

One option would be using the target attribute and refer only to a part of the SVG while everything else is hidden svg > svg:not(:target) {display: none;} Please pay attention to the url for the data attribute: cat.svg#redcat

object {  
display: inline-block;
margin:.5em;
width: 100px;
height: auto;
border:1px solid #d9d9d9;
}
<object type="image/svg+xml"
data="https://s3-us-west-2.amazonaws.com/s.cdpn.io/222579/cat.svg#redcat">
</object>

<object type="image/svg+xml"
data="https://s3-us-west-2.amazonaws.com/s.cdpn.io/222579/cat.svg#blackcat">
</object>

The SVG I'm using looks like this. Please note the CSS inside the SVG.

<style type="text/css">
 <![CDATA[  
    svg > svg:not(:target) {
    display: none;
    }
     ]]> 
</style>
<desc>
<g id="cat">
<path id="cat_body" d="M121.506,108.953. . . z"/>
<path id="cat_head" d="M129.747,18.651. . . .z"/>
</g>
</desc>
<svg version="1.1" id="blackcat" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" viewBox="0 0 225 225">
<use xlink:href ="#cat" fill="black" />
</svg>
<svg version="1.1" id="redcat" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" viewBox="0 0 225 225">
<use xlink:href ="#cat" fill="red" />
</svg>

You can read more about this technique in the book Using SVG with CSS3 and HTML5: Vector Graphics for Web Design chapter 9: A New Point of View

UPDATE: In this case I'm using a button to change the color of the SVG paths.You may use any other event you like.

let data = "https://s3-us-west-2.amazonaws.com/s.cdpn.io/222579/cat.svg";
let object = document.querySelector("object");

black.addEventListener("click", function changeColor(e){
   object.setAttribute('data', data +"#blackcat");
   let clone = object.cloneNode(true);
   let parent = object.parentNode;
   parent.removeChild(object );
   parent.appendChild(clone );
  
   e.currentTarget.removeEventListener(e.type, changeColor);
})
object,button {  
display: inline-block;
margin:.5em;
width: 100px;
height: auto;
border:1px solid #d9d9d9;
}
<div>
<object id="svgContent" type="image/svg+xml"
data="https://s3-us-west-2.amazonaws.com/s.cdpn.io/222579/cat.svg#redcat">
</object>
</div>
<p><button id="black">black cat</button></p>

查看更多
登录 后发表回答