Owlcarousel2: Please add data attributes for all options

Created on 6 Feb 2017  路  11Comments  路  Source: OwlCarousel2/OwlCarousel2

If we call data-owl-carousel in main div so owl carousel execute automatically.

For example:

<div class="owl-carousel owl-theme" data-owl-carousel>
    <div class="item"><h4>1</h4></div>
    <div class="item"><h4>2</h4></div>
    <div class="item"><h4>3</h4></div>
    <div class="item"><h4>4</h4></div>
</div>

even we can call all options from data attributes for example:

<div class="owl-carousel owl-theme" data-owl-carousel="{margin: 10, items: 2}">
    <div class="item"><h4>1</h4></div>
    <div class="item"><h4>2</h4></div>
    <div class="item"><h4>3</h4></div>
    <div class="item"><h4>4</h4></div>
</div>

Most helpful comment

@JonathanSchndr Thank you. It's working now. :)

All 11 comments

You can use this way:

var $carousel = $('[data-owl-carousel]');
if ($carousel.length) {
     $carousel.each(function(index, el) {
          $(this).owlCarousel($(this).data('owl'));
     });
 }

@ocvninfo Thanks for share it. Carousel coming but options not working :(

@bdthemes
Please try this markup:
data-owl-carousel='{"margin": 10, "items": 2}'

oh sorry!
please use this:
$(this).owlCarousel($(this).data('owl-carousel'));
not:
$(this).owlCarousel($(this).data('owl'));

@ocvninfo Thanks it's working now 馃槃

Hi ! how to add HTMl navText used data attributes ? This my code
<data-owl-carousel='{"margin":15,"loop":true,"nav":true,"dots":false,"items":2,"autoplaySpeed":1000,"navText": ["<i class="fa-icon-angle-left"></i>,"<i class="fa-icon-angle-right"></i>"],"responsive":{ "540" :{ "items": 1 }, "860" :{ "items" : 2 } , "1000":{ "items" : 2 }}}'>
But show error correct format json.

@henrytran9x Heave you got any solution for this?

var $owlAttr = { loop: true, items: 1 },  
$extraAttr = $(".owl-carousel").data("owl-carousel");  
$.extend($owlAttr, $extraAttr);
$(".owl-carousel").owlCarousel($owlAttr);

Use default attr + data-owl-carousel='{}'
so it is not necessary to always have all the attributes within the html file

@JonathanSchndr Thank you. It's working now. :)

@henrytran9x You can try this way.

data-owl-carousel='{"margin":15,"loop":true,"nav":true,"dots":false,"items":2,"autoplaySpeed":1000,"navText": ["<i class=&#x27;fa-icon-angle-left&#x27;></i>","<i class=&#x27;fa-icon-angle-right&#x27;></i>"],"responsive":{ "540" :{ "items": 1 }, "860" :{ "items" : 2 } , "1000":{ "items" : 2 }}}'

thanks so much !

Was this page helpful?
0 / 5 - 0 ratings

Related issues

siwel picture siwel  路  3Comments

stratboy picture stratboy  路  3Comments

unite4 picture unite4  路  4Comments

JezCheese picture JezCheese  路  3Comments

mkraha picture mkraha  路  4Comments