本文实例为大家分享了vue swipe自定义组件实现轮播效果的具体代码,供大家参考,具体内容如下
<template>
<layout-div :style="getStyle" class="over-h posi-r">
<layout-div :style="getChildStyle" class="flex" @load="loadHandle">
<slot/>
</layout-div>
<layout-div class="flex-center flex posi-a b0 l0 r0" :height="40" :unit="unit" v-if="!$slots.point">
<layout-div class="flex">
<layout-div v-for="i in this.length" :key="i" class="op-5" :mar="[0,15]" :height="20" :width="20"
:bg="Math.abs(index) == i-1" :fillet="20" :unit="unit"/>
</layout-div>
</layout-div>
<slot v-else name="point"/>
</layout-div>
</template>
<script>
export default {
props: {
value: {
type: Number,
default: 0
},
width: { //宽
type: Number,
default: null
},
height: { //高
type: Number,
default: null,
},
unit: { //单位
type: String,
default: 'px'
},
time: { //时间(毫秒)
type: Number,
default: 3000
},
direction: { //方向 X | Y (注意是:大写)
type: String,
default: "X"
},
duration: { //每次切换的时间(毫秒)
type: Number,
default: 300
},
autoPlay: { //是否自动轮播
type: Boolean,
default: true
}
},
data() {
return {
style: {},
multiple: 50, //倍数处理,更加UI进行 100倍是按照 750*1334 的UI
dom: null, //当前swipe元素
length: 0, //子元素的数量
countWidth: 0, //总长度
index: 0,
isTouch: false,// 手指是否在屏幕上
}
},
methods: {
loadHandle(e) {
this.dom = e.dom
this.length = e.dom.childElementCount
for (let i = 0; i < this.length; i++) {
this.dom.children[i].style.height = this.height "(")[1]) || 0
},
move({dx}) {
dom.style.transform = `translateX(${dx + tranX + self.unit})`;
},
change({direction}) {
switch (direction) {
case 'left':
self.index--;
break;
}
self.moveHandle()
}
})
},
moveHandle() { //移动
const Y = parseFloat(this.dom.style.width) / this.length
this.dom.style.transitionDuration = `${this.duration}ms`
this.dom.style.transform = `translateX(${this.index * Y + this.unit})`;
setTimeout(() => {
if (!this.isTouch) {
if (Math.abs(this.index) == this.length - 1) {
// console.log("要开始调换位置了");
const first = this.dom.firstChild;
first.style.transform = `translateX(${(Math.abs(-this.index) + 1) * Y + this.unit})`;
} else if (Math.abs(this.index) == this.length) {
// console.log("开始下一轮了")
const first = this.dom.firstChild;
this.dom.style.transitionDuration = '0ms'
this.dom.style.transform = `translateX(${0 + this.unit})`;
this.index = 0;
first.style.transform = `translateX(${0 + this.unit})`;
}
}
this.$emit('input', Math.abs(this.index))
}, this.duration)
},
beginSwipe() {
setTimeout(() => {
if (this.isTouch) return this.beginSwipe(); //如果是手指在移动,就不再执行
this.index--
this.moveHandle()
this.beginSwipe()
}, this.time > this.duration "htmlcode">
.over-h {
overflow: hidden;
}
.posi-r {
position: relative;
z-index: 0;
}
.flex {
display: flex;
}
.flex-center {
justify-content: center;
align-items: center;
}
.posi-a {
position: absolute;
z-index: 0;
}
.b0 {
bottom: 0;
}
.l0 {
left: 0;
}
.r0 {
right: 0;
}
.op-5 {
opacity: 0.5
}
.font-68 {
font-size: 0.68rem;
}
.col-f {
color: #fff;
}
示例代码
<layout-swipe :height="240" :time="2000" unit="rem">
<layout-div bg="red" class="flex-center flex font-68 col-f">1</layout-div>
<layout-div bg="yellow" class="flex-center flex font-68 col-f">2</layout-div>
<layout-div bg="green" class="flex-center flex font-68 col-f">3</layout-div>
</layout-swipe>
效果图
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
标签:
vue,swipe,轮播
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件!
如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
暂无“vue swipe自定义组件实现轮播效果”评论...
更新动态
2025年11月05日
2025年11月05日
- 小骆驼-《草原狼2(蓝光CD)》[原抓WAV+CUE]
- 群星《欢迎来到我身边 电影原声专辑》[320K/MP3][105.02MB]
- 群星《欢迎来到我身边 电影原声专辑》[FLAC/分轨][480.9MB]
- 雷婷《梦里蓝天HQⅡ》 2023头版限量编号低速原抓[WAV+CUE][463M]
- 群星《2024好听新歌42》AI调整音效【WAV分轨】
- 王思雨-《思念陪着鸿雁飞》WAV
- 王思雨《喜马拉雅HQ》头版限量编号[WAV+CUE]
- 李健《无时无刻》[WAV+CUE][590M]
- 陈奕迅《酝酿》[WAV分轨][502M]
- 卓依婷《化蝶》2CD[WAV+CUE][1.1G]
- 群星《吉他王(黑胶CD)》[WAV+CUE]
- 齐秦《穿乐(穿越)》[WAV+CUE]
- 发烧珍品《数位CD音响测试-动向效果(九)》【WAV+CUE】
- 邝美云《邝美云精装歌集》[DSF][1.6G]
- 吕方《爱一回伤一回》[WAV+CUE][454M]
