Animating shape using snap.svg and scrollmagic

爷,独闯天下 提交于 2019-12-14 03:17:37

问题


I am trying to create a code to animate my header as the user scrolls. I have managed to create the animations I want using 2 different plugins: snap.svg and scrollmagic. I'm new to both of these so please excuse my noobiness.

Here it is on CodePen: http://codepen.io/anon/pen/dYbPXe

/* Animation 1 */

                    var speed = 250,
                    easing = mina.easeinout;

                [].slice.call ( document.querySelectorAll( '.header > a' ) ).forEach( function( el ) {
                    var s = Snap( el.querySelector( 'svg' ) ), path = s.select( 'path' ),
                        pathConfig = {
                            from : path.attr( 'd' ),
                            to : el.getAttribute( 'data-path-hover' )
                        };

                    el.addEventListener( 'mouseenter', function() {
                        path.animate( { 'path' : pathConfig.to }, speed, easing );
                    } );

                    el.addEventListener( 'mouseleave', function() {
                        path.animate( { 'path' : pathConfig.from }, speed, easing );
                    } );
                } );


/* Animation 2 */
var scrollMagicController = new ScrollMagic();
var tween = TweenMax.to('.header-shape > path', 0.5, {
    fill: 'rgb(255, 39, 46)'    
});

var scene = new ScrollScene({
    triggerElement: '.background',
    triggerHook: '0',
    duration: 400
}).setTween(tween).addTo(scrollMagicController);
scene.addIndicators();

Right now, if you scroll the page, the color of the header changes, and if you hover the header, the shape changes. I want to bind the shape change to scroll, so as users scroll, the shape gradually changes as well as the color.

This might be a simple (maybe unnecessary) thing but I've put wayy too much time into it and it's frustrating me. Thanks for your help in advance.

EDIT: Please let me know if my question is not clear and I will try to clarify. I really want this effect :(


回答1:


Here's what I figured out: http://codepen.io/ironoxidey/pen/WrXLwM

This seems to be all you need:

var tween = TweenMax.to('.header-shape > path', 0.5, {
    fill: 'rgb(255, 39, 46)',
    attr: { d : $('a').attr('data-path-hover') }
});

(started on line 25 of your JS code, under Animation 2)



来源:https://stackoverflow.com/questions/32234007/animating-shape-using-snap-svg-and-scrollmagic

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!