简体   繁体   English

如何在 nuxt 项目中添加 ScrollMagic?

[英]How add ScrollMagic in nuxt project?

I am trying to add ScollMagic in my Nuxt project, I followed this article: https://nuxtjs.org/guide/plugins & I've added ScrollMagic.js in nuxt.config.js and I got this issue: ReferenceError window is not defined.我正在尝试在我的 Nuxt 项目中添加 ScollMagic,我遵循了这篇文章: https://nuxtjs.org/guide/plugins并且我在 nuxt.config.js 中添加了 ScrollMagic.js,我遇到了这个问题:ReferenceError window 是没有定义的。

module.exports = {
 plugins: [
    { src: '~plugins/ScrollMagic', mode: 'client' }
  ],
}

Then I've added this snippet in my component:
import ScrollMagic from 'scrollmagic'

I still have this issue...我还有这个问题...

Even if cover it like this:即使像这样覆盖它:

if (process.client) {
  Vue.use(ScrollMagic)
}

I know this question is a bit old, but maybe someone will find my sulotion helpfull.我知道这个问题有点老了,但也许有人会发现我的 solotion 很有帮助。

EDIT: Also, this way you can register any plugin to nuxt:).编辑:另外,通过这种方式,您可以将任何插件注册到 nuxt :)。

So what I did:所以我做了什么:

1. npm i scrollmagic 1. npm i scrollmagic

  1. Go to nuxt.config.js and add the following to your plugins section: Go 到 nuxt.config.js 并将以下内容添加到您的插件部分:

{ src: '~/plugins/ScrollMagic.js', mode: 'client' }

This will make sure that scrollmagic will only be included in the client.这将确保 scrollmagic 仅包含在客户端中。 This prevents the window undefined error.这可以防止window undefined错误。

  1. Go to the plugins folder, or make it in your root folder and make a new file called ScrollMagic.js . Go 到plugins文件夹,或者在你的根文件夹中创建一个名为ScrollMagic.js的新文件。 In here add the following:在这里添加以下内容:
import Vue from 'vue';
import * as ScrollMagic from 'scrollmagic';

Object.defineProperty(Vue.prototype, '$scrollmagic', { value: ScrollMagic });

This snippet will make the variable $scrollmagic available in every component/page.此代码段将使变量 $scrollmagic 在每个组件/页面中可用。 You can call this.$scrollmagic to use it.你可以调用this.$scrollmagic来使用它。 For example const controller = new this.$scrollmagic.Controller();例如const controller = new this.$scrollmagic.Controller();

Hope this helps anyone.希望这对任何人都有帮助。

currently what works is to use the cdn and integrate in 'nuxt.config.js'.目前可行的是使用cdn并集成到'nuxt.config.js'中。 Like this:像这样:

      head: {
...
script: [
  {
    src: "https://cdnjs.cloudflare.com/ajax/libs/ScrollMagic/2.0.7/ScrollMagic.min.js",
    async: 'async',
    ssr: false,
    defer: true,
    body: true,
  },
  {
    src: "https://cdnjs.cloudflare.com/ajax/libs/ScrollMagic/2.0.7/plugins/debug.addIndicators.min.js",
    async: 'async',
    ssr: false,
    defer: true,
    body: true,
  },
] },

in the component :组件中:

export default {
mounted () {
  if (process.client) {
    var controller = new ScrollMagic.Controller()
    var scene = new ScrollMagic.Scene({ triggerElement: '#trigger1' })

      // exemple : trigger animation by adding a css class

      .setClassToggle('#animate1', 'zap')
      .addIndicators({ name: '1 - add a class' }) // add indicators (requires plugin)
      .addTo(controller)
  }
}}

you can use this method for other puglins that wouldn't work on nuxt by using npm or [puglins]您可以通过使用 npm 或 [puglin] 将此方法用于其他无法在 nuxt 上工作的 puglin

This is answering Drew Baker.这是回答德鲁贝克。

I think you are making it more complicated than it should be.我认为你让它变得比它应该的更复杂。 Using GSAP with Nuxt is fairly simple.将 GSAP 与 Nuxt 一起使用非常简单。

  1. npm install gsap in your terminal. npm install gsap
  2. In the file you want to animate something, import gsap.在您想要制作动画的文件中,导入 gsap。 In the script tag: import { gsap } from 'gsap' .在脚本标签中: import { gsap } from 'gsap'
  3. use GSAP like you're used too.像你一样使用 GSAP。 const lt = gsap.timeline(); . .

If you still want to use GSAP as I explained in the other comment:如果您仍想按照我在另一条评论中解释的那样使用 GSAP:

  1. Follow the steps 1 & 2 from the other comment .按照其他评论中的步骤 1 和 2 进行操作。
  2. In step 3 of the previous comment, create a file called 'gsap.js' in there import Vue and GSAP.在上一条评论的第 3 步中,在其中创建一个名为“gsap.js”的文件,导入 Vue 和 GSAP。
import Vue from 'vue';
import { gsap } from 'gsap';

Object.defineProperty(Vue.prototype, '$gsap', { value: gsap });

Hope this helps you @Drew Baker!希望这可以帮助你@Drew Baker!

use the cdn and integrate in 'nuxt.config.js'.使用 cdn 并集成到“nuxt.config.js”中。

script: [
  {
    src: "https://cdnjs.cloudflare.com/ajax/libs/ScrollMagic/2.0.7/ScrollMagic.min.js",
    async: 'async',
    ssr: false,
    defer: true,
    body: true,
  },
  {
    src: "https://cdnjs.cloudflare.com/ajax/libs/ScrollMagic/2.0.7/plugins/debug.addIndicators.min.js",
    async: 'async',
    ssr: false,
    defer: true,
    body: true,
  },
 {
    src: "https://cdnjs.cloudflare.com/ajax/libs/ScrollMagic/2.0.7/plugins/animation.gsap.min.js",
    async: 'async',
    ssr: false,
    defer: true,
    body: true,
  },
] },

In component在组件中

Anim() {
    if (process.browser && window) {
      const ScrollMagic = window.ScrollMagic;
      const bookTimeline = new TimelineMax({});
      bookTimeline
        .from(".hmbookservice-container", 0.6, {
          yPercent: 20,
          opacity: 0,
          ease: Power4.easeOut,
          clearProps: "all"
        })
        .from(
          ".hmservice-textwrap h4",
          0.6,
          {
            xPercent: 20,
            opacity: 0,
            ease: Power4.easeOut,
            clearProps: "all"
          },
          "-=.1"
        )
        .from(
          ".hmservice-btnwrap",
          0.6,
          {
            xPercent: 20,
            opacity: 0,
            ease: Power4.easeOut,
            clearProps: "all"
          },
          "-=.2"
        );

      const bookController = new ScrollMagic.Controller();
      const bookAnimScene = new ScrollMagic.Scene({
        triggerElement: ".hmbookservice-container",
        reverse: false,
        offset: -200
      })
        .setTween(bookTimeline)
        .addTo(bookController);
    }
  }
  mounted() {
    if (process.browser && window) {
      this.Anim();
    }
  }

For Vue / Nuxt projects you can use vue-scrollmagic plugin.对于 Vue / Nuxt 项目,您可以使用vue-scrollmagic插件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM