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

成品展示

相关推荐
Danny_FD11 分钟前
Vue + Element UI 实现模糊搜索自动补全
前端·javascript
gnip15 分钟前
闭包实现一个简单Vue3的状态管理
前端·javascript
斐济岛上有一只斐济21 分钟前
后端程序员的CSS复习
前端
Enddme24 分钟前
《面试必问!JavaScript 中this 全方位避坑指南 (含高频题解析)》
前端·javascript·面试
有梦想的程序员25 分钟前
微信小程序使用 Tailwind CSS version 3
前端
程序员编程指南41 分钟前
Qt 与 WebService 交互开发
c语言·开发语言·c++·qt·交互
赵英英俊1 小时前
Python day26
开发语言·python
你怎么知道我是队长1 小时前
python---eval函数
开发语言·javascript·python
溟洵1 小时前
Qt 窗口 工具栏QToolBar、状态栏StatusBar
开发语言·前端·数据库·c++·后端·qt
用户2519162427111 小时前
Canvas之图像合成
前端·javascript·canvas