<!DOCTYPE html>
<html lang="en">
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Title</title>
<style>
/*防止長按選中文字*/
.testDiv{
-webkit-touch-callout:none;
-webkit-user-select:none;
-khtml-user-select:none;
-moz-user-select:none;
-ms-user-select:none;
user-select:none;
}
</style>
</head>
<body>
<div>
<button class="longTap" onClick="console.log('tap')" >長按</button>
<div class="testDiv" style="height:60px;background:darkolivegreen" onclick="console.log(111)">hahahhahhahahahahhahhahahha</div>
</div>
<script src="https://cdn.jsdelivr.net/npm/jquery@3.4.1/dist/jquery.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/vconsole@3.3.4/dist/vconsole.min.js"></script>
<script>
var vConsole = new VConsole();
class LongPress{
constructor(type,el, callback) {
this.callback = callback
this.type = type
this.el = document.querySelector(el);
this.timer = null;
this.startTimeStamp = null
this.endTimeStamp = null
this.init();
}
init() {
this.el.addEventListener('touchstart',(e)=>{
this.touchstart(e)
})
this.el.addEventListener('touchend',(e)=>{
this.touchend(e)
})
}
touchstart(e) {
console.log('touchstart',e)
// 清除默認(rèn)行為
//e.preventDefault();
// 開啟定時器
this.timer = setTimeout(() => {
if (typeof this.callback === 'function') {
this.callback();
} else {
console.error('callback is not a function!');
}
}, 700);
//記錄start時間戳
this.startTimeStamp = e.timeStamp
}
touchend(e) {
console.log('touchend',e)
this.endTimeStamp = e.timeStamp
//console.log('end',this,this.timer,this.endTimeStamp-this.startTimeStamp)
// 清除默認(rèn)行為
//e.preventDefault();
// 清除定時器
clearTimeout(this.timer);
}
}
new LongPress('longtap','.longTap',function () {
console.log('long pressing....')
})
new LongPress('longtap','.testDiv',function () {
console.log(1234556)
})
/*class Person{//定義了一個名字為Person的類
constructor(name,age){//constructor是一個構(gòu)造方法,用來接收參數(shù)
this.name = name;//this代表的是實例對象
this.age=age;
}
say(){//這是一個類的方法乾蓬,注意千萬不要加上function
return "我的名字叫" + this.name+"今年"+this.age+"歲了";
}
}
var obj=new Person("laotie",88);
console.log(obj.say());//我的名字叫l(wèi)aotie今年88歲了*/
</script>
</body>
</html>
js 長按
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
- 文/潘曉璐 我一進店門节腐,熙熙樓的掌柜王于貴愁眉苦臉地迎上來外盯,“玉大人,你說我怎么就攤上這事翼雀”ス叮” “怎么了?”我有些...
- 文/不壞的土叔 我叫張陵狼渊,是天一觀的道長箱熬。 經(jīng)常有香客問我,道長狈邑,這世上最難降的妖魔是什么城须? 我笑而不...
- 正文 為了忘掉前任,我火速辦了婚禮官地,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘烙懦。我一直安慰自己驱入,他們只是感情好,可當(dāng)我...
- 文/花漫 我一把揭開白布氯析。 她就那樣靜靜地躺著亏较,像睡著了一般。 火紅的嫁衣襯著肌膚如雪掩缓。 梳的紋絲不亂的頭發(fā)上雪情,一...
- 文/蒼蘭香墨 我猛地睜開眼表悬,長吁一口氣:“原來是場噩夢啊……” “哼弥锄!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起蟆沫,我...
- 正文 年R本政府宣布本今,位于F島的核電站,受9級特大地震影響主巍,放射性物質(zhì)發(fā)生泄漏冠息。R本人自食惡果不足惜,卻給世界環(huán)境...
- 文/蒙蒙 一孕索、第九天 我趴在偏房一處隱蔽的房頂上張望逛艰。 院中可真熱鬧,春花似錦搞旭、人聲如沸散怖。這莊子的主人今日做“春日...
- 文/蒼蘭香墨 我抬頭看了看天上的太陽镇眷。三九已至,卻和暖如春翎嫡,著一層夾襖步出監(jiān)牢的瞬間欠动,已是汗流浹背。 一陣腳步聲響...
推薦閱讀更多精彩內(nèi)容
- 初始化頁面監(jiān)聽整個頁面手勢事件啼肩,在生命周期的初始化使用(可以不寫load事件)window.addEventLis...
- 在移動端,可以根據(jù)touchStart與touchEnd的間隔來判斷是點擊芬沉,雙擊躺同,還是長按阁猜; 主要的代碼: let...
- 問題描述:用js的鍵盤事件控制一個div移動,當(dāng)按下一個方向鍵不放蹋艺,div會先停頓一下剃袍,然后才開始持續(xù)移動。(原因...
- 在移動H5開發(fā)中捎谨,長按保存分享圖片到手機相冊算是必需功能民效。實現(xiàn)原理: 下載html2canvas庫的JS文件導(dǎo)入到...