javascriptjqueryscrollmagicgasp

scrollmagic not responding to the trigger element


I have problems with the ScrollMagic. It's not responding at all on the trigger element. Below I'll provide with the code:

the CSS:

.container {
    height: 3000px;
}

#trigger {
    position: relative;
    top: 300px;
}

.katrori {
    opacity: 0; 
    position:relative; 
    top:300px; 
    background-color:#eee; 
    height:400px; 
    padding:25px; 
    font-family:Arial, Sans-serif;
    font-weight:bold;
}

and the JS:

$(document).ready(function($)) {
    var controller = new ScrollMagic();
    var tween = TweenMax.to(".katrori", 0.5, {opacity: 1, backgroundColor: "#1d1d1d"})
    var scene = new ScrollScene({triggerElement: "#trigger"})
    .setTween(tween)
    .addTo(controller);
});

what am I missing?


Solution

  • Your JS has mainly two errors.

    1. You have one parenthesis (")") too many.

      $(document).ready(function($)) {
                                  ^^ --> one of those
      
    2. You are using ScrollMagic version >=2 (which you should) but use their functions from version 1. Here is their documentation for the current versions.

      The correct way to initialize the container and scene is now:

      var container = new ScrollMagic.Container({...});
      var scene = new ScrollMagic.Scene({...});
      

    When you apply these changes a working example of your code might look like this:

    $(document).ready(function ($) {
        var controller = new ScrollMagic.Controller(),
            tween = TweenMax.to(".katrori", 0.5, {opacity: 1, backgroundColor: "#1d1d1d"}),
            scene = new ScrollMagic.Scene({triggerElement: "#trigger"});
    
        scene
            .setTween(tween)
            .addTo(controller);
    });
    

    You might also want to look at their examples.

    EDIT

    Addition to bulletpoint 2:

    In ScrollMagic version 1 the container and scene were initialized in the script this way:

    var controller = new ScrollMagic({ *global options applying to all scenes added*});
    var scene = new ScrollScene({ *options for the scene* })
    

    In version 2 the same thing is done this way:

    var container = new ScrollMagic.Container({...});
    var scene = new ScrollMagic.Scene({...});
    

    That's why you script didn't work before. The styling is still done in CSS.