Vue2(单页面组件)+swiper.js实现轮播图

1.安装swiper

npm i --save swiper

2.封装swiper组件,以组件方式在单页面中使用

2.1在components文件夹下,创建swiper文件夹,创建MySwiper.vue页面:

<template>
  <div class="swiper">
    <div class="swiper-wrapper">
    //插槽写法,插入swiper-slider组件
    <slot></slot>
  </div> 
  </div>
</template>
<script>
// 1.bundle 引入swiper 所有模块,如果引入默认版本的,没有分页器
import Swiper from 'swiper/bundle'
import 'swiper/css/bundle';
export default {
  mounted(){
     //2.初始化swiper
      new Swiper ('.swiper', {
        direction: 'horizontal', // 水平切换选项
        loop: true, // 循环模式选项
    
        // 如果需要分页器
        pagination: {
          el: '.swiper-pagination',
        },
        autoplay: true,
        // 如果需要前进后退按钮
        navigation: {
          nextEl: '.swiper-button-next',
          prevEl: '.swiper-button-prev',
        },
     
  }) 
},       
}
</script>
<style scoped>
.swiper{
  height:500px;
}
</style>

2.2在swiper文件夹,创建MySwiperSlider.vue页面:

<template>
  <div class="swiper-slide">
    //插槽写法:template可以写任意东西
    <slot></slot>
  </div> 
</template>
<script>

export default {
 
}
</script>

<style scoped>

</style>

3.使用方法

3.1 需求:首页(MyIndex.vue)中需要引入swiper组件

3.2 在MyIndex.vue页面中:

<template>
 //3.使用组件
  <MySwiperViewVue>
      <template #default>
        <MySwiperSlider v-for="itemImg,index in imgUrls" :key="index">
          <template #default>
            <div :style="{'backgroundImage':`url(${itemImg})`}" class="img-box"></div>
          </template>
       
        </MySwiperSlider>
      </template>
    </MySwiperViewVue>
</template>
<script>
//1.引入组件
import MySwiperViewVue from '@/components/swiper/MySwiper.vue'
import MySwiperSlider from '@/components/swiper/MySwiperSlider.vue';
export default {
data(){
  return {
      //4.图片数据,随便换
      imgUrls:[
          "https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fsafe-img.xhscdn.com%2Fbw1%2F59616f4c-a9f9-4ba3-9eec-c85ff57f9adb%3FimageView2%2F2%2Fw%2F1080%2Fformat%2Fjpg&refer=http%3A%2F%2Fsafe-img.xhscdn.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1686628880&t=a7031371aabfdb057978f319a0465b79",
          "https://pica.zhimg.com/v2-e1d73be07fa6624bfcb4590f690d68d3_1440w.jpg",
          "https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fc-ssl.duitang.com%2Fuploads%2Fitem%2F201903%2F15%2F20190315130031_sxkjt.jpeg&refer=http%3A%2F%2Fc-ssl.duitang.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1686629020&t=dd9c3251d08693533af7c4bc3cf6b320"
]
  }
},
components:{
  //2.注册组件
  MySwiperViewVue,MySwiperSlider
}
}
</script>
<style scoped>
.img-box{
width:100%;
height:100%;
background-position: top;
}
</style>

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
引用\[1\]:根据提供的引用内容,版本3.1.3是vue-awesome-swiper的一个旧版本。引用\[2\]中提到了如何全局引入vue-awesome-swiper,并且需要引入swiper的CSS文件。引用\[3\]中展示了如何在Vue组件中使用vue-awesome-swiper实现轮播图。根据您的需求,您想要在切换slide时更换app组件的背景。 为了实现这个需求,您可以在vue-awesome-swiper的slide切换事件中,通过修改app组件的背景样式来实现背景的更换。您可以在Vue组件中监听swiper的slideChange事件,并在事件回调函数中修改app组件的背景样式。 下面是一个示例代码,展示了如何实现这个需求: ```javascript <template> <div id="home-swiper"> <swiper ref="homeSwiper" :options="homeSwiperOptions" @slideChange="handleSlideChange"> <swiper-slide>...</swiper-slide> <swiper-slide>...</swiper-slide> </swiper> </div> </template> <script> import { swiper, swiperSlide } from "vue-awesome-swiper"; import "swiper/dist/css/swiper.css"; export default { components: { swiper, swiperSlide, }, methods: { handleSlideChange() { // 在这里修改app组件的背景样式 // 例如:this.$root.$el.style.background = "red"; }, }, }; </script> ``` 在handleSlideChange方法中,您可以根据需要修改app组件的背景样式。例如,您可以使用`this.$root.$el.style.background`来修改app组件的背景颜色。请根据您的具体需求进行相应的修改。 希望这个示例能够帮助您实现轮播图并更换app组件的背景。如果您有任何其他问题,请随时提问。 #### 引用[.reference_title] - *1* [【npm install vue-awesome-swiper@3.1.3 -S 】下载成功但是vue-awesome-swiper 用不了](https://blog.csdn.net/Sonnenlicht77/article/details/126951340)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [关于swiper的两种用法(swiper@4.0.7 vue-awesome-swiper@3.1.3)](https://blog.csdn.net/weixin_52259399/article/details/129066576)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [vue-awesome-swiper @3.1.3使用,记录一些bug及解决方法](https://blog.csdn.net/gegegegege12/article/details/121387965)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值