vue3 实现 探探左滑不喜欢 右滑喜欢
Download ▾
Assistant
要实现探探风格的左滑不喜欢、右滑喜欢功能,可以借助 Vue3 的 Touch 事件和动画功能来实现。
首先,在你的组件中,可以使用 @touchstart、@touchmove 和 @touchend 这些 Touch 事件来处理滑动操作。
Vue3 中实现探探风格的左滑不喜欢、右滑喜欢功能的插件
Assistant
你可以使用现有的插件来实现探探风格的左滑不喜欢、右滑喜欢功能,例如 swiper 插件。这个插件为轮播图提供了丰富的手势交互功能,可以很方便地应用到你的探探风格相关功能中。
首先,你需要安装 swiper 插件:
code
npm install swiper
接下来,你可以在你的组件中使用 swiper 插件,并配置相应的参数来实现探探风格的左滑不喜欢、右滑喜欢功能。
swiper11+vue3 实现探探风格的左滑不喜欢、右滑喜欢功能的插件
Assistant
对不起,我之前提到的 swiper 插件并不支持 Vue 3,它是基于 Swiper.js v5 的插件,而 Swiper.js v5 目前还不支持 Vue 3。
推荐你使用 vue-awesome-swiper 插件,它基于 Swiper.js v6,完全支持 Vue 3,可以很方便地实现探探风格的左滑不喜欢、右滑喜欢功能。
首先,你需要安装 vue-awesome-swiper 插件:
bash
npm install vue-awesome-swiper
然后,你可以在你的组件中使用 vue-awesome-swiper 插件,并配置相应的参数来实现探探风格的左滑不喜欢、右滑喜欢功能。
<template>
<div class="swipe-card"
@touchstart="onTouchStart"
@touchmove="onTouchMove"
@touchend="onTouchEnd"
:style="cardStyle"
>
<!-- 卡片内容 -->
</div>
</template>
<script>
export default {
data() {
return {
startX: 0, // touchstart 时手指横坐标
startY: 0, // touchstart 时手指纵坐标
offsetX: 0, // touchmove 时的横向偏移量
cardStyle: {} // 卡片的样式,通过动态绑定应用到卡片元素上
};
},
methods: {
onTouchStart(event) {
this.startX = event.touches[0].clientX;
this.startY = event.touches[0].clientY;
},
onTouchMove(event) {
// 计算横向偏移量
this.offsetX = event.touches[0].clientX - this.startX;
// 更新卡片的样式,让卡片伴随手指滑动
this.cardStyle = {
transform: `translateX(${this.offsetX}px)`,
transition: 'none'
};
},
onTouchEnd(event) {
if (Math.abs(this.offsetX) > 50) {
// 如果滑动距离超过一定阈值,判定为滑动
if (this.offsetX > 0) {
// 滑动距离大于 0,右滑,喜欢
console.log('喜欢');
} else {
// 滑动距离小于 0,左滑,不喜欢
console.log('不喜欢');
}
// 重置卡片位置
this.cardStyle = {
transform: 'translateX(0)',
transition: 'transform 0.3s ease-in-out'
};
} else {
// 滑动距离不够,恢复原位
this.cardStyle = {
transform: 'translateX(0)',
transition: 'transform 0.3s ease-in-out'
};
}
// 清空 offsetX 值
this.offsetX = 0;
}
}
};
</script>
上述代码中,通过 @touchstart 事件获取滑动操作的起始坐标,并在 @touchmove 事件处理函数中计算横向偏移量,更新卡片的样式以实现卡片随手指滑动的效果。然后,在 @touchend 事件处理函数中,根据滑动距离的大小,判断用户是左滑还是右滑,并进行相应的操作(例如打印“喜欢”或“不喜欢”,或者触发其他功能)。最后,通过设置卡片的样式来重置卡片的位置。
通过上述实现,你可以在 Vue3 中实现探探风格的左滑不喜欢、右滑喜欢功能。
<template>
<div class="swiper-container">
<div class="swiper-wrapper">
<!-- 卡片内容 -->
<div class="swiper-slide" v-for="card in cards" :key="card.id">
{{ card.content }}
</div>
</div>
<div class="swiper-button-next"></div>
<div class="swiper-button-prev"></div>
</div>
</template>
<script>
import { Swiper, Navigation } from 'swiper';
import 'swiper/swiper-bundle.css';
Swiper.use([Navigation]);
export default {
data() {
return {
swiper: null, // swiper 实例
cards: [ /* 卡片数据 */ ]
};
},
mounted() {
this.initSwiper();
},
methods: {
initSwiper() {
this.swiper = new Swiper('.swiper-container', {
navigation: {
nextEl: '.swiper-button-next',
prevEl: '.swiper-button-prev'
},
on: {
slideChangeTransitionEnd: () => {
// 记录当前显示的卡片索引
const activeIndex = this.swiper.activeIndex;
if (activeIndex === 0 && this.swiper.isBeginning) {
// 第一张卡片
console.log('不喜欢');
} else if (activeIndex === this.cards.length - 1 && this.swiper.isEnd) {
// 最后一张卡片
console.log('喜欢');
}
}
}
});
}
}
};
</script>
上述代码中,我们引入了 Swiper 和 Navigation 两个对象,分别提供了 Swiper 和导航按钮功能。然后,在 mounted 钩子函数中初始化 swiper 实例,并通过设置 navigation 参数来添加导航按钮。在 on 事件回调中,我们监听了 slideChangeTransitionEnd 事件,当卡片切换完成时触发,判断当前显示的卡片索引是否处于极限位置,从而触发相应的操作(例如打印“喜欢”或“不喜欢”,或者触发其他功能)。
通过使用 swiper 插件,你可以更快速和便捷地实现探探风格的左滑不喜欢、右滑喜欢功能。
<template>
<div class="swiper-container">
<swiper
:options="swiperOptions"
@slideChangeTransitionEnd="onSlideChangeTransitionEnd"
>
<!-- 卡片内容 -->
<swiper-slide v-for="card in cards" :key="card.id">
{{ card.content }}
</swiper-slide>
</swiper>
</div>
</template>
<script>
import { Swiper, SwiperSlide } from 'vue-awesome-swiper';
import 'swiper/swiper-bundle.css';
export default {
components: {
Swiper,
SwiperSlide
},
data() {
return {
swiperOptions: {
loop: false,
navigation: true
},
cards: [ /* 卡片数据 */ ]
};
},
methods: {
onSlideChangeTransitionEnd() {
if (this.swiper.isBeginning) {
// 第一张卡片
console.log('不喜欢');
} else if (this.swiper.isEnd) {
// 最后一张卡片
console.log('喜欢');
}
}
}
};
</script>
上述代码中,我们引入了 Swiper 和 SwiperSlide 组件,并注册到组件中。然后,我们通过 vue-awesome-swiper 插件提供的 swiper 组件来包裹卡片内容。在 swiper 组件中,我们通过 :options 属性来传递 swiperOptions 对象,其中包含了 Swiper.js 的配置选项,可以根据需要进行调整。在 @slideChangeTransitionEnd 事件处理函数中,我们通过判断当前卡片索引是否处于极限位置,从而触发相应的操作(例如打印“喜欢”或“不喜欢”,或者触发其他功能)。
使用 vue-awesome-swiper 插件,你可以很轻松地在 Vue 3 中实现探探风格的左滑不喜欢、右滑喜欢功能。