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

成品展示

相关推荐
鱼大大博客26 分钟前
Edge Scdn的应用场景有哪些?
前端·edge
桂月二二27 分钟前
深入探索 Rust 中的异步编程:从基础到实际案例
开发语言·后端·rust
两只鱼丿32 分钟前
Edge安装问题,安装后出现:Could not find Edge installation
前端·edge
早上好啊! 树哥2 小时前
JavaScript Math(算数) 对象的用法详解
开发语言·javascript·ecmascript
noravinsc3 小时前
requests请求带cookie
开发语言·python·pycharm
screct_demo3 小时前
通俗易懂的讲一下Vue的双向绑定和React的单向绑定
前端·javascript·html
有心还是可以做到的嘛3 小时前
ref() 和 reactive() 区别
前端·javascript·vue.js
程序猿(雷霆之王)5 小时前
C++——继承
开发语言·c++
自律小仔6 小时前
桌面开发 的变量声明(Variable Declaration)核心知识
开发语言·后端·golang
ouyang_ouba6 小时前
pygame飞机大战
开发语言·python·pygame