Search code examples
javascriptvue.jsvuejs2owl-carouselowl-carousel-2

Trigger next and previous element in 'vue-owl-carousel' package Vue.js


I have problem with triggering next or previous item in vue-owl-carousel package in Vue with javascript.

Can anybody help?

Carousel code :

<carousel id="newGamesCarousel" :items="1" :dots="false" :nav="false" :margin="10" :key="refresh">
  <div class="newGames-item" v-for="item in newProducts">
    <div class="details">
      <div class="labels flex-wrap">
        <span class="mb-1" v-for="platform in item.platforms">{{platform.name}}</span>
        <span class="mb-1" v-for="region in item.regions">ریجن {{region.name}}</span>
      </div>
      <h2 class="gameTitle">{{item.name}}</h2>
      <div class="priceBox">
        <router-link class="add-to-cart" :to="{path: '/game/'+item.id+'/'+stringToSlug(item.title)+''}">+</router-link>
        <div class="price">
          <div class="former" v-if="item.sale_percent !== false">
            <span class="percent">%{{item.sale_percent}}</span>
            <span class="total">{{item.price}}</span>
          </div>
          <div class="later">
            {{item.regular_price}}
            <span class="unit">تومان</span>
          </div>
        </div>
      </div>
    </div>

    <figure class="image">
      <img v-bind:src="item.thumbnail_url" v-bind:alt="item.name">
    </figure>
  </div>
</carousel>

And the next and previous buttons:

<div class="arrows">
  <svg class="nextCarousel" id="newGamesNext" viewBox="0 0 24 24">
    <use xlink:href="/src/assets/imgs/sprite.svg#arrow"></use>
  </svg>
  <svg class="prevCarousel" id="newGamesPrev" viewBox="0 0 24 24">
    <use xlink:href="/src/assets/imgs/sprite.svg#arrow"></use>
  </svg>
</div>

Solution

  • as per documentation described here : https://www.npmjs.com/package/vue-owl-carousel

    Custom prev and next buttons using slot, the buttons will be hidden while start and end in non-loop mode

    <carousel>
    
      <template slot="prev"><span class="prev">prev</span></template>
    
      <template slot="next"><span class="next">next</span></template>
    
    </carousel>
    

    within the carousel tag after adding all your slides

    you can add <template slot="{{prev/next}}">{{add your icon image here }}</template> and it should take care of the previous and next events on its own

    <template slot="prev">
       <svg class="nextCarousel" id="newGamesNext" viewBox="0 0 24 24"><use xlink:href="/src/assets/imgs/sprite.svg#arrow"></use></svg>
    </template>
    

    this is what I feel should solve your issue. If Im missing something feel free to add urls of documentations/ tutorials that you followed to get your code here. please let me know if the solution worked or not