Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

use the same class to animate on view with scrollmagic

Tags:

scrollmagic

I am pretty new to scrollmagic and i am looking to use a single class multiple times throughout a page to animate when it comes into view. Is this possible?

see pen

Any pointers welcome.

$(function() {
    controller = new ScrollMagic();
    var tween5 = TweenMax.to(".animate", 0.5,{scale: 1.02,
    backgroundColor: 'rgb(255, 39, 46)'
    });
    var scene5 = new ScrollScene({
            duration: 200,
            triggerElement: ".animate",
            triggerHook: "onCenter",
        })
        .setTween(tween5)
        .addTo(controller);

      scene5.addIndicators();
});
like image 231
hamish mckay Avatar asked Jan 30 '15 20:01

hamish mckay


1 Answers

To know how to solve this you need to know how the two frameworks react to multiple elements being supplied (which is essentially what you do if you supply a class that resolves to more than one element).

TweenMax will animate all elements at the same time, while ScrollMagic can only have one trigger element per scene (because there can only be one start and end per scene), which is why it will use the first element of the matched set.

So logically your above code results in all the elements being animated, as soon as the first one passes the trigger.

To solve this you'll have to define a scene for each element:

$(function() {
    controller = new ScrollMagic();
    $(".animate").each(function (index, elem) {
        var tween = TweenMax.to(elem, 0.5,
                               {scale: 1.02, backgroundColor: 'rgb(255, 39, 46)' }
                    );
        new ScrollScene({
                duration: 200,
                triggerElement: elem,
                triggerHook: "onCenter",
            })
            .setTween(tween)
            .addTo(controller)
            .addIndicators();
    });
});

Updated pen: http://codepen.io/janpaepke/pen/JoygRd

like image 88
Jan Paepke Avatar answered Oct 26 '22 16:10

Jan Paepke