janpaepke / ScrollMagic

The javascript library for magical scroll interactions.
http://ScrollMagic.io
Other
14.88k stars 2.17k forks source link

ScrollMagic is not defined [using webpack] #771

Open glennphilp opened 6 years ago

glennphilp commented 6 years ago

Repo with issue: https://gphilp@bitbucket.org/gphilp/ihatetomatoes.git

Error: Uncaught ReferenceError: ScrollMagic is not defined

Any help resolving this would be greatly appreciated.

I thought the error might have had something to do with jQuery. So I tested ScrollMagic.Controller outside of any jQuery reference and still get the same not defined error.

Is the issue created because the new ScrollMagic.Controller triggering before ScrollMagic has fully loaded? This shouldn't be the issue though since ScrollMagic and plugins come before the Controller init.

So far I have successfully added ScrollMagic, animation.gsap, and debug with webpack by using the alias config within the gulpfile.

Site structure:

src
- assets
-- js
--- app.js [imports]
--- libs
---- iht-awwwards.js [custom javascript]
---- scrollmagic.js [import scrollmagic and plugins]
1-800-jono commented 6 years ago

This is probably because your custom js code is above ScrollMagic library. In your HTML it should be: <script type='text/javascript' src='/ScrollMagic.min.js> <script type='text/javascript' src='/your-custom-code.js></script>

glennphilp commented 6 years ago

I appreciate the help. Unfortunately, this doesn't resolve the issue.

See I am trying to incorporate ScrollMagic and GSAP with Webpack. I have been successful at getting Webpack to include ScrollMagic in the build.js. And according to my Webpack file I included my ScrollMagic calls after ScrollMagic.

I have even gone as far as removing my custom.js from the Webpack build and having two separate files: libs.js and init.js.

However, I still get the same error when calling: <script src="js/libs.js"></script> // file contains in order jquery, gsap, scrollmagic <script src="js/init.js"></script> // file contains var newScene = new ScrollMagic.Scene();

glennphilp commented 6 years ago

@OfficialYoungX, thank you for your input. I too realize I can add GSAP and ScrollMagic with script tags and CDN. This isn't my goal. My goal is to have it included in my build process for a single JS file.

jleifeld commented 6 years ago

You can import the library directly in the file and it works perfectly:

// Import ScrollMagic library
import ScrollMagic from 'scrollmagic';

// init controller
var controller = new ScrollMagic.Controller();

// create a scene
new ScrollMagic.Scene({
  duration: 1400, //scroll distance for scene
  offset: 900 //start this scene after scrolling for offset
})
.setPin("#some-div") //pins the element for the scenes duration
.addTo(controller);
denisinla commented 6 years ago

Review the following > https://github.com/janpaepke/ScrollMagic/issues/324

HadiNiazi commented 4 years ago

Yeah! I got the solution. insert these code in your html file.

This works for me. @random-yang Thanks

Monika39-maker commented 3 years ago

Worked for me tooo...thanks youuuuuuu