vue3中使用swiper(9)完整版

这篇具有很好参考价值的文章主要介绍了vue3中使用swiper(9)完整版。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

vue3中使用swiper(9)完整版

vue3中使用swiper(9)完整版

1、使用方式

1)安装 swiper插件;

方法一:npm install swiper

方法二:yarn add swiper

注意:如果npm 无法安装swiper时,使用yarn安装;

npm install -g yarn // 安装yarn
yarn init # yarn  // 初始化
yarn remove 包名称  // 删除某个包
yarn run serve  // 运行

vue3中使用swiper(9)完整版

2)参数介绍
  • modules:

  • loop: 是否循环播放

  • slides-per-view:控制一次显示几张轮播图

  • space-between: 每张轮播图之间的距离,该属性不可以和margin 属性同时使用;

  • autoplay: 是否自动轮播, delay为间隔的毫秒数;disableOnInteraction属性默认是true,也就是当用户手动滑动后禁用自动播放, 设置为false后,将不会禁用,会每次手动触发后再重新启动自动播放。

  • navigation: 定义左右切换箭头

  • pagination: 控制是否可以点击圆点指示器切换轮播

  • scrollbar: 是否显示轮播图的滚动条, draggable设置为 true就可以拖动底部的滚动条(轮播当中,一般不怎么会使用到这个属性)

2、代码如下

1)组件swiperCom.vue的代码:

<template>
  <swiper
    class="swiper"
    :modules="modules"
    :loop="true"
    :slides-per-view="1"
    :space-between="50"
    :navigation="navigation"
    :autoplay="{ delay: 3000, disableOnInteraction: false }"
    :pagination="{ clickable: true }"
    :scrollbar="{ draggable: true }"
    @swiper="onSwiper"
    @slideChange="onSlideChange"
  >
    <swiper-slide
      v-for="(item, index) in imgs"
      :key="index"
      class="swiper-slide"
    >
      <img :src="item.pic" alt="" class="swiper-img" />
    </swiper-slide>
    <div class="swiper-button-prev" @click.stop="prevEl(item, index)" />
    <!--左箭头。如果放置在swiper外面,需要自定义样式。-->
    <div class="swiper-button-next" @click.stop="nextEl" />
    <!--右箭头。如果放置在swiper外面,需要自定义样式。-->
    <!-- 如果需要滚动条 -->
    <!-- <div class="swiper-scrollbar"></div> -->
  </swiper>
</template>
<script>
// Import Swiper Vue.js components
import { ref } from 'vue'
import { Swiper, SwiperSlide } from 'swiper/vue'
import { Autoplay, Navigation, Pagination, Scrollbar, A11y } from 'swiper'
import 'swiper/scss'
import 'swiper/scss/navigation'
import 'swiper/scss/pagination'
// import 'swiper/css/scrollbar'

export default {
  name: 'SwiperCom',
  data () {
    return {
      imgs: [
        { pic: require('../assets/001.jpg') },
        { pic: require('../assets/002.jpg') },
        { pic: require('../assets/003.jpg') },
        { pic: require('../assets/004.png') }
      ]
    }
  },
  components: {
    Swiper,
    SwiperSlide
  },
  setup () {
    const onSwiper = (swiper) => {
      console.log(swiper)
    }
    const navigation = ref({
      nextEl: '.swiper-button-next',
      prevEl: '.swiper-button-prev'
    })
    const prevEl = (item, index) => {
      // console.log('上一张' + index + item)
    }
    const nextEl = () => {
      // console.log('下一张')
    }
    // 更改当前活动swiper
    const onSlideChange = (swiper) => {
      // swiper是当前轮播的对象,里面可以获取到当前swiper的所有信息,当前索引是activeIndex
      console.log(swiper.activeIndex)
    }
    return {
      onSwiper,
      onSlideChange,
      prevEl,
      nextEl,
      navigation,
      modules: [Autoplay, Navigation, Pagination, Scrollbar, A11y]
    }
  }
}
</script>
<style lang="scss" > 
.swiper {
  width: 339rem;
  height: 150rem;
  background-color: antiquewhite;
  .swiper-slide {
    .swiper-img {
      width: 339px;
      height: 150px;
    }
  }
  .swiper-button-next,
  .swiper-button-prev {
    --swiper-theme-color: red;
    --swiper-navigation-size: 20rem;
  }
//   改变小圆点的样式
  .swiper-pagination-bullet-active {
    background: white;
  }
}
</style>

vue3中使用swiper(9)完整版

2)组件HomeView.vue的代码:

<template>
  <div class="home">
    <swiperCom />  // 在HomeView中调用组件swiperCom
  </div>
</template>

<script>
// @ is an alias to /src
import swiperCom from '@/components/swiperCom.vue' // 引入组件swiperCom
export default {
  name: 'HomeView',
  components: {
    swiperCom   // 所用到的组件记得写在components中
  }
}
</script>

3、基本步骤

1)在 components 文件中新建组件 swiperCom.vue ,在HomeView.vue中调用组件

vue3中使用swiper(9)完整版文章来源地址https://www.toymoban.com/news/detail-492328.html

到了这里,关于vue3中使用swiper(9)完整版的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • vue3.0 透传属性和事件的使用方式

    如何“透传属性和事件” 父组件在使用子组件的时候,如何“透传属性和事件”给子组件呢? 透传属性和事件并没有在子组件中用 props 和 emits 声明 透传属性和事件最常见的如 @click 和 class 、 id 、 style   当子组件 只有一个根元素时 ,透传属性和事件会自动添加到该根元素

    2024年01月25日
    浏览(24)
  • vue3中使用route、router、store的方式

    route:  (1) vue3写法:  (2) vue2写法:  router:  (1) vue3写法:  (2) vue2写法:  store:  (1) vue3写法:  (2) vue2写法:

    2024年02月13日
    浏览(30)
  • Vue3使用全局函数或变量的两种常用方式

    例如:想要在index.ts中创建getAction函数,并可以全局使用: 方式一:使用依赖注入(provide/inject) 在main.ts中进行挂载: 在要使用的页面注入: 方式二:使用 app.config.globalProperties 和 getCurrentInstance() app.config.globalProperties:一个用于注册能够被应用内所有组件实例访问到的全局

    2024年02月12日
    浏览(31)
  • 你可能从未使用过的调试 Vue3 (开源项目) 源码的方式

        1. 前言 大家好,我是若川。我倾力持续组织了一年多源码共读,感兴趣的可以加我微信 lxchuan12 参与。另外,想学源码,极力推荐关注我写的专栏《学习源码整体架构系列》,目前是掘金关注人数(4.7k+人)第一的专栏,写有20余篇源码文章。 看一个开源仓库,第一步一

    2024年02月04日
    浏览(31)
  • vue中swiper使用

    说明:导入相应js引css import \\\"Swiper\\\" from \\\"swiper\\\" import \\\"swiper/css/swiper.css\\\"; import \\\"swiper/js/swiper\\\"; 说明:必要的结构使用;直接封装成一个组件  说明:(页面当中务必要有结构);注释已经写入代码。 第一:可以解决获取数据在Swiper实例之前;第二:可以解决v-for遍历完后在Swiper之

    2024年02月14日
    浏览(32)
  • vue2 使用swiper

    在vue2项目中要用到轮播的功能,用swiper插件实现遇到了一些坑 总结下。 一、运行安装命令: 如果直接运行npm i swiper 是默认安装的最新版本是适用vue3 不适用vue2的 安装完成后oackage.json多了  vue-awesome-swiper本来是5.0.1 我手动改成了4.1.1,因为当我运行时,我的项目报错  所以

    2024年02月11日
    浏览(36)
  • vue使用swiper三行轮播问题

    1、轮播图设置属性slidesPerColumn:3实现不了,解决方案如下: this.scheduleData是后台请求的数据,通过3个一组分组转换为this.scheduleListThreede 的数据! 2、逻辑处理如下: computed: {       scheduleListThree: function () {           let index = 0;           let count = 3;           let arrThree

    2024年02月09日
    浏览(30)
  • Swiper在Vue2中的使用

    以swiper3为例 1. 下载swiper3  2. 在main.js中引入Vue-Awesome-Swiper  3.  在swiper.vue中  2.6.7版本 3.1.3版本 1. 下载swiper3  2.  在swiper.vue中 引入样式和组件 数据是写死的时候,能够loop:true是有效的; 数据是动态获取的loop:true就会失效。 解决办法: 加上v-if=\\\"list.length\\\"有效解决

    2024年02月11日
    浏览(28)
  • vue中swiper轮播图的使用

    说明:导入相应js引css import \\\"Swiper\\\" from \\\"swiper\\\" import \\\"swiper/css/swiper.css\\\"; import \\\"swiper/js/swiper\\\"; 说明:必要的结构使用;直接封装成一个组件  说明:(页面当中务必要有结构);注释已经写入代码。 第一:可以解决获取数据在Swiper实例之前;第二:可以解决v-for遍历完后在Swiper之

    2024年02月14日
    浏览(34)
  • vue3:router安装与使用

    Terminal中运行以下代码自动安装 安装完成后,在package.json中查看vue-router是否安装成功 src目录下新建一个router文件夹,在router文件夹里新建一个index.js文件,index.js中的代码如下 main.js中代码如下 在主页面App.vue中的模板内写入一下两行代码即可 运行项目,打开运行地址,此时

    2023年04月19日
    浏览(37)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包