Simplebar: Options in data atributes and init element by classname

Created on 31 Jul 2017  路  6Comments  路  Source: Grsmto/simplebar

Hi, nice plugin.

I have two questions:

  1. documentation does not specify that 芯ptions can be set through in data attributes ( Example: data-simplebar-autohide="false" ) Can you add information about that?

  2. now simplebar inited only by ID ( new SimpleBar(document.getElementById('myElement')) or new SimpleBar($('#myElement')[0]) ) can you make init it by class name? Example:

new SimpleBar($('.js-simplebar'), {
    option1: value1,
    option2: value2
})

Thanks for your attention.

Most helpful comment

@Grsmto thanks

All 6 comments

Hi,
good point for the data attribute! I'll add it to the doc.

About your second point, yes, you can init Simplebar as you want, it simply takes an element as input.
The problem on your example is that you pass it a jQuery object and not directly a DOM element. If I remember well, in jQuery you can do $('.js-simplebar').get(0) to get the DOM element from a jQuery object.

If you don't want to use jQuery for that, you can do document.getElementsByClassName('js-simplebar')[0].

@Grsmto Hi! Can i init like that ?

new SimpleBar( document.getElementsByClassName('js-simplebar') )

so i want to pass a list of elements

No you have to pass a single element. So you'll have to loop through every elements selected and initialise Simplebar for each of them. Something like:

Array.prototype.forEach.call(document.getElementsByClassName('js-simplebar'), function(el) {
    new SimpleBar(el);
});

Or if you use jQuery:

$('.js-simplebar').each(function(el) {
    new SimpleBar(el);
});

@Grsmto thanks

@Grsmto thanks

@Grsmto thanks

$('.js-simplebar').each(function(index, el) {
    new SimpleBar(el);
});
Was this page helpful?
0 / 5 - 0 ratings

Related issues

Blackleones picture Blackleones  路  3Comments

EmilMoe picture EmilMoe  路  4Comments

Grsmto picture Grsmto  路  7Comments

danielg42 picture danielg42  路  5Comments

RennerBink picture RennerBink  路  3Comments