当前位置: 首页 > news >正文

4.3 Sensors -- onLongPress

4.3 Sensors – onLongPress

https://vueuse.org/core/onLongPress/

作用

监控元素的长按事件。

方法提供了一些修饰符,可以在options中指定:

stop、once、prevent、capture、self

官方示例

<script setup lang="ts">
import { ref } from 'vue'
import { onLongPress } from '@vueuse/core'const htmlRefHook = ref<HTMLElement>()
const longPressedHook = ref(false)function onLongPressCallbackHook(e: PointerEvent) {longPressedHook.value = true
}
function resetHook() {longPressedHook.value = false
}// 这个函数是主要的
onLongPress(htmlRefHook,onLongPressCallbackHook,{modifiers: {prevent: true}}
)
</script><template><p>Long Pressed: {{ longPressedHook }}</p><button ref="htmlRefHook" class="ml-2 button small">Press long</button><button class="ml-2 button small" @click="resetHook">Reset</button>
</template>
  • 无渲染组件代码如下:通过@trigger触发回调
<script setup lang="ts">
import { ref } from 'vue'
import { OnLongPress } from '@vueuse/components'const longPressedComponent = ref(false)function onLongPressCallbackComponent(e: PointerEvent) {longPressedComponent.value = true
}
function resetComponent() {longPressedComponent.value = false
}
</script><template><p>Long Pressed: {{ longPressedComponent }}</p><OnLongPressas="button"class="ml-2 button small"@trigger="onLongPressCallbackComponent">Press long</OnLongPress><button class="ml-2 button small" @click="resetComponent">Reset</button>
</template>
  • 指令用法:v-on-long-press可以接收一个函数或者一个数组
<script setup lang="ts">
import { ref } from 'vue'
import { vOnLongPress } from '@vueuse/components'const longPressedDirective = ref(false)function onLongPressCallbackDirective(e: PointerEvent) {longPressedDirective.value = true
}
function resetDirective() {longPressedDirective.value = false
}
</script><template><p>Long Pressed: {{ longPressedDirective }}</p><buttonv-on-long-press.prevent="onLongPressCallbackDirective"class="ml-2 button small">Press long</button><buttonv-on-long-press="[onLongPressCallbackDirective, { delay: 1000, modifiers: { stop: true } }]"class="ml-2 button small">Press long (with options)</button><button class="ml-2 button small" @click="resetDirective">Reset</button>
</template>

源码分析

主要思路:

  1. 对目标元素进行监听,监听pointerdown、pointerup、pointerleave三个事件,按下时设置定时器
  2. 当按下和抬起(或者离开)的间隔,大于给定的时间(比如1.5s),则触发回调函数;
  3. 如果在1.5s以内抬起,则清除定时器。
  4. 剩下的都是一些参数,见代码注释。

⚠️:h5可能有兼容性问题,如果这几个事件不管用,改成touch即可。

export function onLongPress(target: MaybeElementRef,handler: (evt: PointerEvent) => void,options?: OnLongPressOptions,
) {const elementRef = computed(() => unrefElement(target))let timeout: ReturnType<typeof setTimeout> | undefinedfunction clear() {if (timeout) {clearTimeout(timeout)timeout = undefined}}function onDown(ev: PointerEvent) {if (options?.modifiers?.self && ev.target !== elementRef.value)returnclear()// 阻止默认事件if (options?.modifiers?.prevent)ev.preventDefault()// 停止事件冒泡if (options?.modifiers?.stop)ev.stopPropagation()timeout = setTimeout(() => handler(ev),  // 超过delay时间后,触发回调options?.delay ?? DEFAULT_DELAY,)}const listenerOptions: AddEventListenerOptions = {// 捕获阶段执行capture: options?.modifiers?.capture,// 只监听一次once: options?.modifiers?.once,}useEventListener(elementRef, 'pointerdown', onDown, listenerOptions)useEventListener(elementRef, 'pointerup', clear, listenerOptions)useEventListener(elementRef, 'pointerleave', clear, listenerOptions)
}

http://www.mrgr.cn/news/19264.html

相关文章:

  • C++ | Leetcode C++题解之第394题字符串解码
  • 数据结构之链表
  • Python Tkinter小程序
  • 分类预测|基于蜣螂优化极限梯度提升决策树的数据分类预测Matlab程序DBO-Xgboost 多特征输入单输出 含基础模型
  • 浏览器自动化测试的利器:Cypress
  • SPI总线协议详解
  • Golang | Leetcode Golang题解之第394题字符串解码
  • 高效异步编程:使用Python的asyncio库实现异步I/O操作
  • 我找到了一个让ChatGPT稳定通过草莓测试的方法,百试百灵!
  • 【conda】Conda 环境迁移指南:如何更改 envs_dirs 和 pkgs_dirs 以及跨盘迁移
  • 深度学习应用 - 语音识别篇
  • YoloV10改进策略:卷积篇|基于PConv的二次创新|附结构图|性能和精度得到大幅度提高(独家原创)
  • Java | Leetcode Java题解之第393题UTF-8编码验证
  • 9 自研rgbd相机基于rk3566之qt框架开发rgbd融合线程
  • pytorch pyro更高阶的优化器会使用更高阶的导数,比如二阶导数(Hessian矩阵)
  • 【嵌入式撸码】内存相关的大小尽量偶数对齐
  • J.U.C Review - 阻塞队列原理/源码分析
  • https和harbor仓库跟k8s
  • Steam游戏截图方法
  • 如何判断字符串是否对称?