未捕获的TypeError:$(...)。velocity不是函数

2 javascript jquery velocity commonjs browserify

我有以下代码:

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
    };

}();
Run Code Online (Sandbox Code Playgroud)

togglePanel()被触发时,将引发以下错误:

未捕获的TypeError:$(...)。velocity不是函数

通常可以通过确保在需要JQuery的库之前加载JQuery来解决此问题。但我是..

var $ = require('jquery'); // first
var velocity = require('velocity-animate'); // second
Run Code Online (Sandbox Code Playgroud)

所以..什么给了?

Que*_*tin 5

文档中

模块加载器:Browserify
如果您将Velocity与jQuery一起使用,则必须在Velocity之前使用jQuery,并且必须在window对象上全局分配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 });
Run Code Online (Sandbox Code Playgroud)

您仅将jQuery分配给了局部 $变量。