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};
      }
    }
  }
}

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

相关推荐
ct97813 分钟前
React 状态管理方案深度对比
开发语言·前端·react
胡志辉的博客30 分钟前
深入浅出理解浏览器事件循环:从一道输出题讲到 Chrome 源码
前端·javascript·chrome·chromium·event loop
代码不加糖38 分钟前
js中不会冒泡的事件有哪些?
前端·javascript·vue.js
懂懂tty1 小时前
Vue2与Vue3之间API差异
前端·javascript·vue.js
AI焦点1 小时前
跨越协议鸿沟:Tool Use状态机从Anthropic到OpenAI兼容体系的适配要点
前端·人工智能
Dxy12393102161 小时前
Python线程锁:为什么多线程会“打架“,以及怎么解决
开发语言·前端·python
海兰2 小时前
【web应用】Excel 项目数据自动化分析系统(AI 驱动分析)详细设计与部署指南(附源代码)
前端·人工智能·自动化·excel
2501_940041742 小时前
技术分享:高质量全栈开发提示词设计实践 —— 覆盖简单到复杂
前端·prompt
IT_陈寒2 小时前
Python的os.path.join居然能这么坑?
前端·人工智能·后端
艳阳天_.2 小时前
星瀚弹框页面实现
java·前端·python