结婚证书多少钱,不差钱字幕版,ailete417
最近面试发现很多前端程序员都从来没有写过插件的经验,基本上都是网上百度。所以打算写一系列文章,手把手的教一些没有写过组件的兄弟们如何去写插件。本系列文章都基于vue,核心内容都一样,会了之后大家可以快速的改写成react、angular或者是小程序等组件。这篇文章是第一篇,写的是一个类似qq的侧边菜单组件。
效果展示
先让大家看个效果展示,知道咱们要做的东西是个怎么样的样子,图片有点模糊,大家先将就点:
开始制作
dom结构
整体结构中应该存在两个容器:1. 菜单容器 2. 主页面容器;因此当前dom结构如下:
<template> <div class="r-slide-menu"> <div class="r-slide-menu-wrap"></div> <div class="r-slide-menu-content"></div> </div> </template>
为了使得菜单内容和主题内容能够定制,我们再给两个容器中加入两个slot插槽:默认插槽中放置主体内容、菜单放置到menu插槽内:
<template> <div class="r-slide-menu"> <div class="r-slide-menu-wrap"> <slot name="menu"></slot> </div> <div class="r-slide-menu-content"> <slot></slot> </div> </div> </template>
css样式
我项目中使用了scss,代码如下:
<style lang="scss"> @mixin one-screen { position: absolute; left:0; top:0; width:100%; height:100%; overflow: hidden; } .r-slide-menu{ @include one-screen; &-wrap, &-content{ @include one-screen; } &-transition{ -webkit-transition: transform .3s; transition: transform .3s; } } </style>
此时我们就得到了两个绝对定位的容器
javascript
现在开始正式的代码编写了,首先我们理清下交互逻辑:
所以现在咱们需要在使用组件的时候能够入参定制菜单宽度以及触发菜单收起关闭的临界值和菜单宽度的比例,同时需要给主体容器添加touch事件,最后我们给菜单容器和主体容器添加各自添加一个控制他们运动的style,通过控制这个style来控制容器的移动
<template> <div class="r-slide-menu"> <div class="r-slide-menu-wrap" :style="wrapstyle"> <slot name="menu"></slot> </div> <div class="r-slide-menu-content" :style="contentstyle" @touchstart="touchstart" @touchmove="touchmove" @touchend="touchend"> <slot></slot> </div> </div> </template> <script> export default { props: { width: { type: string, default: '250' }, ratio: { type: number, default: 2 } }, data () { return { ismoving: false, transitionclass: '', startpoint: { x: 0, y: 0 }, oldpoint: { x: 0, y: 0 }, move: { x: 0, y: 0 } } }, computed: { wrapstyle () { let style = { width: `${this.width}px`, left: `-${this.width / this.ratio}px`, transform: `translate3d(${this.move.x / this.ratio}px, 0px, 0px)` } return style }, contentstyle () { let style = { transform: `translate3d(${this.move.x}px, 0px, 0px)` } return style } }, methods: { touchstart (e) {}, touchmove (e) {}, touchend (e) {} } }
接下来,我们来实现我们最核心的touch事件处理函数,事件的逻辑如下:
touchstart (e) { this.oldpoint.x = e.touches[0].pagex this.oldpoint.y = e.touches[0].pagey this.startpoint.x = this.move.x this.startpoint.y = this.move.y this.settransition() }, touchmove (e) { let newpoint = { x: e.touches[0].pagex, y: e.touches[0].pagey } let movex = newpoint.x - this.oldpoint.x let movey = newpoint.y - this.oldpoint.y if (math.abs(movex) < math.abs(movey)) return false e.preventdefault() this.ismoving = true movex = this.startpoint.x * 1 + movex * 1 movey = this.startpoint.y * 1 + movey * 1 if (movex >= this.width) { this.move.x = this.width } else if (movex <= 0) { this.move.x = 0 } else { this.move.x = movex } }, touchend (e) { this.settransition(true) this.ismoving = false this.move.x = (this.move.x > this.width / this.ratio) ? this.width : 0 }, settransition (istransition = false) { this.transitionclass = istransition ? 'r-slide-menu-transition' : '' }
上面,这段核心代码中有一个settransition 函数,这个函数的作用是在手指离开的时候给容器元素添加transition属性,让容器有一个过渡动画,完成关闭或者打开动画;所以在手指按下去的瞬间需要把容器上的这个transition属性去除,避免滑动过程中出现容器和手指滑动延迟的不良体验。 最后提醒下,代码中使用translate3d而非translate的原因是为了启动移动端手机的动画3d加速,提升动画流畅度。最终代码如下:
<template> <div class="r-slide-menu"> <div class="r-slide-menu-wrap" :class="transitionclass" :style="wrapstyle"> <slot name="menu"></slot> </div> <div class="r-slide-menu-content" :class="transitionclass" :style="contentstyle" @touchstart="touchstart" @touchmove="touchmove" @touchend="touchend"> <slot></slot> </div> </div> </template> <script> export default { props: { width: { type: string, default: '250' }, ratio: { type: number, default: 2 } }, data () { return { ismoving: false, transitionclass: '', startpoint: { x: 0, y: 0 }, oldpoint: { x: 0, y: 0 }, move: { x: 0, y: 0 } } }, computed: { wrapstyle () { let style = { width: `${this.width}px`, left: `-${this.width / this.ratio}px`, transform: `translate3d(${this.move.x / this.ratio}px, 0px, 0px)` } return style }, contentstyle () { let style = { transform: `translate3d(${this.move.x}px, 0px, 0px)` } return style } }, methods: { touchstart (e) { this.oldpoint.x = e.touches[0].pagex this.oldpoint.y = e.touches[0].pagey this.startpoint.x = this.move.x this.startpoint.y = this.move.y this.settransition() }, touchmove (e) { let newpoint = { x: e.touches[0].pagex, y: e.touches[0].pagey } let movex = newpoint.x - this.oldpoint.x let movey = newpoint.y - this.oldpoint.y if (math.abs(movex) < math.abs(movey)) return false e.preventdefault() this.ismoving = true movex = this.startpoint.x * 1 + movex * 1 movey = this.startpoint.y * 1 + movey * 1 if (movex >= this.width) { this.move.x = this.width } else if (movex <= 0) { this.move.x = 0 } else { this.move.x = movex } }, touchend (e) { this.settransition(true) this.ismoving = false this.move.x = (this.move.x > this.width / this.ratio) ? this.width : 0 }, // 点击切换 switch () { this.settransition(true) this.move.x = (this.move.x === 0) ? this.width : 0 }, settransition (istransition = false) { this.transitionclass = istransition ? 'r-slide-menu-transition' : '' } } } </script> <style lang="scss"> @mixin one-screen { position: absolute; left:0; top:0; width:100%; height:100%; overflow: hidden; } .r-slide-menu{ @include one-screen; &-wrap, &-content{ @include one-screen; } &-transition{ -webkit-transition: transform .3s; transition: transform .3s; } } </style>
总结
以上所述是小编给大家介绍的vue移动端ui框架实现qq侧边菜单组件,希望对大家有所帮助
如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复
详解element上传组件before-remove钩子问题解决
vue.js中使用微信扫一扫解决invalid signature问题(完美解决)
网友评论