最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - jQuery each function for multiple carousels next, prev identifier issue - Stack Overflow

programmeradmin0浏览0评论

I'm using the Owl Carousel for my site and want to use the carousel multiple times on one page, i have successfully achieved this using the .each, however the jQuery code i'm using, when clicking the previous or next buttons to show the item in the carousel it triggers all the carousels. Clicking the next/prev button moves the item in all the carousels.

jQuery(document).ready(function($) {        
    $(".owlcarousel-slider").each( function() {     
        var $this = $(this);
        var autoscroll = $this.attr("data-autoscroll"); 
        if(autoscroll == 1) {autoscroll = true;} else {autoscroll = false;}

        $this.owlCarousel({
            autoPlay: autoscroll
        });      

        $(".next").click(function(){
            $this.trigger('owl.next');
        })

        $(".prev").click(function(){
            $this.trigger('owl.prev');
        })             
    });
});

I believe the incorrect code has to be this bit,

$(".next").click(function(){
    $this.trigger('owl.next');
})

$(".prev").click(function(){
    $this.trigger('owl.prev');
})

Unfortunaltly my jQuery isn't my strongest, i believe i'm almost there.

Thankyou

I'm using the Owl Carousel for my site and want to use the carousel multiple times on one page, i have successfully achieved this using the .each, however the jQuery code i'm using, when clicking the previous or next buttons to show the item in the carousel it triggers all the carousels. Clicking the next/prev button moves the item in all the carousels.

jQuery(document).ready(function($) {        
    $(".owlcarousel-slider").each( function() {     
        var $this = $(this);
        var autoscroll = $this.attr("data-autoscroll"); 
        if(autoscroll == 1) {autoscroll = true;} else {autoscroll = false;}

        $this.owlCarousel({
            autoPlay: autoscroll
        });      

        $(".next").click(function(){
            $this.trigger('owl.next');
        })

        $(".prev").click(function(){
            $this.trigger('owl.prev');
        })             
    });
});

I believe the incorrect code has to be this bit,

$(".next").click(function(){
    $this.trigger('owl.next');
})

$(".prev").click(function(){
    $this.trigger('owl.prev');
})

Unfortunaltly my jQuery isn't my strongest, i believe i'm almost there.

Thankyou

Share Improve this question asked Feb 26, 2014 at 22:15 ShoeboxShoebox 6012 gold badges8 silver badges17 bronze badges 3
  • To begin with, take out your click listeners out of $.each – martynas Commented Feb 26, 2014 at 22:22
  • Hi, thankyou for your response, heres the updated code but still the same issue, pastie/8794785 – Shoebox Commented Feb 26, 2014 at 22:37
  • Could you create a jsFiddle? jsfiddle – martynas Commented Feb 26, 2014 at 22:40
Add a ment  | 

2 Answers 2

Reset to default 5

If someone else still got this issue, here is a working example:

HTML Markup

<div id="owl-demo" class="owl-carousel owl-theme">
   <div class="item"><h1>1</h1></div>
   <div class="item"><h1>2</h1></div>
   <div class="item"><h1>3</h1></div>
   <div class="item"><h1>4</h1></div>
   <div class="item"><h1>5</h1></div>
   <div class="item"><h1>4</h1></div>
   <div class="item"><h1>5</h1></div>
   <div class="item"><h1>4</h1></div>
   <div class="item"><h1>5</h1></div>
   <div class="item"><h1>4</h1></div>
   <div class="item"><h1>5</h1></div>
</div>
<div class="customNavigation">
   <a class="btn prev">Previous</a>
   <a class="btn next">Next</a>
</div>

Javascript

$(".owl-carousel").each(function() {
  var $this = $(this);
  $this.owlCarousel({
    items : 5
  });
  // Custom Navigation Events
  $this.parent().find(".next").click(function(){
    $this.trigger('owl.next');
  });
  $this.parent().find(".prev").click(function(){
    $this.trigger('owl.prev');
  });
});

Wasn't able to test it myself, but try this.

Remove the part you said you think is wrong:

$(".next").click(function(){
    $this.trigger('owl.next');
})

$(".prev").click(function(){
    $this.trigger('owl.prev');
})

And add "navigation : true" to your carousel properties:

$this.owlCarousel({
    autoPlay: autoscroll,
    navigation : true
}); 
发布评论

评论列表(0)

  1. 暂无评论