vue3 轮播图的实现

最近开发过程中,有一些轮播图的需求,虽然公司的组件库已经有swiper的组件,但是功能不全,很多效果实现不了,于是经过翻找swiper的官网,发现你想要的样式都有,下面来说一下swiper的简单使用。

想实现的效果

点击prev和next可实现图片的切换

安装

swiper的安装是比较简单的。

中文官网:www.swiper.com.cn/index.html

英文官网: swiperjs.com/

css 复制代码
npm i swiper

使用

接下来就是swiper的使用了,swiper的使用非常简单。可查看官网例子
codesandbox.io/p/sandbox/2...

例子有归有,使用简单归简单,但是实现的样式和自己想要的差距还是很大,查了一波资料,现将代码放出,哈哈。
html

xml 复制代码
<swiper
  :navigation="{
    nextEl: '.swiper-button-next1',
    prevEl: '.swiper-button-prev1'
  }"
  :modules="modules"
  class="mySwiper"
  :slides-per-view="3"
>
    <swiper-slide>
       <img src="图片地址" alt="" />
    </swiper-slide>
    <swiper-slide>
       <img src="图片地址" alt="" />
    </swiper-slide>
    <swiper-slide>
       <img src="图片地址" alt="" />
    </swiper-slide>
    <swiper-slide>
       <img src="图片地址" alt="" />
    </swiper-slide>
    <swiper-slide>
       <img src="图片地址" alt="" />
    </swiper-slide>
    <swiper-slide>
       <img src="图片地址" alt="" />
    </swiper-slide>
    <swiper-slide>
       <img src="图片地址" alt="" />
    </swiper-slide>
    <swiper-slide>
       <img src="图片地址" alt="" />
    </swiper-slide>
    <div class="swiper-button-prev-one" slot="button-prev">
        <Button>
        </Button>
    </div>
    <div class="swiper-button-next-one" slot="button-next">
        <Button>
        </Button>
    </div>
</swiper>

通过navigation来控制轮播图的上一页,下一页,通过slides-per-view来控制每页显示几张图片。 js

javascript 复制代码
import { Swiper, SwiperSlide, navigationPrev } from 'swiper/vue'
import 'swiper/css'
import 'swiper/css/navigation'
import { Navigation } from 'swiper/modules'
const modules = [Navigation]

js部分也是毕竟简单的,把该引入的文件引入即可。这样难道就实现效果了吗,当然不是,还需要改css样式

css(css部分部分采用tailwindcss编写)

scss 复制代码
.mySwiper {
  @apply pb-2xl;
  .swiper-button-prev-one {
    @apply text-[#333] absolute text-[.875rem] left-0 bottom-0 cursor-pointer;
    :deep(.m-button) {
      @apply w-6 h-6;

      span {
        @apply text-sm #{!important};
      }
    }
  }
  .swiper-button-next-one {
    @apply text-[#333] absolute text-[.875rem] left-[2.5rem] bottom-0 cursor-pointer;
    :deep(.m-button) {
      @apply w-6 h-6 bg-[#000] text-[#fff];
      span {
        @apply text-sm #{!important};
      }
    }
  }
}

至此轮播图的效果就实现了,在做轮播图的需求时,需要仔细认真地查看文档,我是比较喜欢看英文文档,我觉得讲述比较全,大家学习的时候自行选择即可。

相关推荐
蓝倾1 分钟前
京东批量获取商品SKU操作指南
前端·后端·api
JSLove8 分钟前
常见 npm 报错问题
前端·npm
sunbyte8 分钟前
50天50个小项目 (Vue3 + Tailwindcss V4) ✨ | ContentPlaceholder(背景占位)
前端·javascript·css·vue.js·tailwindcss
爱学习的茄子9 分钟前
React Hooks进阶:从0到1打造高性能Todo应用
前端·react.js·面试
知性的小mahua13 分钟前
vue3+canvas实现摄像头ROI区域标记
前端
嘗_29 分钟前
暑期前端训练day5
前端
uncleTom66635 分钟前
前端布局利器:rem 适配全面解析
前端
谦哥38 分钟前
Claude4免费Vibe Coding!目前比较好的Cursor替代方案
前端·javascript·claude
LEAFF1 小时前
如何 测试Labview是否返回数据 ?
前端
Spider_Man1 小时前
🚀 从阻塞到丝滑:React中DeepSeek LLM流式输出的实现秘籍
前端·react.js·llm