ScrollMagic + GSAP gives me “window is not defined” on "gridsome build"
- Dominant language
- JavaScript
- Stars
- 8.5k
- Forks
- 466
- PR merge metrics
- No merged PRs in 30d
Description
After installing both and combining them to work together, ScrollMagic gives me a "window is not defined" error. I've already tried many solutions, but I'm still struggling with it. Can someone help me, please? Or suggest a good alternative for ScrollMagic+GSAP?
([Link](https://github.com/OziOcb/pozyzniewski-portfolio/tree/scrollmagic-issue) to the repo on GitHub.)
I installed them via yarn.
`yarn add scrollmagic gsap`
In main.js I added:
(I followed [this](https://github.com/pirony/ks-vue-scrollmagic/issues/13))
```javascript
/* GSAP & ScrollMagic */
import ScrollMagic from "scrollmagic"
import "imports-loader?define=>false!scrollmagic/scrollmagic/uncompressed/plugins/animation.gsap"
// Load ScrollMagic's Indicators only on development environment
if (process.env.NODE_ENV === "development") {
require("imports-loader?define=>true!scrollmagic/scrollmagic/uncompressed/plugins/debug.addIndicators")
}
const GSAPScrollMagic = {
install(Vue, options) {
// GSAP
Vue.prototype.$GSAP = {
TweenMax,
TimelineMax,
Linear,
Power1,
Power2,
Power3,
Back
}
// ScrollMagic
Vue.prototype.$ScrollMagic = {
Controller: new ScrollMagic.Controller(),
Scene: ScrollMagic.Scene
}
}
}
export default function(Vue, { router, head, isClient }) {
// Add GSAP & ScrollMagic to the project
Vue.use(GSAPScrollMagic)
}
```
And in my pages/index.vue file i simply add it like this:
```javascript
export default {
...
mounted() {
// create a Tween
const testTween = new this.$GSAP.TimelineMax()
testTween
.from(".smElement", 1.5, { ease: this.$GSAP.Power2.easeOut, opacity: 0 })
// create a scene
const testScene = new this.$ScrollMagic.Scene({
triggerElement: ".smElement",
triggerHook: 0.7,
duration: 300
})
.addIndicators()
.setTween(testTween)
// Add the scene to the controller
this.$ScrollMagic.Controller.addScene(testScene)
}
}
```
everything works great on 'gridsome develop'.
On 'gridsome build' I get this error:
```bash
Gridsome v0.7.10
Initializing plugins...
Load sources - 0.06s
Create GraphQL schema - 0.09s
Create pages and templates - 0.05s
Generate temporary code - 0.06s
Bootstrap finish - 1.92s
10% building 1/1 modules 0 active
Update available: 0.3.1
Run yarn add global @gridsome/cli to update
Compile assets - 20.38s
Execute GraphQL (6 queries) - 0.02s
Write out page data (6 files) - 0s
Could not generate HTML for "/blog/test/":
ReferenceError: window is not defined
at Object. (node_modules/scrollmagic/scrollmagic/uncompressed/ScrollMagic.js:37:0)
at assets/js/app.a4f12461.js:3124:37
at Object. (node_modules/scrollmagic/scrollmagic/uncompressed/ScrollMagic.js:27:1)
at __webpack_require__ (webpack/bootstrap:25:0)
at Module. (assets/js/app.a4f12461.js:12578:19)
at __webpack_require__ (webpack/bootstrap:25:0)
at Object. (assets/js/app.a4f12461.js:8588:18)
at __webpack_require__ (webpack/bootstrap:25:0)
at assets/js/app.a4f12461.js:118:18
at Object. (assets/js/app.a4f12461.js:121:10)
```
#
I've found [a similar issue with ScrollMagic on Nuxt.js](https://stackoverflow.com/questions/47844836/nuxtjs-with-scrollmagic-gives-me-window-is-not-defined), but I'm not sure how to convert the solution to Gridsome.
I've also tried to follow [the official docs for adding external script without SSR support](https://gridsome.org/docs/assets-scripts/#without-ssr-support), with no luck...
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.