动态加载的SVG动画在Chrome中运行,但不适用于Firefox和Safari

时间:2014-04-21 10:37:14

标签: jquery google-chrome firefox svg safari

我的页面中有一个SVG元素,我使用JQuery动态加载随机创建的方块,并在其中设置动画。 这完全符合我在Chrome中的要求,但我无法在Firefox和Safari中使用它。我还没有在IE中测试过。

在这里你可以测试动画,在Chrome中它运行正常: http://jsfiddle.net/Koendema/Fe8Jk/15/

有什么想法吗?

HTML:

<header>
    <svg class="svgHeader" style="width:400; height:300; background: #000;">
    </svg>
</header>

JQuery的:

animateBg('header', 'svgHeader', 20);  


function animateBg(parentName, svgClass, totalRects){

    //var svgG = $('.' + svgClass + ' g');
    var svgG = $('.' + svgClass);
    var minSize = 10; //min size square
    var maxSize = 85; //max size square
    var widthSvg = $('.' + svgClass).innerWidth();
    var heightSvg =  $('.' + svgClass).innerHeight();

    $('.' + svgClass).attr('height', $(parentName).innerHeight());

    for (i=1;i <= totalRects;i++){

        var randomSize = getRandomRange(minSize, maxSize);
        var randomLeft = getRandomRange(randomSize, widthSvg);//to calculate a random x from position later
        var randomOpacity = (Math.random() * 1).toFixed(1);
        var randomDur = getRandomRange(8, 30); //duration of the animation
        var yFrom = getRandomRange(randomSize, heightSvg);
        var yTo = getRandomRange(randomSize, heightSvg);

        if (i <= (totalRects / 2)) {
            var xFrom = 0 - randomLeft - randomSize;
            var xTo = widthSvg;
        } else {
            var xFrom = widthSvg + randomLeft + randomSize; 
            var xTo = 0 - randomSize;
        }

        $(svg("rect"))
            .attr('id', svgClass + 'Rect' + i)
            .attr('width', randomSize)
            .attr('height', randomSize)
            .attr('x', xFrom)
            .attr('y', yFrom)
            .css({
                opacity:randomOpacity,
                fill:'#ab2469',
                stroke:'#ab2469'
            })
            .html(
                '<animate ' +
                'attributeName="x"' +
                'attributeType="XML"' +
                'from="' + xFrom + '"' +
                'to="'+ xTo + '" ' +
                'begin="0s" dur="' + randomDur + 's" ' +
                'repeatCount="indefinite" ' +
                 '/>' +
                '<animate attributeName="y" ' +
                'attributeType="XML" ' +
                'from="' + yFrom + '"' + 
                'to="' + yTo + '" ' +
                'begin="0s" dur="' + randomDur + 's" ' +
                'repeatCount="indefinite" ' +
                '/>'
            )
            .appendTo(svgG);
    }
}

function svg(tag)
{
   return document.createElementNS('http://www.w3.org/2000/svg', tag);
}

function getRandomRange(min, max){
    return Math.floor((Math.random() * (max - min) + min));
}

我终于通过使用命名空间以及动画来实现它。 我不得不用Javascript替换一些JQuery:

http://jsfiddle.net/Koendema/7nURy/7/

2 个答案:

答案 0 :(得分:0)

您的<animate>元素名称空间错误。它们将作为FF中的xhtml命名空间而不是SVG命名空间。从Firebug中的DOM选项卡:

localName       "animate"
namespaceURI    "http://www.w3.org/1999/xhtml"

您需要以与创建<rect>元素相同的方式创建它们。

答案 1 :(得分:0)

我终于通过使用命名空间以及动画来实现它。我不得不用Javascript替换一些JQuery:

http://jsfiddle.net/Koendema/7nURy/7/

function animateBg(parentName, svgId, totalRects){

//var svgG = $('.' + svgClass + ' g');
var svgG = $('#' + svgId);
var minSize = 10; //min grootte vierkant
var maxSize = 85; //max grootte vierkant

$('#' + svgId).css('height', $(parentName).innerHeight());
var widthSvg = $('#' + svgId).innerWidth();
var heightSvg =  $('#' + svgId).innerHeight();

$('#' + svgId).css('height', $(parentName).innerHeight());

for (i=1;i <= totalRects;i++){

    var randomSize = getRandomRange(minSize, maxSize);
    var randomLeft = getRandomRange(randomSize, widthSvg);//om later een random x from positie te berekenen
    var randomOpacity = (Math.random() * 1).toFixed(1);
    var randomDur = getRandomRange(8, 30); //duration van de animatie
    var yFrom = getRandomRange(randomSize, heightSvg);
    var yTo = getRandomRange(randomSize, heightSvg);

    if (i <= (totalRects / 2)) {
        var xFrom = 0 - randomLeft - randomSize;
        var xTo = widthSvg;
    } else {
        var xFrom = widthSvg + randomLeft + randomSize; 
        var xTo = 0 - randomSize;
    }

    var rect = svg("rect");
    var animateX = svg("animate");
    var animateY = svg("animate");

    rect.setAttributeNS(null,'id', svgId + 'Rect' + i)
    rect.setAttributeNS(null, 'width', randomSize)
    rect.setAttributeNS(null, 'height', randomSize)
    rect.setAttributeNS(null, 'x', xFrom)
    rect.setAttributeNS(null, 'y', yFrom)
    rect.setAttributeNS(null, 'style', 'opacity:' + randomOpacity + ';' +
                                        'fill: #ab2469;'  )

    animateX.setAttributeNS(null, 'attributeName', 'x')
    animateX.setAttributeNS(null, 'attributeType', 'XML')
    animateX.setAttributeNS(null, 'from', xFrom)
    animateX.setAttributeNS(null, 'to', xTo)
    animateX.setAttributeNS(null, 'begin', '0s')
    animateX.setAttributeNS(null, 'dur', randomDur + 's')
    animateX.setAttributeNS(null, 'repeatCount', 'indefinite'); 

    animateY.setAttributeNS(null, 'attributeName', 'y')
    animateY.setAttributeNS(null, 'attributeType', 'XML')
    animateY.setAttributeNS(null, 'from', yFrom)
    animateY.setAttributeNS(null, 'to', yTo)
    animateY.setAttributeNS(null, 'begin', '0s')
    animateY.setAttributeNS(null, 'dur', randomDur + 's')
    animateY.setAttributeNS(null, 'repeatCount', 'indefinite');         

    rect.appendChild(animateX);
    rect.appendChild(animateY);
    document.getElementById(svgId).appendChild(rect);
}
}