m-rm
m-rm

Reputation: 340

Use jScrollPane JQuery plugin with RequireJS

Currently JQuery is working fine with RequireJS. I now tried to use the jScrollPane plugin for JQuery. This plugin depends on the mousewheel plugin, so both have to be loaded. Since both plugins support AMD this shouldn't be a big problem.

I tried multiple solutions including

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

and

// 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();
    });
});

but nothing is working. Using the JavaScript debugger in Chrome the callback function is never called and no error is thrown. The files are called jquery.jscrollpane.js and jquery.mousewheel.js and are placed in the same folder as RequireJS and JQuery.

Also it should be possible to load the plugins only once since they just extend JQuery, but I haven't found an example for that.

What am I doing wrong?

Upvotes: 2

Views: 1205

Answers (1)

ekuusela
ekuusela

Reputation: 5282

Since both plugins support AMD you shouldn't use a shim config.

Also, while creating the snippet below, I noticed that the scroll pane doesn't work if the .scroll-pane container has text only and no p elements.

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>

Upvotes: 1

Related Questions