javascript - 将 jScrollPane JQuery 插件与 RequireJS 一起使用

标签 javascript jquery requirejs jscrollpane jquery-jscrollpane

目前 JQuery 与 RequireJS 配合良好。我现在尝试使用 JQuery 的 jScrollPane 插件。该插件依赖于鼠标滚轮插件,因此两者都必须加载。由于这两个插件都支持 AMD,所以这应该不是什么大问题。

我尝试了多种解决方案,包括

require(['jquery.jscrollpane','jquery.mousewheel'], function(JScrollPane) {
    $(function()
    {
        $('.scroll-pane').jScrollPane();
    });
});

// At the beginning of main.js
requirejs.config({
    "shim": {
        "jquery.jscrollpane": ["jquery"],
        "jquery.mousewheel": ["jquery"]
    }
});

// Later in another script
define(["jquery", "jquery.mousewheel", "jquery.jscrollpane"], function($) {
    $(function()
    {
        $('.scroll-pane').jScrollPane();
    });
});

但没有任何效果。在 Chrome 中使用 JavaScript 调试器,永远不会调用回调函数,也不会抛出任何错误。这些文件称为 jquery.jscrollpane.js 和 jquery.mousewheel.js,与 RequireJS 和 JQuery 位于同一文件夹中。

此外,应该可以只加载一次插件,因为它们只是扩展了 JQuery,但我还没有找到这方面的示例。

我做错了什么?

最佳答案

因为这两个插件都支持 AMD,所以你不应该使用 shim config .

此外,在创建下面的代码片段时,我注意到如果 .scroll-pane 容器只有文本而没有 p 元素,则滚动 Pane 不起作用。

requirejs.config({
  paths: {
    'jquery': 'http://code.jquery.com/jquery-2.1.3.min',
    'jquery.mousewheel': 'http://rawgit.com/jquery/jquery-mousewheel/c61913ebf569c7a3f086b7fb40d941c282d1ce48/jquery.mousewheel',
    'jquery.jscrollpane': 'http://jscrollpane.kelvinluck.com/script/jquery.jscrollpane.min'
  }
});


requirejs(["jquery", "jquery.mousewheel", "jquery.jscrollpane"], function($) {
  $(function() {
    $('.scroll-pane').jScrollPane();
  });
});
.scroll-pane {
  width: 100%;
  height: 100px;
  overflow: auto;
}
<link href="http://jscrollpane.kelvinluck.com/style/jquery.jscrollpane.css" rel="stylesheet" />
<script src="http://requirejs.org/docs/release/2.1.17/minified/require.js"></script>
<div class="scroll-pane">
  <p>jScrollPane is designed to be flexible but very easy to use. After you have downloaded and included the relevant files in the head of your document all you need to to is call one javascript function to initialise the scrollpane. You can style the resultant
    scrollbars easily with CSS or choose from the existing themes. There are a number of different examples showcasing different features of jScrollPane and a number of ways for you to get support.</p><p>
    Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec in ligula id sem tristique ultrices eget id neque. Duis enim turpis, tempus at accumsan vitae, lobortis id sapien. Pellentesque nec orci mi, in pharetra ligula. Nulla facilisi. Nulla facilisi.
    Mauris convallis venenatis massa, quis consectetur felis ornare quis. Sed aliquet nunc ac ante molestie ultricies. Nam pulvinar ultricies bibendum. Vivamus diam leo, faucibus et vehicula eu, molestie sit amet dui. Proin nec orci et elit semper ultrices. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Sed quis urna mi, ac dignissim mauris. Quisque mollis ornare mauris, sed laoreet diam malesuada quis. Proin vel elementum ante. Donec hendrerit arcu ac odio tincidunt posuere. Vestibulum nec risus eu lacus semper viverra.</p>
</div>

关于javascript - 将 jScrollPane JQuery 插件与 RequireJS 一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29967715/

相关文章:

javascript - 如何在 slider 悬停时暂停

javascript - ajax开发的问题

javascript - 重构代码以在 jQuery 1.3.2 上工作

jquery - 如何在 Node Express Web 服务器中使用 requirejs、jquery 和 d3

mobile - 使用 RequireJS 和 Backbone.js 进行页面转换

javascript - 如何在主页面和子页面中实现requirejs。?

javascript - :hover style sticking on iOS browser

javascript - 在 NodeJS 中模拟发出 "Socket hang up"

javascript - tinyMce 绑定(bind) focusin/focusout 事件?

javascript - 添加和删​​除类时jquery Click事件的问题