【问题标题】:GreenSock TimelineMax on path elements not working in Safari and Firefox路径元素上的 GreenSock TimelineMax 在 Safari 和 Firefox 中不起作用
【发布时间】:2016-08-23 21:43:50
【问题描述】:

我正在尝试为我在 JavaScript/jQuery 中创建和附加的几个路径元素设置动画。然后我使用时间轴来为这些元素一一设置动画(使用不同的值,所以我不能使用交错)。最后,我不想播放完整的时间线,而只想播放其中的一部分。

这在 Chrome 中工作得很好,但在 Safari 和 Firefox 中却不行,我不知道为什么。

这是一个 CodePen: http://codepen.io/elisabeth_hamel/pen/kXqOmw

编辑: CodePen 已更新,现在可以正常工作了。

这里是代码:

HTML

<svg xmlns='http://www.w3.org/2000/svg' width='20' height='10' viewBox='0 0 2 1' preserveAspectRatio='xMinYMid meet'></svg>

CSS

svg{
    width: 100%;
    position: absolute;
    top: 0;
    bottom: 0;
    left: 60px;
    margin: auto;
    overflow: visible;
    z-index: 1;
    .up{
        -webkit-transform: translate3d(0, 40px, 0);
        transform: translate3d(0, 40px, 0);
    }
    .down{
        -webkit-transform: translate3d(0, 40px, 0);
        transform: translate3d(0, -40px, 0);
    }
}

JS

$(function(){
    var trianglesTimeline = new TimelineMax({paused: true, smoothChildTiming: true});

    function setAnimations(){
        var nbTriangles, i = 0, svg = '', random = 1, thisPath;

        nbTriangles = ($(window).width() - 60)/9 | 0;
        for(i; i<nbTriangles; i++){
            random = (Math.random()*(2.5-0.5) + 0.5).toFixed(1);
            if(i%2 === 0){
                svg += "<path fill='#000' d='M0 0H2 L1 1Z' class='down' data-op='"+random+"' data-x='"+i+"' style='opacity:0'/>";
            }else{
                svg += "<path fill='#000' d='M0 1H2 L1 0Z' class='up' data-op='"+random+"' data-x='"+i+"' style='opacity:0'/>";
            }
        }
        $('svg').html(svg);

        i = 0;
        for(i; i<nbTriangles; i++){
            thisPath = $('path').eq(i);
            TweenMax.set(thisPath, {x: thisPath.data('x')}, 0);
            trianglesTimeline.to(thisPath, 0.3, {opacity: thisPath.data('op'), y: '0px', delay: 0.04*i}, 0);
        }
        trianglesTimeline.tweenTo( trianglesTimeline.duration() * 0.1 );
    }

    setAnimations();
});

任何帮助将不胜感激!

【问题讨论】:

    标签: javascript animation svg gsap


    【解决方案1】:

    好吧,原来 CSS 规则转换规则覆盖了 TweenMax 在路径元素上设置的转换。我不知道为什么!

    所以我用内联转换替换了这些类。如果有人感兴趣,这是新的工作代码:

    CSS

    svg{
        width: 100%;
        position: absolute;
        top: 0;
        bottom: 0;
        left: 60px;
        margin: auto;
        overflow: visible;
        z-index: 1;
    }
    

    JS

    $(function(){
    
        var trianglesTimeline = new TimelineMax({paused: true, smoothChildTiming: true});
    
        function setAnimations(){
            var nbTriangles, i = 0, svg = '', random = 1, thisPath;
    
            nbTriangles = ($(window).width() - 60)/9 | 0;
            for(i; i<nbTriangles; i++){
                random = (Math.random()*(2.5-0.5) + 0.5).toFixed(1);
                if(i%2 === 0){
                    svg += "<path fill='#000' d='M0 0H2 L1 1Z' transform='translate("+i+", 40)' data-op='"+random+"' style='opacity:0'/>";
                }else{
                    svg += "<path fill='#000' d='M0 1H2 L1 0Z' transform='translate("+i+", -40)' data-op='"+random+"' style='opacity:0'/>";
                }
            }
            $('svg').html(svg);
    
            i = 0;
            for(i; i<nbTriangles; i++){
                thisPath = $('path').eq(i);
                trianglesTimeline.to(thisPath, 0.3, {opacity: thisPath.data('op'), y: '0px', delay: 0.04*i}, 0);
            }
            trianglesTimeline.tweenTo( trianglesTimeline.duration() * 0.1 );
        }
    
        setAnimations();
     });
    

    编辑

    为了让它在 IE 中工作,我做了一些其他的修改:

    $(function(){
        function makeSVG(tag, attrs){
            var el = document.createElementNS('http://www.w3.org/2000/svg', tag), k;
            for(k in attrs){
                el.setAttribute(k, attrs[k]);
            }
            return el;
        }
    
        var trianglesTimeline = new TimelineMax({paused: true, smoothChildTiming: true});
    
        function setAnimations(){
            var nbTriangles, i = 0, svg = '', random = 1, thisPath, y, d;
    
            nbTriangles = ($(window).width() - 60)/9 | 0;
            for(i; i<nbTriangles; i++){
                random = (Math.random()*(2.5-0.5) + 0.5).toFixed(1);
                if(i%2 === 0){
                    y = 40;
                    d = 'M0 0H2 L1 1Z';
                }else{
                    y = -40;
                    d = 'M0 1H2 L1 0Z';
                }
                svg = makeSVG('path', {fill: '#000', d: d, transform:'translate('+i+', '+y+')', 'data-op': random, style: 'opacity:0'});
                $('svg').append(svg);
            }
    
            i = 0;
            for(i; i<nbTriangles; i++){
                thisPath = $('path').eq(i);
                trianglesTimeline.to(thisPath, 0.3, {opacity: thisPath.data('op'), y: '0px', delay: 0.04*i}, 0);
            }
            trianglesTimeline.tweenTo( trianglesTimeline.duration() * 0.1 );
        }
    
        setAnimations();
    });    
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-11-13
      • 2017-01-02
      • 1970-01-01
      • 2018-03-09
      • 2021-10-19
      • 1970-01-01
      相关资源
      最近更新 更多