目前,JQuery与RequireJS配合良好。我现在尝试将jScrollPane插件用于JQuery。此插件取决于mousewheel插件,因此都必须加载。由于两个插件都支持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元素,则滚动窗格将不起作用。



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/

10-12 12:34
查看更多