Search code examples
javascriptjqueryslimscroll

slimScroll destroy doesn't unbind the scroll events


I'm using slimScroll jQuery plugin and it seems that the destroy option doesn't completely destroy the plugin effect on the site.

For example, if you try to destroy the plugin and then scroll over the previously scrollable content, the site scrolling functionality stops working. You will be able to scroll using the scrollbar, not not by using the mouse wheel / trackpad.

Here's a reproduction of the bug

Notice a couple of things:

  • Scrolling with mouse wheel / trackpad over the previously scrollable element blocks the scrolling.
  • Scrolling outside the previously scrollable element works as expected.
  • If you scroll the slimScroll until its bottom before destroying it, when destroying it, it works properly as it should in any occasion.

I've already reported it in the repository, but no answers are given. It seems its kind of abandoned. I tried different proposed solutions, but none of them work properly.

The lack of a proper method to destroy the plugin seems to be the problem...

Used code in jsfiddle:

$('.scrollable').slimScroll({
    allowPageScroll: true,
    height: '250px',
    size: '10px',
    alwaysVisible: true
});

$('.destroy').click(function(){
    $('.scrollable').slimScroll({
        destroy:true
    });
});

Solution

  • The problem is that the plugin is not removing the registered events. This should fix the problem:

    $('.destroy').click(function(){
        $('.scrollable').slimScroll({
            destroy:true
        });
    
        var $elem = $('.scrollable'),
        events = jQuery._data( $elem[0], "events" );
    
        if (events) {
            jQuery._removeData( $elem[0], "events" );
        }
    
    });