Swiper轮播图在Vue框架中的运用

要在Vue中运用Swiper需要下载Swiper插件
在命令行cmd(可以cd 根目录)输入

npm install vue-awesome-swiper --save

下载成功后,查看package.json中是否存在
在这里插入图片描述
在mian.js中全局引入

import VueAwesomeSwiper from 'vue-awesome-swiper'
// 同时必须引入css样式文件
import 'swiper/css/swiper.css'

// 挂载到vue全局上

或在需要使用的界面引入

  require('swiper/css/swiper.css');
  import { swiper, swiperSlide } from 'vue-awesome-swiper'
  import 'swiper/css/swiper.css'
    components: {
      swiper,
      swiperSlide
    },

引入后便可以使用了
下面是我根据Swiper中文网中提供的教程做的Swiper轮播图 github地址
Html部分:

<template>
  <swiper :options="swiperOption" class="swiper-container swiper-pagination1"  ref="mySwiper">
          <!-- 添加的图片 -->
          <swiper-slide><img class="banner-img" src="https://res.vmallres.com/pimages//pages/picImages/E8poFW2xF0bISth9yLi7.jpg"/> </swiper-slide>
          <swiper-slide><img class="banner-img" src="https://res.vmallres.com/pimages//pages/picImages/0zJXgKgsDnZ8QoOF5rFH.jpg"/> </swiper-slide>
          <swiper-slide><img class="banner-img" src="https://res.vmallres.com/pimages//pages/picImages/5svSNfEBGG0Ento38IiB.jpg"/> </swiper-slide>
          <swiper-slide><img class="banner-img" src="https://res.vmallres.com/pimages//pages/picImages/bv5j4XkZk65XPmH6rtK5.png"/> </swiper-slide>
		  <swiper-slide><img class="banner-img" src="https://res.vmallres.com/pimages//pages/picImages/xgiGbFOMMqIfduyMkxOW.png"/> </swiper-slide>
		  <swiper-slide><img class="banner-img" src="https://res.vmallres.com/pimages//pages/picImages/tbWzBKicnSITdaMdohKg.jpg"/> </swiper-slide>
          <!-- Optional controls -->
          <div class="swiper-pagination"  slot="pagination"></div>
          <div class="swiper-button-prev" slot="button-prev"></div>
          <div class="swiper-button-next" slot="button-next"></div>
  </swiper>
</template>

Script部分:

<script>
  require('swiper/css/swiper.css');
  import { swiper, swiperSlide } from 'vue-awesome-swiper'
  import 'swiper/css/swiper.css'
  export default {
    components: {
      swiper,
      swiperSlide
    },
    data() {
      return {
        swiperOption: {
          pagination: '.swiper-pagination1',
          slidesPerView: 1,
          spaceBetween: 30,
          centeredSlides: false,
          spaceBetween: 0,
          onSlideChangeEnd: swiper => {
            //这个位置放swiper的回调方法
            this.page = swiper.realIndex+1;
            this.index = swiper.realIndex;
          },
          navigation: {
            nextEl: '.swiper-button-next',
            prevEl: '.swiper-button-prev',
          },
          //自动播放
          autoplay:{
            delay:5000,
            disableOnInteraction:false
          },
          pagination: {
            el: '.swiper-pagination',
            clickable: true,
          },
          //循环
          loop:true
        }
      }
    },
    //定义swiper对象
    computed: {
      swiper() {
        return this.$refs.mySwiper.swiper;
      }
    },
    mounted () {
      this.swiper.slideTo(0, 0, false);
    }
 
  }
</script>

自己另外加的Style:

<style>
	.swiper-container {
		cursor: pointer;
	}
	.swiper-pagination {
		float: right;
		margin-bottom: 65px;
	}
	.swiper-container-horizontal > .swiper-pagination-bullets {
	    bottom: 10px;
	    left: -172px;
	    width: 100%;
	}
	.swiper-button-prev {
		width:60px;
		height:60px;
		border-radius: 50%;
		background: #DCDCDC;
		opacity: 50%;		
		color: #FFFFFF;
		margin-top: -66px;
		margin-left: 370px;
	}
	.swiper-button-prev:hover {
		background:#A9A9A9;
	}
	.swiper-button-next {
		width:60px;
		height:60px;
		border-radius: 50%;
		background: #DCDCDC;
		opacity: 50%;
		color: #FFFFFF;
		margin-top: -66px;
		margin-right: 150px;
	}
	.swiper-button-next:hover{
		background:#A9A9A9;
	}
	.button-next {
		width:20px;
		height:20px;
		color: #FFFFFF;
	}
</style>

效果图:
在这里插入图片描述
为了还原与华为商城轮播图的相似度,更改了swiper.css中小圆点及左右按钮的属性。

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue 框架实现轮播图通常有以下几个步骤: 1. 引入轮播图插件:Vue 有很多轮播图插件可供选择,比如 `vue-awesome-swiper`、`vue-carousel` 等,你可以根据具体需求选择合适的插件。 2. 在组件使用轮播图插件:根据插件的使用文档,在组件引入轮播图插件,并在模板使用相应的标签。 3. 设置轮播图的数据源:通常轮播图需要一个数组来存储图片或者其他需要轮播的内容,你可以在组件的 `data` 选项定义这个数组,并在模板使用 `v-for` 指令来循环渲染轮播图。 4. 配置轮播图的参数:轮播图插件通常会提供一些可配置的参数,比如轮播间隔时间、是否自动播放、是否循环播放等等,你可以根据具体需求进行配置。 下面是一个使用 `vue-awesome-swiper` 插件实现轮播图的示例: ``` <template> <div class="swiper-container"> <div class="swiper-wrapper"> <div class="swiper-slide" v-for="item in list" :key="item.id"> <img :src="item.imgUrl" alt=""> </div> </div> <div class="swiper-pagination"></div> </div> </template> <script> import 'swiper/css/swiper.css' // 引入插件的样式文件 import Swiper from 'swiper' // 引入插件 export default { data() { return { list: [ { id: 1, imgUrl: 'http://xxx.com/1.jpg' }, { id: 2, imgUrl: 'http://xxx.com/2.jpg' }, { id: 3, imgUrl: 'http://xxx.com/3.jpg' }, { id: 4, imgUrl: 'http://xxx.com/4.jpg' }, ] } }, mounted() { // 初始化轮播图 new Swiper('.swiper-container', { autoplay: true, loop: true, pagination: { el: '.swiper-pagination', }, }) } } </script> ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值