你们好啊,我是大家的攻城狮,我是 Ken,人贱贱爱的前端攻城狮,我要告诉你个严重的问题,这几天心情低落,代码撸不动啊,结果今天一到公司,打开禅道,你们可能不知道什么是 禅道
就是专门给程序猿提各类 bug 的地方,一早来了三个 bug,顿时一万个草泥马从脑门飘过:前端
最后我仍是怂了,咱们来看看究竟是什么 bug?一看挖槽,原来是兼容性问题,你知道吗?作前端最惧怕的是什么呢?就是尼玛的兼容,尤为是有一个顽固的浏览器,IE,这个千刀万剐的家伙,祸害了多少代的程序猿,哈哈,幸亏的是咱们只须要兼容移动端,pc 端的网站须要兼容 ipad 端。vue
接下来看看是什么问题?原来是在 pc 端右键菜单不能出现了,我一看原来是没有兼容移动端的事件,由于在 pc 端咱们只须要使用 @contextmenu
这个事件就能够实现右键菜单的出现了,node
固然在此以前咱们须要实现一个菜单组件,咱们这个菜单可能须要兼容二级菜单,因此须要实现二级菜单:express
<template>
<!-- 右键菜单 -->
<ul class="menu" v-show="show">
<li
:key="index"
@click="clickOuterEvent(clickMenu.fnName, clickMenus.text)"
class="menu-item icon-right-arrow"
v-for="(clickMenu,index) in clickMenus"
>
<i class="menu-item-bg"></i>
{{clickMenu.text}}
<span class="right-arrow" v-show="clickMenu.secondaryMenu"></span>
<ul class="secondary-menu">
<li
:key="index"
@click.stop="clickEvent(item.fnName, item.text)"
class="menu-item"
v-for="(item,index) in clickMenu.secondaryMenu"
>
<i class="menu-item-bg"></i>
{{item.text}}
</li>
</ul>
</li>
</ul>
</template>
这其中有一个逻辑当右键时,右键菜单才会出现,因此须要一个控制右键菜单 menuShow
,固然咱们也须要知道菜单名称是什么,就须要一个数组来控制 clickMenus
:数组
<script>
export default {
name: 'fyMenu',
props: {
menuShow: {
type: Boolean,
default: false,
},
clickMenus: {
type: Array,
default: () => {
return []
},
},
// 鼠标右键菜单
contextmenu: {
type: Object,
default: function _default() {
return { height: 0, width: -10 }
},
},
},
data() {
return {
show: this.menuShow,
}
},
methods: {
clickOuterEvent(name, text) {
this.$emit('update:menu-show', false)
this.$emit('clickOuterEvent', name, text)
},
clickEvent(name, text) {
this.$emit('update:menu-show', false)
this.$emit('clickEvent', name, text)
},
getShow() {
this.show = false
},
documentListen() {
document.addEventListener('click', this.getShow, false)
}
},
watch: {
menuShow(val) {
this.show = val
},
},
mounted() {
this.documentListen()
},
beforeDestroy() {
document.removeEventListener('click', this.getShow, false)
}
}
</script>
在外面的组件咱们应该这样使用:浏览器
<fy-menu
:clickMenus="clickMenus"
:menu-show="showMenu"
@clickOuterEvent="clickEvent"
ref="menu"
></fy-menu>
export default {
data() {
return {
clickMenus: [
{
fnName: 'modifyGroup',
text: '编辑',
},
{
fnName: 'combineGroup',
text: this.ufd.text['text.plan.combine.group'],
},
{
fnName: 'delete',
text: this.ufd.text['text.del'],
},
],
showMenu: false, // 展现菜单标志
}
},
methods: {
clickEvent(name){}
}
}
接下来咱们须要了解下移动端事件,由于移动端不像 pc 端同样有鼠标事件,移动端只有触摸事件:ide
今天咱们处理 bug 的使用最简单的一种方式,就是使用 @touchstart
,@touchend
:函数
// 实现移动端长按出现右键菜单
start(e, group, nodeIndex, dayIndex, dayPriceSort, groupIndex) {
clearTimeout(this.loop) //再次清空定时器,防止重复注册定时器
let that = this
this.loop = setTimeout(() => {
that.ipadMenucontext(e, group, nodeIndex, dayIndex, dayPriceSort, groupIndex)
}, 1000)
},
end(e) {
e.preventDefault()
clearTimeout(this.loop) //清空定时器,防止重复注册定时器
},
咱们能够看到使用经过延迟函数里执行咱们右键时须要处理的动做,在咱们手指开始触摸时,须要先使用 clearTimeout
先清除上一次的定时器,再去执行咱们想要执行的动做,再触摸结束以后,咱们须要清除定时器oop
因此这样处理完成以后,咱们能够在 ipad 端看到当咱们手指长按 iPad 屏幕的时候,右键菜单就会出现了。网站
以上就是移动端长按实现右键菜单。
接下来就是总结
在vue中长按事件并无封装,在使用的时候须要咱们本身取写一个方法获取长按事件。
方法一:使用@touchstart,@touchend
start () {
clearTimeout(this.loop); //再次清空定时器,防止重复注册定时器
this.loop = setTimeout(() => {
console.log("长按了");
}, 1000);
},
end () {
clearTimeout(this.loop); //清空定时器,防止重复注册定时器
},
备注:使用的时候注意若是是图片,建议把图片设置为背景,直接使用图片,在长按时会触发浏览器对图片的保存、分享等。
方法二:
export default {
install(Vue, options = {
time: 2000,
}) {
Vue.directive('longpress', {
bind: function(el, binding, vNode) {
if (typeof binding.value !== 'function') {
const compName = vNode.context.name
let warn = `[longpress:] provided expression '${binding.expression}' is not afunction, but has to be `
if (compName) { warn += `Found in component '${compName}' ` }
console.warn(warn)
}
// 定义变量
let pressTimer = null
// 定义函数处理程序
// 建立计时器( 1秒后执行函数 )
let start = (e) => {
if (e.type === 'click' && e.button !== 0) {
return
}
if (pressTimer === null) {
pressTimer = setTimeout(() => {
// 执行函数
handler()
}, options.time)
}
}
// 取消计时器
let cancel = (e) => {
// 检查计时器是否有值
if (pressTimer !== null) {
clearTimeout(pressTimer)
pressTimer = null
}
}
// 运行函数
const handler = (e) => {
// 执行传递给指令的方法
binding.value(e)
}
// 添加事件监听器
el.addEventListener('mousedown', start)
el.addEventListener('touchstart', start)
// 取消计时器
el.addEventListener('click', cancel)
el.addEventListener('mouseout', cancel)
el.addEventListener('touchend', cancel)
el.addEventListener('touchcancel', cancel)
},
})
},
}
人生代码 发起了一个读者讨论有时候问题欢迎你们加入讨论啊,给我一些建议啊,否则我都听不到粉丝的声音