未捕获的类型错误:$(..).versity不是函数

Uncaught TypeError: $(...).velocity is not a function

本文关键字:versity 函数 类型 错误      更新时间:2023-09-26

我有以下代码:

var $ = require('jquery');
var velocity = require('velocity-animate');
module.exports = function () {
    function togglePanel () {
        $('.trip-assist-search-panel__container').velocity({
            height: '0px'
        },{
            duration: 400
        });
    }
    return {
        togglePanel: togglePanel
    };
}();

togglePanel()被触发时,会抛出以下错误:

未捕获类型错误:$(…)。速度不是的函数

这通常是通过确保在需要JQuery的库之前加载JQuery来解决的。但是,我是…

var $ = require('jquery'); // first
var velocity = require('velocity-animate'); // second

所以。。什么东西?

来自文档:

模块加载器:Browserify
如果您将Velocity与jQuery一起使用,则必须在Velocity之前要求jQuery,并且必须分配jQuery对窗口对象进行全局查询:

window.jQuery = window.$ = require("path/to/jquery-x.x.x.js");
require("path/to/velocity.js");
// Optional: If you're using the UI pack, require it after Velocity. (You don't need to assign it to a variable.)
require("path/to/velocity.ui.js");
/* Your app code here. */
$("body").velocity({ opacity: 0.5 });

您只将jQuery分配给了本地$变量。