尝试在head标签中添加hammer.js和其他库



我的header.php文件看起来像这样,只是当我尝试使用hammer.js时它不起作用。jquery.hammer.customswipe和events.js也有错误。

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script src="<?php bloginfo('template_directory'); ?>/scripts/jquery-scrollto.js"></script>
<script src="<?php bloginfo('template_directory'); ?>/scripts/scripts.js"></script>
<script src="<?php bloginfo('template_directory'); ?>/scripts/core/js/libraries/jquery.hammer.min.js"></script>
<script src="<?php bloginfo('template_directory'); ?>/scripts/core/js/functions/jquery.hammer.customswipe.js"></script>
<script src="<?php bloginfo('template_directory'); ?>/scripts/core/js/events.js"></script>

错误:

jquery.hammer.js:8 Uncaught ReferenceError: Hammer is not defined
at jquery.hammer.js:8
at jquery.hammer.js:10
(anonymous) @ jquery.hammer.js:8
(anonymous) @ jquery.hammer.js:10
jquery.hammer.customswipe.js:123 Uncaught TypeError: options.containers.hammer is not a function
at Object.move (jquery.hammer.customswipe.js:123)
at Object.left (jquery.hammer.customswipe.js:42)
at init (jquery.hammer.customswipe.js:157)
at init.$.fn.customSwipe (jquery.hammer.customswipe.js:173)
at events.js:2
at events.js:10
move @ jquery.hammer.customswipe.js:123
left @ jquery.hammer.customswipe.js:42
init @ jquery.hammer.customswipe.js:157
$.fn.customSwipe @ jquery.hammer.customswipe.js:173
(anonymous) @ events.js:2
(anonymous) @ events.js:10

显示您提到的错误会很有用。然而,话虽如此,我还是会转而使用wp_enqueue_scriptWordPress排队脚本。加载您的文件,而不是直接将它们添加到header.php文件中。

您可能还需要将这些文件移动到页脚而不是页眉中进行加载。就是一个例子

wp_enqueue_script( 'hammerJS', get_template_directory_uri() . '/scripts/core/js/libraries/jquery.hammer.customswipe.js', ['jquery'], false, true );

这将确保jquery(['jquery']参数(在此脚本之前加载,并将脚本放在页脚(最后一个参数(中。

尝试一下,看看你是否取得了进展,但显示你遇到的错误将使我们能够提供更多帮助。

最新更新