【问题标题】:Anime.js with Wordpress: module is not defined带有 Wordpress 的 Anime.js:未定义模块
【发布时间】:2019-02-09 13:18:13
【问题描述】:

我在本地使用带有纯 HTML、CSS 和 JS 的anime.js 制作了一些动画。 现在我需要在基于 wordpress 的网站中实现这些动画。

不幸的是,我收到了错误 module is not defined。它指的是anime.js文件中的最后一行module.exports = anime;

我真的不太了解 Wordpress 开发,但我确实知道如何将脚本排入队列(至少我认为我知道)

这是我的 functions.php 文件中的代码,我在其中调用了动画.js 和我的 js 文件,尽管我什至不知道这个错误是否与我如何将文件排入队列有关。

function load_my_scripts() {
    wp_register_script('animejs', get_template_directory_uri() . "/js/anime.js", array(), '', true );
    wp_enqueue_script('animejs');
    wp_register_script('scriptjs', get_template_directory_uri() . "/js/script.js", array('animejs'), '', true );
    wp_enqueue_script('scriptjs');
}
add_action('init', 'load_my_scripts');  

出现问题的站点是这个:http://www.provokatur.at/

真的希望有人能帮帮我,谢谢。

【问题讨论】:

  • 我总是这样 enque 脚本:

标签: javascript wordpress module anime.js enqueue


【解决方案1】:

如果您确定您的代码是正确的,请不要忘记在页面中添加<?php get_footer(); ?>

编辑: 我尝试在我的主题中使用animejs,这段代码对我有用

function load_my_scripts() {
wp_register_script('animejs', 'https://cdnjs.cloudflare.com/ajax/libs/animejs/2.2.0/anime.min.js');
wp_enqueue_script('testscript', get_template_directory_uri(). '/js/test.js', array('animejs'), '', true);
}

add_action ('wp_enqueue_scripts', 'load_my_scripts');

【讨论】:

  • 感谢您到目前为止的帮助,但我无法让它工作。我现在没有错误,但动画仍然拒绝播放。 Anime.js 已正确加载。我复制了动画并在控制台中运行它,一切正常,但不是来自 script.js 文件。
  • 难道我也必须在我的脚本文件中包含anime.js 文件吗?但这是否意味着我以错误的方式排队?
  • 在您的情况下,代码将是 wp_register_script('animejs', get_template_directory_uri() . '/js/anime.js');wp_enqueue_script('scriptjs', get_template_directory_uri(). '/js/script.js', array('animejs'), '', true);
【解决方案2】:

我想通了。

我不再将anime.js 文件排入队列,我只是将其注册到:

wp_register_script('animejs', 'https://cdnjs.cloudflare.com/ajax/libs/animejs/2.2.0/anime.min.js');

在那之后动画没有被执行,我所做的只是添加

  jQuery(document).ready(function($){
        //Code goes here
  })

显然这是用jQuery执行动画......虽然我真的不明白这背后的原因但我仍然希望我的解释能帮助一些人。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-10-14
    • 2016-09-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-02
    • 1970-01-01
    相关资源
    最近更新 更多