怎么样用Vue写一个可复用轮播组件
Admin 2022-08-06 群英技术资讯 1027 次浏览
今天小编跟大家讲解下有关“怎么样用Vue写一个可复用轮播组件”的内容 ,相信小伙伴们对这个话题应该有所关注吧,小编也收集到了相关资料,希望小伙伴们看了有所帮助。本文用vue简单的实现了一个轮播图的基础功能,并抽离出来作为一个公共组件,方便复用
html和js部分
<template>
<div
class="img-box"
ref="img-box"
:style="{width: styles.width, height: styles.height}"
>
<div v-for="(item, index) in imgList"
:key="index"
class="img-item"
:ref="'img-item-' + index"
:class="{'active': index === active}"
>
<img
:src="item"
style="width:100%"
:style="{height: styles.height}"
/>
</div>
<div
class="img-position"
v-if="isShowPosition"
>
<template v-for="(item, index) in imgList">
<span :key="index"
class="img-position-item"
:ref="'img-position-' + index"
:class="[
{'active': index === active},
isCircle ? 'circle' : '',
isNums ? 'nums' : ''
]"
@click="clickSpan(index)"
>
{{isNums ? index + 1 : ''}}
</span>
</template>
</div>
<div
class="left-btn"
v-if="isShowLeftOrRightBtn"
@click="clickBtn('left')"
>
<i class="iconfont roll-zuo"></i>
</div>
<div
class="right-btn"
v-if="isShowLeftOrRightBtn"
@click="clickBtn('right')"
>
<i class="iconfont roll-you"></i>
</div>
</div>
</template>
<script>
export default {
name: 'Roll',
props: {
imgList: { // 图片列表 src数组
type: Array,
default: () => []
},
isShowPosition: { // 是否显示下方小圆点
type: Boolean,
default: true
},
positionInner: { // 圆点内容
type: String,
default: 'circle' // 默认圆点,可选值 circle => 圆点 num => 数字 both => 圆点+数字
},
isShowLeftOrRightBtn: { // 是否显示左右按钮
type: Boolean,
default: true
},
duration: { // 切换间隔
type: [Number, String],
default: 3000
},
styles: { // 自定义轮播图片宽高 默认500*300
type: Object,
default: () => {
return {
width: '500px',
height: '300px'
}
}
}
},
data () {
return {
active: 0, // 当前轮播图片
timer: null // 定时器
}
},
computed: {
isCircle () {
return ['circle', 'both'].includes(this.positionInner)
},
isNums () {
return ['num', 'both'].includes(this.positionInner)
}
},
updated () {
if (this.timer) this.clearTimer()
this.setTimer()
},
created () {
this.setTimer()
},
methods: {
clickSpan (index) {
this.clearTimer()
this.active = index
},
clickBtn (arg) {
this.clearTimer()
if (arg === 'left') {
this.active = this.active - 1 < 0 ? this.imgList.length - 1 : this.active - 1
} else {
this.active = this.active + 1 === this.imgList.length ? 0 : this.active + 1
}
this.setTimer()
},
setTimer () {
this.timer = setInterval(() => {
this.clickBtn('right')
}, Number(this.duration))
},
clearTimer () {
clearInterval(this.timer)
this.timer = null
}
}
}
</script>
css样式部分
<style scoped>
@import url('//at.alicdn.com/t/font_1451815_senarwrqu6.css');
* {
margin: 0;
padding: 0;
}
.img-box {
position: relative;
margin: 0 auto;
}
.img-item {
height: 100%;
width: 100%;
opacity: 0;
position: absolute;
top: 0;
right: 0;
left: 0;
bottom: 0;
z-index: -5;
text-align: center;
}
.img-item.active {
z-index: 0;
opacity: 1;
transition: .3s;
}
.img-position {
position: absolute;
bottom: 5px;
left: 50%;
display: flex;
transform: translate(-50%, 0);
}
.img-position-item {
display: inline-block;
width:10px;
height:10px;
box-sizing: border-box;
cursor: pointer;
}
.img-position-item.circle {
border-radius: 50%;
border: 1px solid #606266;
}
.img-position-item.nums {
width: 18px;
height: 18px;
display: flex;
justify-content: center;
align-items: center;
color: #606266;
font-size:14px;
}
.img-position-item:hover, .img-position-item.active {
border-color: #d1d2d3;
color: #d1d2d3;
transition: .3s;
}
.img-position-item + .img-position-item {
margin-left: 10px;
}
.left-btn, .right-btn {
position: absolute;
top: 50%;
bottom: 0;
width: 20px;
height: 30px;
display: flex;
justify-content: center;
align-items: center;
cursor: pointer;
color: #d1d2d3;
font-size: 20px;
transform: translate(0, -50%);
}
.left-btn:hover, .right-btn:hover {
color: #fff;
transition: .3s;
}
.left-btn {
left: 5px;
}
.right-btn {
right: 5px;
}
</style>
只是简单的实现了一个轮播图比较基础的部分,之前用原生写了一遍,现在用vue写一遍作为一个组件,也还不错。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
猜你喜欢
目录不同环境下如何配置代理第一种情况第二种情况Vue设置本地代理本地安装Whistle谷歌浏览器安装Proxy SwitchyOmega扩展浏览器操作不同环境下如何配置代理实际开发的时候,经常会遇到并行开发的需求,会出现第一个需求接口转发是一个地址,下一个需求接口转发是一个地址,这时候如何解决呢?第一种情况前端也不申请
本篇文章带大家用Node.js探索一下反应式编程,介绍一下在Node中应用反应式编程的方法,以及它的好处和利弊,希望对大家有所帮助!
很多朋友在token过期时刷新页面,页面长时间未操作,再刷新页面时,第一次弹出“token失效,请重新登录!”提示,针对这个问题该怎么处理呢,下面小编给大家带来原因分析及解决方法,一起看看吧
TypeScript中泛型是什么意思?怎样使用?对新手来说,可能对泛型和泛型的用法不是很了解,对此这篇文章给大家分享TypeScript中泛型的内容。小编觉得挺实用的,因此给大家做个参考,感兴趣的朋友接着往下看。
排他思想的算法就是排除掉其他的,本文主要介绍了JavaScript 排他思想的实现,以及介绍了两个示例,感兴趣的可以了解一下
成为群英会员,开启智能安全云计算之旅
立即注册关注或联系群英网络
7x24小时售前:400-678-4567
7x24小时售后:0668-2555666
24小时QQ客服
群英微信公众号
CNNIC域名投诉举报处理平台
服务电话:010-58813000
服务邮箱:service@cnnic.cn
投诉与建议:0668-2555555
Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 ICP核准(ICP备案)粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008