Is it possible to listen image load event in SVG?

前端 未结 2 1383
别跟我提以往
别跟我提以往 2021-02-13 04:25

Is it possible to listen for an load event in SVG? If yes, how to do this?

2条回答
  •  滥情空心
    2021-02-13 05:19

    I found that this would not work for SVG object created using D3, but the answer here worked great:

    How can I display a placeholder image in my SVG until the real image is loaded?

    For example this worked:

    var img = innerG.append("image")
        .attr('onload', function() {
            console.log('loaded');
        })
        .attr("xlink:href", src)
        .attr("width", size)
        .attr("height", size);
    

    But this did not work:

    var img = innerG.append("image")
        .attr("xlink:href", src)
        .attr("width", size)
        .attr("height", size);
    
    img.addEventListener('load', function() { console.log('loaded'); });
    

提交回复
热议问题