前端Vue自定义简单实用轮播图封装组件 快速实现轮播图

随着技术的发展,开发的复杂度也越来越高,传统开发方式将一个系统做成了整块应用,经常出现的情况就是一个小小的改动或者一个小功能的增加可能会引起整体逻辑的修改,造成牵一发而动全身。通过组件化开发,可以有效实现单独开发,单独维护,而且他们之间可以随意的进行组合。大大提升开发效率低,降低维护成本。

组件化对于任何一个业务场景复杂的前端应用以及经过多次迭代之后的产品来说都是必经之路。组件化要做的不仅仅是表面上看到的模块拆分解耦,其背后还有很多工作来支撑组件化的进行,例如结合业务特性的模块拆分策略、模块间的交互方式和构建系统等等 。

本文给大家介绍的组件:

前端Vue自定义简单实用轮播图封装组件 快速实现轮播图,

阅读全文下载完整组件代码请关注微信公众号: 前端组件开发

 

d848d5658a07453c843277846948c608.png

效果图如下:

 

912f653adf59c6c867f99d2f2d7476e5.png

5daa1293f2e35a54cffdbc8ec2a4c12b.png

# cc-mySwiper

#### 使用方法

```使用方法

<!-- 自定义轮播图 swiperArr: 轮播数组  @swiperItemClick: 轮播图条目点击-->

<cc-mySwiper :swiperArr="banner" @swiperItemClick="swiperItemClick"></cc-mySwiper>

```

#### HTML代码实现部分

```html

<template>

<view class="content">

<!-- menuArr:导航菜单栏  @leftClick: 左导航按钮事件点击  @rigClick: 右导航按钮事件点击-->

<cc-navHeader :menuArr="menuArr" @leftClick="leftClick" @rigClick="rigClick"></cc-navHeader>

<!-- 自定义顶部搜索框 用于搜索跳转 skipUrl:跳转url为绝对路径 /pages开头 -->

<cc-headSearch skipUrl="/pages/index/search"></cc-headSearch>

<!-- 自定义轮播图 swiperArr: 轮播数组  @swiperItemClick: 轮播图条目点击-->

<cc-mySwiper :swiperArr="banner" @swiperItemClick="swiperItemClick"></cc-mySwiper>

</view>

</template>

<script>

export default {

data() {

return {

menuArr: [{

"id": "1",

"menu": "手机",

"url": "/pages/phone/phone"

},

{

"id": "2",

"menu": "升学",

"url": "/pages/study/study"

},

{

"id": "3",

"menu": "配件",

"url": "/pages/parts/parts"

},

{

"id": "4",

"menu": "生活",

"url": "/pages/life/life"

}

],

banner: [{

"id": 1,

"image": "/static/image/banner1.jpg"

},

{

"id": 2,

"image": "/static/image/banner2.jpg"

},

{

"id": 3,

"image": "/static/image/banner3.jpg"

},

{

"id": 4,

"image": "/static/image/banner4.jpg"

}

]

}

},

mounted() {

},

methods: {

swiperItemClick(item){

uni.showModal({

title: '点击轮播图',

content: '点击轮播图数据  = ' + JSON.stringify(item)

})

},

leftClick(item) {

uni.showModal({

title: '点击导航栏按钮',

content: '点击导航栏左侧搜索按钮 '

})

},

rigClick(flag) {

if (flag == 0) {

uni.showModal({

title: '点击导航栏按钮',

content: '点击导航右侧购物车按钮 '

})

} else {

uni.showModal({

title: '点击导航栏按钮',

content: '点击导航右侧更多按钮 '

})

}

}

}

}

</script>

<style>

page {

background-color: #f7f7f7;

}

.content {

display: flex;

flex-direction: column;

}

</style>

```

 

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Vue轮播图组件封装是一种常见的前端开发技术,它可以实现在网页中展示多张图片,并通过组件间的数据通信来实现图片切换等功能。以下是一个简单Vue轮播图组件封装的示例: 1. 创建一个Vue组件,命为Carousel: ```javascript <template> < class="carousel"> <img :src="currentImage" alt="carousel image" /> <button @click="prev">Previous</button> <button @click="next">Next</button> </div> </template> <script> export default { data() { return { images: [ "image1.jpg", "image2.jpg", "image3.jpg" ], currentIndex: 0 }; }, computed: { currentImage() { return this.images[this.currentIndex]; } }, methods: { prev() { this.currentIndex = (this.currentIndex - 1 + this.images.length) % this.images.length; }, next() { this.currentIndex = (this.currentIndex + 1) % this.images.length; } } }; </script> <style scoped> .carousel { width: 100%; height: 300px; position: relative; } .carousel img { width: 100%; height: 100%; object-fit: cover; } .carousel button { position: absolute; top: 50%; transform: translateY(-50%); } </style> ``` 2. 在父组件中使用Carousel组件,并通过props属性传递数据: ```javascript <template> <div> <carousel :images="carouselImages"></carousel> </div> </template> <script> import Carousel from "@/components/Carousel.vue"; export default { data() { return { carouselImages: [ "image1.jpg", "image2.jpg", "image3.jpg" ] }; }, components: { Carousel } }; </script> ``` 在上述示例中,Carousel组件通过props属性接收父组件传递的images数据,并在computed属性中根据currentIndex动态计算当前显示的图片路径。通过点击按钮触发prev和next方法来切换图片。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

前端组件开发

你的钟意将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值