-3

I was trying to use ScrollMagic but it is not working when I inspect element it is giving this

error255 Uncaught ReferenceError: $ is not defined

Even tho I've included all the libraries in the header and the actual script on the bottom of the page.

Header

<script src="js/ScrollMagic.min.js"></script>
<script src="js/debug.addIndicators.min.js"></script>
<script src="js/animation.gsap.min.js"></script>
<script src="js/animation.velocity.min.js"></script>

Script

<script>
    $(function () { // wait for document ready
      // init
      var controller = new ScrollMagic.Controller();

      // define movement of panels
      var wipeAnimation = new TimelineMax()
        .fromTo("section.panel.pink", 1, {x: "-100%"}, {x: "0%", ease: Linear.easeNone})  // in from left
        .fromTo("section.panel.green", 1, {x: "100%"}, {x: "0%", ease: Linear.easeNone})  // in from left
        .fromTo("section.panel.red", 1, {x: "-100%"}, {x: "0%", ease: Linear.easeNone})  // in from left
        .fromTo("section.panel.blue", 1, {x: "-100%"}, {x: "0%", ease: Linear.easeNone})  // in from left
        .fromTo("section.panel.white", 1, {x: "-100%"}, {x: "0%", ease: Linear.easeNone})  // in from left

      // create scene to pin and link animation
      new ScrollMagic.Scene({
          triggerElement: "#designPart",
          triggerHook: "onLeave",
          duration: "300%"
        })
        .setPin("#designPart")
        .setTween(wipeAnimation)
        .addIndicators() // add indicators (requires plugin)
        .addTo(controller);
    });
</script>

The section where I'm using it.

<div id="designPart">
  <p>Design</p>
  <section class="panel pink">
    <img src="images/pink.png">
  </section>
  <section class="panel green">
    <img src="images/green.png">
  </section>
  <section class="panel red">
    <img src="images/red.png">
  </section>
  <section class="panel blue">
    <img src="images/blue.png">
  </section>
  <section class="panel white">
    <img src="images/white.png">
  </section>
</div><!--designPart-->
Радж
  • 9
  • 5
  • 1
    Possible duplicate of [Uncaught ReferenceError: $ is not defined?](https://stackoverflow.com/questions/2075337/uncaught-referenceerror-is-not-defined) – HoldOffHunger Oct 02 '18 at 21:34

2 Answers2

1

You do not seem to be loading the jQuery library itself (and ScrollMagic uses jQuery) - and the error message is simply telling you that there is no jQuery object to reference with the "$" variable

Make sure that you load the jQuery PRIOR to the ScrollMagic, so that the jQuery namespace exists before ScrollMagic requires it, as Snowmonkey states in the comments below.

You need to either load jQuery from a local source eg:

<script src="js/jquery-2.1.1.js"></script>

or from a cdn

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

One advantage of loading a common library like jQuery from a CDN (Content Delivery Network) is that there is a fair chance that users will already have it cached in their browsers from other web activity and if it is cached, the browser does not need to load it again.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
gavgrif
  • 15,194
  • 2
  • 25
  • 27
  • After I adding local jquery from Scrollmagic, it shows these errors jquery.ScrollMagic.min.js:2 Uncaught TypeError: Cannot set property 'ScrollMagic' of undefined at jquery.ScrollMagic.min.js:2 at jquery.ScrollMagic.min.js:2 at jquery.ScrollMagic.min.js:2 (anonymous) @ jquery.ScrollMagic.min.js:2 (anonymous) @ jquery.ScrollMagic.min.js:2 (anonymous) @ jquery.ScrollMagic.min.js:2 – Радж Oct 02 '18 at 21:14
  • 1
    put that jquery loading line BEFORE the scrollmagic loading line. Its trying to stick scrollmagic into the jquery namespace, which hasn't been created yet. – Snowmonkey Oct 02 '18 at 21:21
  • 1
    Thanks @Snowmonkey, I would have said the same thing. I should have put that into my solution. Cheers – gavgrif Oct 02 '18 at 21:25
  • 1
    I might suggest you edit your answer anyway, for the sake of completeness, @gavgrif. – Snowmonkey Oct 02 '18 at 21:28
  • Thanks guys for helping out but I try to put jquery loading before the Scrollmagic.min.js it is giving these errors again. **Cannot read property '_util' of undefined at jquery.ScrollMagic.min.js:2 at jquery.ScrollMagic.min.js:2 at jquery.ScrollMagic.min.js:2 (anonymous) @ jquery.ScrollMagic.min.js:2 (anonymous) @ jquery.ScrollMagic.min.js:2 (anonymous) @ jquery.ScrollMagic.min.js:2 Index.html:258 Uncaught ReferenceError: $ is not defined** I'm sorry I am very new to this – Радж Oct 02 '18 at 21:29
  • Do you have all the required files and plugins - eg – gavgrif Oct 02 '18 at 21:38
  • Yes I have added all the required plugins ** ** – Радж Oct 02 '18 at 21:41
0

$(function() {
      
  var controller = new ScrollMagic.Controller();

  var wipeAnimation = new TimelineMax()
  .fromTo("section.panel.pink",  1, {x: "100%"}, {x: "0%", ease: Linear.easeNone})
  .fromTo("section.panel.green", 1, {x: "100%"}, {x: "0%", ease: Linear.easeNone})
  .fromTo("section.panel.red",   1, {x: "100%"}, {x: "0%", ease: Linear.easeNone})
  .fromTo("section.panel.blue",  1, {x: "100%"}, {x: "0%", ease: Linear.easeNone})
  .fromTo("section.panel.white", 1, {x: "100%"}, {x: "0%", ease: Linear.easeNone});

  // create scene to pin and link animation
  new ScrollMagic.Scene({
      triggerElement: "#designPart",
      triggerHook: "onLeave",
      duration: "300%"
  })
  .setPin("#designPart")
    .setTween(wipeAnimation)
    .addIndicators()
    .addTo(controller);
});
section img {
  height: 32px;
  width: 32px;
}
<!-- those are all the libraries required:-->
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/ScrollMagic/2.0.5/ScrollMagic.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/ScrollMagic/2.0.5/plugins/animation.velocity.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/ScrollMagic/2.0.5/plugins/animation.gsap.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/ScrollMagic/2.0.5/plugins/debug.addIndicators.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/gsap/2.0.2/TweenMax.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/gsap/2.0.2/TimelineMax.min.js"></script>

<div id="designPart">
  <p>Design</p>
  <section class="panel pink">
    <img src="https://www.sqlite.org/images/nocopy.gif">
  </section>
  <section class="panel green">
    <img src="https://www.sqlite.org/images/nocopy.gif">
  </section>
  <section class="panel red">
    <img src="https://www.sqlite.org/images/nocopy.gif">
  </section>
  <section class="panel blue">
    <img src="https://www.sqlite.org/images/nocopy.gif">
  </section>
  <section class="panel white">
    <img src="https://www.sqlite.org/images/nocopy.gif">
  </section>
</div>
Martin Zeitler
  • 1
  • 19
  • 155
  • 216
  • @Радж click "run code snippet"; it animates on scroll and CDN libraries used. even added images now. and actually, without `$(function(){});` you wouldn't even need jQuery `$`. – Martin Zeitler Oct 04 '18 at 23:44