vue3<script setup>中使用Swiper

swiper网址

Swiper中文网-轮播图幻灯片js插件,H5页面前端开发

Swiper - The Most Modern Mobile Touch Slider

安装 Swiper

npm安装:

复制代码
npm install swiper

yarn安装:

复制代码
yarn add swiper

导入带有所有模块(捆绑包)的 Swiper

复制代码
// import Swiper bundle with all modules installed
import Swiper from 'swiper/bundle';

// import styles bundle
import 'swiper/css/bundle';

// init Swiper:
const swiper = new Swiper(...);

添加基本的 Swiper 布局

复制代码
<!-- Slider main container -->
<div class="swiper">
  <!-- Additional required wrapper -->
  <div class="swiper-wrapper">
    <!-- Slides -->
    <div class="swiper-slide">Slide 1</div>
    <div class="swiper-slide">Slide 2</div>
    <div class="swiper-slide">Slide 3</div>
    ...
  </div>
  <!-- If we need pagination -->
  <div class="swiper-pagination"></div>

  <!-- If we need navigation buttons -->
  <div class="swiper-button-prev"></div>
  <div class="swiper-button-next"></div>

  <!-- If we need scrollbar -->
  <div class="swiper-scrollbar"></div>
</div>

添加一些自定义样式来设置 Swiper 大小

复制代码
.swiper {
  width: 600px;
  height: 300px;
}

最后,我们需要在 JS 中初始化 Swiper:

复制代码
const swiper = new Swiper('.swiper', {
  // Optional parameters
  direction: 'vertical',
  loop: true,

  // If we need pagination
  pagination: {
    el: '.swiper-pagination',
  },

  // Navigation arrows
  navigation: {
    nextEl: '.swiper-button-next',
    prevEl: '.swiper-button-prev',
  },

  // And if we need scrollbar
  scrollbar: {
    el: '.swiper-scrollbar',
  },
});

完整代码

复制代码
<script lang="ts" setup>
import { onMounted } from 'vue'
// import Swiper bundle with all modules installed
import Swiper from 'swiper/bundle';

// import styles bundle
import 'swiper/css/bundle';
onMounted(() => {
  // init Swiper:
  const swiper = new Swiper('.swiper', {
    // Optional parameters
    direction: 'vertical',
    loop: true,

    // If we need pagination
    pagination: {
      el: '.swiper-pagination',
    },

    // Navigation arrows
    navigation: {
      nextEl: '.swiper-button-next',
      prevEl: '.swiper-button-prev',
    },

    // And if we need scrollbar
    scrollbar: {
      el: '.swiper-scrollbar',
    },
  });
})


</script>

<template>
  <!-- Slider main container -->
  <div class="swiper">
    <!-- Additional required wrapper -->
    <div class="swiper-wrapper">
      <!-- Slides -->
      <div class="swiper-slide">Slide 1</div>
      <div class="swiper-slide">Slide 2</div>
      <div class="swiper-slide">Slide 3</div>
      ...
    </div>
    <!-- If we need pagination -->
    <div class="swiper-pagination"></div>

    <!-- If we need navigation buttons -->
    <div class="swiper-button-prev"></div>
    <div class="swiper-button-next"></div>

    <!-- If we need scrollbar -->
    <div class="swiper-scrollbar"></div>
  </div>
</template>

<style lang="less" scoped>
.swiper {
  width: 600px;
  height: 300px;
}
</style>

注意事项:

1.需要在组件完成初始渲染并创建 DOM 节点后运行代码

复制代码
import { onMounted } from 'vue'

onMounted(() => {
    // init Swiper:
    const swiper = new Swiper(...);
})

生命周期钩子 | Vue.js

2.示例使用less,需要安装

成品展示

相关推荐
FlyWIHTSKY40 分钟前
vue2和vue3有什么区别,用法上有哪些区别
前端·javascript·vue.js
码云之上41 分钟前
10 颗一模一样的「Edit」:中后台 Web Agent 怎么找到对的那一行
前端·人工智能·浏览器
量化分析码农1 小时前
【Python量化多因子实战 04】因子暴露不是 alpha:市值与行业中性化实战
前端·后端
不可能片场2 小时前
Electron 发布 正文写入的三条通道与读回口径
前端·electron
程序员大阳2 小时前
副队长JavaScript教程(66)-本地存储实战:待办事项网页的开发
javascript·json·实战·待办事项·本地存储·案例
jerrywus2 小时前
让 AI 帮你部署 Vercel,报错了也不用手动搬日志
前端·agent·mcp
补码缩写2 小时前
11 行 Python 拆 PNG 块,解压后的字节数能手算
前端
coding漫漫长路2 小时前
PNG压缩先看图是哪一类,先无损再量化多半白做
前端
用户257680763212 小时前
搜「洗牙」为什么搜不到「洁治」
前端
费曼学习法2 小时前
Agent 死循环的根因不在模型:错误分类、退避、熔断、快照四层实现
前端