bootstrap的轮播如何用vue渲染

bootstrap官网轮播的例子:

<div id="carouselExampleControls" class="carousel slide" data-ride="carousel">
  <div class="carousel-inner">
    <div class="carousel-item active">
      <img class="d-block w-100" src=".../800x400?auto=yes&bg=777&fg=555&text=First slide" alt="First slide">
    </div>
    <div class="carousel-item">
      <img class="d-block w-100" src=".../800x400?auto=yes&bg=666&fg=444&text=Second slide" alt="Second slide">
    </div>
    <div class="carousel-item">
      <img class="d-block w-100" src=".../800x400?auto=yes&bg=555&fg=333&text=Third slide" alt="Third slide">
    </div>
  </div>
  <a class="carousel-control-prev" href="#carouselExampleControls" role="button" data-slide="prev">
    <span class="carousel-control-prev-icon" aria-hidden="true"></span>
    <span class="sr-only">Previous</span>
  </a>
  <a class="carousel-control-next" href="#carouselExampleControls" role="button" data-slide="next">
    <span class="carousel-control-next-icon" aria-hidden="true"></span>
    <span class="sr-only">Next</span>
  </a>
</div>

注意轮播部分是carousel-item 激活的class为crousel-item active

<div id="carouselExampleControls" class="carousel slide" data-ride="carousel">
  <div class="carousel-inner">

    <div class="carousel-item active" v-for="item of data" :key="item.key">
      <img class="d-block w-100" :src="item.key" alt="First slide">
    </div>

  </div>
  <a class="carousel-control-prev" href="#carouselExampleControls" role="button" data-slide="prev">
    <span class="carousel-control-prev-icon" aria-hidden="true"></span>
    <span class="sr-only">Previous</span>
  </a>
  <a class="carousel-control-next" href="#carouselExampleControls" role="button" data-slide="next">
    <span class="carousel-control-next-icon" aria-hidden="true"></span>
    <span class="sr-only">Next</span>
  </a>
</div>

这样会出现问题,class都有active 全部都是激活的 就无法切换轮播了

我想是这样写的

<div id="carouselExampleControls" class="carousel slide" data-ride="carousel">
  <div class="carousel-inner">
    
    <div v-for="(item,index) of data">
    <div v-if="index==0">
        <div class="carousel-item active" :key="item.key">
          <img class="d-block w-100" src="item.img" alt="First slide">
        </div>
    </div>
    <div v-else>
       <div class="carousel-item " :key="item.key">
          <img class="d-block w-100" src="item.img" alt="First slide">
        </div>
    </div>
    
    
  </div>
  <a class="carousel-control-prev" href="#carouselExampleControls" role="button" data-slide="prev">
    <span class="carousel-control-prev-icon" aria-hidden="true"></span>
    <span class="sr-only">Previous</span>
  </a>
  <a class="carousel-control-next" href="#carouselExampleControls" role="button" data-slide="next">
    <span class="carousel-control-next-icon" aria-hidden="true"></span>
    <span class="sr-only">Next</span>
  </a>
</div>

but这些写就不能切换了,请问下这个怎么解决呢?

阅读 5.5k
3 个回答

先说说自己用vue写轮播的思路哈。

无缝循环就是首尾多两张图片,这个先不讨论,然后就是active的问题。
active的话,可以对自己原有的数据格式稍微加工一下,反正要有一个标识,例如:
[index: 0, src: ''],
[index: 1, src: '']
...

类似这样,vue代码里可以动态绑定class

<div v-for="(item, index) in data">
    <div :class="`item ${index === item.index ? 'active' : ''}`"></div>
</div>
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题
宣传栏