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,需要安装

成品展示

相关推荐
我叫黑大帅22 分钟前
搜“张三”把备注也带出来了?不用加高级筛选,如何让一个搜索框实现「选项直达」!
前端·javascript·面试
KevinWang_38 分钟前
iOS 开发计划(三) - 打印 JavaScript 调试日志
javascript
炸膛坦客2 小时前
单片机/C/C++八股:(三十七)指针数组 和 数组指针
c语言·开发语言·c++
夜不会漫长2 小时前
C++:C++11特性
开发语言·c++·算法
如夏倾城2 小时前
WAF (Web Application Firewall,Web应用防火墙)
前端·网络·安全·web安全·web app
apcipot_rain3 小时前
C++转java日记(13)
java·开发语言
图扑可视化3 小时前
基于图扑软件智慧制硅厂 Web SCADA 开发实践
前端·三维可视化·scada·hmi·组态软件
前端fighter4 小时前
2026年前端面试题总结(pdf版)
前端·pdf
郝学胜-神的一滴4 小时前
Effective Python 条款41:试Mix‑In组合式开发
开发语言·pytorch·python·pycharm·numpy
谢小飞4 小时前
运维加了个参数,我们的网页挂掉了
前端·浏览器