前言

嗨,说起探探想必各位程序汪都不陌生(毕竟妹子很多),能在上面丝滑的翻牌子,探探的的堆叠滑动组件起到了关键的作用,下面就来看看如何用vue写一个探探的堆叠组件 ?

一. 功能分析

简单使用下探探会发现,堆叠滑动的功能很简单,用一张图概括就是:


简单归纳下里面包含的基本功能点:

  • 图片的堆叠
  • 图片第一张的滑动
  • 条件成功后的滑出,条件失败后的回弹
  • 滑出后下一张图片堆叠到顶部

体验优化

  • 根据触摸点的不同,滑动时首图有不同角度偏移
  • 偏移面积判定是否成功滑出

二. 具体实现

有了归纳好的功能点,我们实现组件的思路会更清晰

1. 堆叠效果

堆叠图片效果在网上有大量的实例,实现的方法大同小异,主要通过在父层设定perspective及perspective-origin,来实现子层的透视,子层设定好translate3d Z轴数值即可模拟出堆叠效果,具体代码如下

// 图片堆叠dom<!--opacity: 0 隐藏我们不想看到的stack-item层级--><!--z-index: -1 调整stack-item层级"-->
<ul class="stack"><li class="stack-item" style="transform: translate3d(0px, 0px, 0px);opacity: 1;z-index: 10;"><img src="1.png" alt="01"></li><li class="stack-item" style="transform: translate3d(0px, 0px, -60px);opacity: 1;z-index: 1"><img src="2.png" alt="02"></li><li class="stack-item" style="transform: translate3d(0px, 0px, -120px);opacity: 1;z-index: 1"><img src="3.png" alt="03"></li><li class="stack-item" style="transform: translate3d(0px, 0px, -180px);opacity: 0;z-index: -1"><img src="4.png" alt="04"></li><li class="stack-item" style="transform: translate3d(0px, 0px, -180px);opacity: 0;z-index: -1"><img src="5.png" alt="05"></li>
</ul>
<style>
.stack {width: 100%;height: 100%;position: relative;perspective: 1000px; //子元素视距perspective-origin: 50% 150%; //子元素透视位置-webkit-perspective: 1000px;-webkit-perspective-origin: 50% 150%;margin: 0;padding: 0;}.stack-item{background: #fff;height: 100%;width: 100%;border-radius: 4px;text-align: center;overflow: hidden;}.stack-item img {width: 100%;display: block;pointer-events: none;}
</style>

上面只是一组静态代码,我们希望得到的是vue组件,所以需要先建立一个组件模板stack.vue,在模板中我们可以使用v-for,遍历出stack节点,使用:style 来修改各个item的style,代码如下

<template><ul class="stack"><li class="stack-item" v-for="(item, index) in pages" :style="[transform(index)]"><img :src="item.src"></li></ul>
</template>
<script>
export default {props: {// pages数据包含基础的图片数据pages: {type: Array,default: []}},data () {return {// basicdata数据包含组件基本数据basicdata: {currentPage: 0 // 默认首图的序列},// temporaryData数据包含组件临时数据temporaryData: {opacity: 1, // 记录opacityzIndex: 10, // 记录zIndexvisible: 3 // 记录默认显示堆叠数visible}}},methods: {// 遍历样式transform (index) {if (index >= this.basicdata.currentPage) {let style = {}let visible = this.temporaryData.visiblelet perIndex = index - this.basicdata.currentPage// visible可见数量前滑块的样式if (index <= this.basicdata.currentPage   visible - 1) {style['opacity'] = '1'style['transform'] = 'translate3D(0,0,'   -1 * perIndex * 60   'px'   ')'style['zIndex'] = visible - index   this.basicdata.currentPagestyle['transitionTimingFunction'] = 'ease'style['transitionDuration'] = 300   'ms'} else {style['zIndex'] = '-1'style['transform'] = 'translate3D(0,0,'   -1 * visible * 60   'px'   ')'}return style}}}
}
</script>

关键点

  • :style可以绑定对象的同时,也可以绑定数组和函数,这在遍历的时候很有用

最基本的dom结构已经构建完毕,下一步是让首张图片“动”起来

2. 图片滑动

图片滑动效果,在很多场景中都有出现,其原理无非是监听touchs事件,得到位移,再通过translate3D改变目标位移,因此我们要实现的步骤如下

  • 对stack进行touchs事件的绑定
  • 监听并储存手势位置变化的数值
  • 改变首图css属性中translate3D的x,y值

具体实现

在vue框架中,不建议直接操作节点,而是通过指令v-on对元素进行绑定,因此我们将绑定都写在v-for遍历里,通过index进行判断其是否是首图,再使用:style修改首页的样式,具体代码如下:

<template><ul class="stack"><li class="stack-item" v-for="(item, index) in pages":style="[transformIndex(index),transform(index)]"@touchstart.stop.capture="touchstart"@touchmove.stop.capture="touchmove"@touchend.stop.capture="touchend"@mousedown.stop.capture="touchstart"@mouseup.stop.capture="touchend"@mousemove.stop.capture="touchmove"><img :src="item.src"></li></ul>
</template>
<script>
export default {props: {// pages数据包含基础的图片数据pages: {type: Array,default: []}},data () {return {// basicdata数据包含组件基本数据basicdata: {start: {}, // 记录起始位置end: {}, // 记录终点位置currentPage: 0 // 默认首图的序列},// temporaryData数据包含组件临时数据temporaryData: {poswidth: '', // 记录位移posheight: '', // 记录位移tracking: false // 是否在滑动,防止多次操作,影响体验}}},methods: {touchstart (e) {if (this.temporaryData.tracking) {return}// 是否为touchif (e.type === 'touchstart') {if (e.touches.length > 1) {this.temporaryData.tracking = falsereturn} else {// 记录起始位置this.basicdata.start.t = new Date().getTime()this.basicdata.start.x = e.targetTouches[0].clientXthis.basicdata.start.y = e.targetTouches[0].clientYthis.basicdata.end.x = e.targetTouches[0].clientXthis.basicdata.end.y = e.targetTouches[0].clientY}// pc操作} else {this.basicdata.start.t = new Date().getTime()this.basicdata.start.x = e.clientXthis.basicdata.start.y = e.clientYthis.basicdata.end.x = e.clientXthis.basicdata.end.y = e.clientY}this.temporaryData.tracking = true},touchmove (e) {// 记录滑动位置if (this.temporaryData.tracking && !this.temporaryData.animation) {if (e.type === 'touchmove') {this.basicdata.end.x = e.targetTouches[0].clientXthis.basicdata.end.y = e.targetTouches[0].clientY} else {this.basicdata.end.x = e.clientXthis.basicdata.end.y = e.clientY}// 计算滑动值this.temporaryData.poswidth = this.basicdata.end.x - this.basicdata.start.xthis.temporaryData.posheight = this.basicdata.end.y - this.basicdata.start.y}},touchend (e) {this.temporaryData.tracking = false// 滑动结束,触发判断},// 非首页样式切换transform (index) {if (index > this.basicdata.currentPage) {let style = {}let visible = 3let perIndex = index - this.basicdata.currentPage// visible可见数量前滑块的样式if (index <= this.basicdata.currentPage   visible - 1) {style['opacity'] = '1'style['transform'] = 'translate3D(0,0,'   -1 * perIndex * 60   'px'   ')'style['zIndex'] = visible - index   this.basicdata.currentPagestyle['transitionTimingFunction'] = 'ease'style['transitionDuration'] = 300   'ms'} else {style['zIndex'] = '-1'style['transform'] = 'translate3D(0,0,'   -1 * visible * 60   'px'   ')'}return style}},// 首页样式切换transformIndex (index) {// 处理3D效果if (index === this.basicdata.currentPage) {let style = {}style['transform'] = 'translate3D('   this.temporaryData.poswidth   'px'   ','   this.temporaryData.posheight   'px'   ',0px)'style['opacity'] = 1style['zIndex'] = 10return style}}}
}
</script>

3. 条件成功后的滑出,条件失败后的回弹

条件的触发判断是在touchend/mouseup后进行,在这里我们先用简单的条件进行判定,同时给予首图弹出及回弹的效果,代码如下

<template><ul class="stack"><li class="stack-item" v-for="(item, index) in pages":style="[transformIndex(index),transform(index)]"@touchmove.stop.capture="touchmove"@touchstart.stop.capture="touchstart"@touchend.stop.capture="touchend"@mousedown.stop.capture="touchstart"@mouseup.stop.capture="touchend"@mousemove.stop.capture="touchmove"><img :src="item.src"></li></ul>
</template>
<script>
export default {props: {// pages数据包含基础的图片数据pages: {type: Array,default: []}},data () {return {// basicdata数据包含组件基本数据basicdata: {start: {}, // 记录起始位置end: {}, // 记录终点位置currentPage: 0 // 默认首图的序列},// temporaryData数据包含组件临时数据temporaryData: {poswidth: '', // 记录位移posheight: '', // 记录位移tracking: false, // 是否在滑动,防止多次操作,影响体验animation: false, // 首图是否启用动画效果,默认为否opacity: 1 // 记录首图透明度}}},methods: {touchstart (e) {if (this.temporaryData.tracking) {return}// 是否为touchif (e.type === 'touchstart') {if (e.touches.length > 1) {this.temporaryData.tracking = falsereturn} else {// 记录起始位置this.basicdata.start.t = new Date().getTime()this.basicdata.start.x = e.targetTouches[0].clientXthis.basicdata.start.y = e.targetTouches[0].clientYthis.basicdata.end.x = e.targetTouches[0].clientXthis.basicdata.end.y = e.targetTouches[0].clientY}// pc操作} else {this.basicdata.start.t = new Date().getTime()this.basicdata.start.x = e.clientXthis.basicdata.start.y = e.clientYthis.basicdata.end.x = e.clientXthis.basicdata.end.y = e.clientY}this.temporaryData.tracking = truethis.temporaryData.animation = false},touchmove (e) {// 记录滑动位置if (this.temporaryData.tracking && !this.temporaryData.animation) {if (e.type === 'touchmove') {this.basicdata.end.x = e.targetTouches[0].clientXthis.basicdata.end.y = e.targetTouches[0].clientY} else {this.basicdata.end.x = e.clientXthis.basicdata.end.y = e.clientY}// 计算滑动值this.temporaryData.poswidth = this.basicdata.end.x - this.basicdata.start.xthis.temporaryData.posheight = this.basicdata.end.y - this.basicdata.start.y}},touchend (e) {this.temporaryData.tracking = falsethis.temporaryData.animation = true// 滑动结束,触发判断// 简单判断滑动宽度超出100像素时触发滑出if (Math.abs(this.temporaryData.poswidth) >= 100) {// 最终位移简单设定为x轴200像素的偏移let ratio = Math.abs(this.temporaryData.posheight / this.temporaryData.poswidth)this.temporaryData.poswidth = this.temporaryData.poswidth >= 0 ? this.temporaryData.poswidth   200 : this.temporaryData.poswidth - 200this.temporaryData.posheight = this.temporaryData.posheight >= 0 ? Math.abs(this.temporaryData.poswidth * ratio) : -Math.abs(this.temporaryData.poswidth * ratio)this.temporaryData.opacity = 0// 不满足条件则滑入} else {this.temporaryData.poswidth = 0this.temporaryData.posheight = 0}},// 非首页样式切换transform (index) {if (index > this.basicdata.currentPage) {let style = {}let visible = 3let perIndex = index - this.basicdata.currentPage// visible可见数量前滑块的样式if (index <= this.basicdata.currentPage   visible - 1) {style['opacity'] = '1'style['transform'] = 'translate3D(0,0,'   -1 * perIndex * 60   'px'   ')'style['zIndex'] = visible - index   this.basicdata.currentPagestyle['transitionTimingFunction'] = 'ease'style['transitionDuration'] = 300   'ms'} else {style['zIndex'] = '-1'style['transform'] = 'translate3D(0,0,'   -1 * visible * 60   'px'   ')'}return style}},// 首页样式切换transformIndex (index) {// 处理3D效果if (index === this.basicdata.currentPage) {let style = {}style['transform'] = 'translate3D('   this.temporaryData.poswidth   'px'   ','   this.temporaryData.posheight   'px'   ',0px)'style['opacity'] = this.temporaryData.opacitystyle['zIndex'] = 10if (this.temporaryData.animation) {style['transitionTimingFunction'] = 'ease'style['transitionDuration'] = 300   'ms'}return style}}}
}
</script>

4. 滑出后下一张图片堆叠到顶部

重新堆叠是组件最后一个功能,同时也是最重要和复杂的功能。在我们的代码里,stack-item的排序依赖绑定:style的transformIndex和transform函数,函数里判定的条件是currentPage,那是不是改变currentPage,让其 1,即可完成重新堆叠呢?

答案没有那么简单,因为我们滑出是动画效果,会进行300ms的时间,而currentPage变化引起的重排,会立即变化,打断动画的进行。因此我们需要先修改transform函数的排序条件,后改变currentPage。

具体实现

  • 修改transform函数排序条件
  • 让currentPage 1
  • 添加onTransitionEnd事件,在滑出结束后,重新放置stack列表中

代码如下:

<template><ul class="stack"><li class="stack-item" v-for="(item, index) in pages":style="[transformIndex(index),transform(index)]"@touchmove.stop.capture="touchmove"@touchstart.stop.capture="touchstart"@touchend.stop.capture="touchend"@mousedown.stop.capture="touchstart"@mouseup.stop.capture="touchend"@mousemove.stop.capture="touchmove"@webkit-transition-end="onTransitionEnd"@transitionend="onTransitionEnd"><img :src="item.src"></li></ul>
</template>
<script>
export default {props: {// pages数据包含基础的图片数据pages: {type: Array,default: []}},data () {return {// basicdata数据包含组件基本数据basicdata: {start: {}, // 记录起始位置end: {}, // 记录终点位置currentPage: 0 // 默认首图的序列},// temporaryData数据包含组件临时数据temporaryData: {poswidth: '', // 记录位移posheight: '', // 记录位移lastPosWidth: '', // 记录上次最终位移lastPosHeight: '', // 记录上次最终位移tracking: false, // 是否在滑动,防止多次操作,影响体验animation: false, // 首图是否启用动画效果,默认为否opacity: 1, // 记录首图透明度swipe: false // onTransition判定条件}}},methods: {touchstart (e) {if (this.temporaryData.tracking) {return}// 是否为touchif (e.type === 'touchstart') {if (e.touches.length > 1) {this.temporaryData.tracking = falsereturn} else {// 记录起始位置this.basicdata.start.t = new Date().getTime()this.basicdata.start.x = e.targetTouches[0].clientXthis.basicdata.start.y = e.targetTouches[0].clientYthis.basicdata.end.x = e.targetTouches[0].clientXthis.basicdata.end.y = e.targetTouches[0].clientY}// pc操作} else {this.basicdata.start.t = new Date().getTime()this.basicdata.start.x = e.clientXthis.basicdata.start.y = e.clientYthis.basicdata.end.x = e.clientXthis.basicdata.end.y = e.clientY}this.temporaryData.tracking = truethis.temporaryData.animation = false},touchmove (e) {// 记录滑动位置if (this.temporaryData.tracking && !this.temporaryData.animation) {if (e.type === 'touchmove') {this.basicdata.end.x = e.targetTouches[0].clientXthis.basicdata.end.y = e.targetTouches[0].clientY} else {this.basicdata.end.x = e.clientXthis.basicdata.end.y = e.clientY}// 计算滑动值this.temporaryData.poswidth = this.basicdata.end.x - this.basicdata.start.xthis.temporaryData.posheight = this.basicdata.end.y - this.basicdata.start.y}},touchend (e) {this.temporaryData.tracking = falsethis.temporaryData.animation = true// 滑动结束,触发判断// 简单判断滑动宽度超出100像素时触发滑出if (Math.abs(this.temporaryData.poswidth) >= 100) {// 最终位移简单设定为x轴200像素的偏移let ratio = Math.abs(this.temporaryData.posheight / this.temporaryData.poswidth)this.temporaryData.poswidth = this.temporaryData.poswidth >= 0 ? this.temporaryData.poswidth   200 : this.temporaryData.poswidth - 200this.temporaryData.posheight = this.temporaryData.posheight >= 0 ? Math.abs(this.temporaryData.poswidth * ratio) : -Math.abs(this.temporaryData.poswidth * ratio)this.temporaryData.opacity = 0this.temporaryData.swipe = true// 记录最终滑动距离this.temporaryData.lastPosWidth = this.temporaryData.poswidththis.temporaryData.lastPosHeight = this.temporaryData.posheight// currentPage 1 引发排序变化this.basicdata.currentPage  = 1// currentPage切换,整体dom进行变化,把第一层滑动置零this.$nextTick(() => {this.temporaryData.poswidth = 0this.temporaryData.posheight = 0this.temporaryData.opacity = 1})// 不满足条件则滑入} else {this.temporaryData.poswidth = 0this.temporaryData.posheight = 0this.temporaryData.swipe = false}},onTransitionEnd (index) {// dom发生变化后,正在执行的动画滑动序列已经变为上一层if (this.temporaryData.swipe && index === this.basicdata.currentPage - 1) {this.temporaryData.animation = truethis.temporaryData.lastPosWidth = 0this.temporaryData.lastPosHeight = 0this.temporaryData.swipe = false}},// 非首页样式切换transform (index) {if (index > this.basicdata.currentPage) {let style = {}let visible = 3let perIndex = index - this.basicdata.currentPage// visible可见数量前滑块的样式if (index <= this.basicdata.currentPage   visible - 1) {style['opacity'] = '1'style['transform'] = 'translate3D(0,0,'   -1 * perIndex * 60   'px'   ')'style['zIndex'] = visible - index   this.basicdata.currentPagestyle['transitionTimingFunction'] = 'ease'style['transitionDuration'] = 300   'ms'} else {style['zIndex'] = '-1'style['transform'] = 'translate3D(0,0,'   -1 * visible * 60   'px'   ')'}return style// 已滑动模块释放后} else if (index === this.basicdata.currentPage - 1) {let style = {}// 继续执行动画style['transform'] = 'translate3D('   this.temporaryData.lastPosWidth   'px'   ','   this.temporaryData.lastPosHeight   'px'   ',0px)'style['opacity'] = '0'style['zIndex'] = '-1'style['transitionTimingFunction'] = 'ease'style['transitionDuration'] = 300   'ms'return style}},// 首页样式切换transformIndex (index) {// 处理3D效果if (index === this.basicdata.currentPage) {let style = {}style['transform'] = 'translate3D('   this.temporaryData.poswidth   'px'   ','   this.temporaryData.posheight   'px'   ',0px)'style['opacity'] = this.temporaryData.opacitystyle['zIndex'] = 10if (this.temporaryData.animation) {style['transitionTimingFunction'] = 'ease'style['transitionDuration'] = 300   'ms'}return style}}}
}
</script>

ok~ 完成了上面的四步,堆叠组件的基本功能就已经实现,快来看看效果吧


堆叠滑动效果已经出来了,但是探探在体验上,还增加了触碰角度偏移,以及判定滑出面积比例

角度偏移的原理,是在用户每次进行touch时,记录用户触碰位置,计算出最大的偏移角度,在滑动出现位移时,线性增加角度以至最大的偏移角度。 使用在stack中具体要做的是:

  • touchmove中计算出所需角度和方向
  • touchend及onTransitionEnd中将角度至零

判定滑出面积比例,主要通过偏移量计算出偏移面积,从而得到面积比例,完成判断

完整的代码和demo可以在github上查看源码,这里就不贴出来了

谢谢大家看完这篇文章,喜欢可以在github上给个⭐️ ,最后祝大家在探探上都能找到前女友?

分享我写的另一个vue-slider组件vue-consise-slider

最近在找新工作,坐标广州,三年前端经验,熟悉vue,有工作介绍的朋友可以邮箱联系我warpcgd@qq.com

❤️❌ 如何用vue制作一个探探滑动组件相关推荐

  1. flask 检测post是否为空_用Flask和Vue制作一个单页应用(五)

    使用POST向后台发送数据 Flask app修改 可以直接使用现有的路由处理函数来接收前端发来的数据,server/app.py中的all_res()修改如下: @app.route('/resou ...

  2. 怎么用python制作简单的程序-神级程序员教你如何用python制作一个牛逼的外挂!...

    玩过电脑游戏的同学对于外挂肯定不陌生,但是你在用外挂的时候有没有想过如何做一个外挂呢?(当然用外挂不是那么道义哈,呵呵),那我们就来看一下如何用python来制作一个外挂.... 我打开了4399小游 ...

  3. 手把手教你如何用Python制作一个电子相册?末附python教程

    这里简单介绍一下python制作电子相册的过程,主要用到tkinter和pillow这2个库,tkinter用于窗口显示照片,pillow用来处理照片,照片切换分为2种方式,一种是自动切换(每隔5秒) ...

  4. python可视化迷宫求解_如何用 Python 制作一个迷宫游戏

    相信大家都玩过迷宫的游戏,对于简单的迷宫,我们可以一眼就看出通路,但是对于复杂的迷宫,可能要仔细寻找好久,甚至耗费数天,然后可能还要分别从入口和出口两头寻找才能找的到通路,甚至也可能找不到通路. 虽然 ...

  5. 如何用JAVA制作一个漂亮的表格

    如何用JAVA制作一个漂亮的表格 表格图片: 选中一行时高亮显示,且字体变为红色: 在表格中添加数据: 代码如下: import java.awt.BorderLayout; import java. ...

  6. 基于 Vue 制作一个猜拳小游戏

    目录 前言: 项目效果展示: 对应素材: 代码实现思路: 实现代码: 总结: 前言: 在工作学习之余玩一会游戏既能带来快乐,还能缓解生活压力,跟随此文一起制作一个小游戏吧. 描述: 石头剪子布,是一种 ...

  7. VUE做一个公共的提示组件,显示两秒自动隐藏,显示的值父组件传递给子组件

    需求:VUE做一个公共的提示组件,显示两秒自动隐藏,显示的值由父组件动态传给子组件. 效果图: 实现步骤: 1.创建一个子组件 Toptips.vue (它就是公共提示组件), optips.vue代 ...

  8. html5倒计时秒杀怎么做,vue 设计一个倒计时秒杀的组件

    HTML CSS HTML5 CSS3 vue 设计一个倒计时秒杀的组件 简介: 倒计时秒杀组件在电商网站中层出不穷  不过思路万变不离其踪,我自己根据其他资料设计了一个vue版的 核心思路:1.时间 ...

  9. 如何用matlab制作一个小软件

    转:https://www.ttin.top/2018/03/29/TT0012/ 制作一个小软件的方法很多,比如说c++的MFC,本站在之前的第一个MFC文章里就提到过:matlab的GUIDE,在 ...

最新文章

  1. 在近期,美众议院为何密集提出了15项新兴技术法案?
  2. Java程序员从笨鸟到菜鸟之(五十二)细谈Hibernate(三)Hibernate常用API详解及源码分析--csdn 曹胜欢...
  3. python数字类型-Python数字类型及其操作
  4. json字符串与对象互相转换
  5. python按键盘上哪个键运行_python通过按下键盘特定按键,开始运行python文件
  6. 客座编辑:吴东亚(1972-),女,中国电子技术标准化研究院信息技术研究中心高级工程师、副主任,国家OID注册中心副主任。...
  7. 极简版ASP.NET Core学习路径及教程
  8. 什么是git subcommand,如何创建git子命令?
  9. Java程序员必备的一些流程图/架构图(拿走不谢)
  10. 地推话术 地推活动策划方案 活动策划方案案例 分享经济活动策划方案
  11. 计算机技术与软件专业技术资格 (水平) 考试 - 报考指南
  12. mysql explain 性能分析_MySQL性能分析(Explain)
  13. 人口流向逆转带来什么变化(zz)
  14. 高科技还是“智商税”?你怎么选
  15. 送人玫瑰,手有余香----七夕情人节的电子玫瑰
  16. canvas小虫子(利用canvas形成多个形状类似虫子的线条)
  17. macos U盘引导安装
  18. 百度鹰眼控制台整合javaweb
  19. 数据万象技术演进之路
  20. 洛谷 P3964 [TJOI2013]松鼠聚会(切比雪夫距离和曼哈顿距离转换)

热门文章

  1. 开源后台管理系统地址
  2. OSChina 周六乱弹 ——还有一口气,我就学好PHP
  3. 高仿IPhone滑动锁屏
  4. 互感器(变压器或电流互感器)电磁感应和减极性分析。之三(最终结论)
  5. 电源电路上两个电感互感了!!!
  6. day20+day21:java常用类
  7. Vue使用图片懒加载
  8. ac在计算机上是什么作用,ac+是什么
  9. 绝地求生服务器维护2月22,绝地求生2月22日更新了什么_绝地求生2.22更新内容介绍_游戏吧...
  10. 赵本山成股神:五年春晚小品准确预测A股走势