由于JS原生事件中,没有这两种事件,所以自己写了一下。
参数介绍:el 表示要绑定事件的元素,fn 表示要事件触发时执行的函数。
// 移动端,双击事件
function double_click(el, fn) {
let timestamp = 0;
el.addEventListener('click', () => {
const now = +new Date();
if (now - timestamp <= 300) {
fn();
timestamp = 0;
} else {
timestamp = now;
}
});
}
// 移动端,长按事件
function long_press(el, fn) {
let timer;
el.addEventListener('touchstart', () => timer = setTimeout(fn, 500));
el.addEventListener('touchend', () => clearTimeout(timer));
}