-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmobiledevice.js
35 lines (28 loc) · 1017 Bytes
/
mobiledevice.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
function isMobileDevice() {
return /Mobi|Android/i.test(navigator.userAgent);
}
const touchArea = document.getElementById('touchArea');
if (isMobileDevice()) {
// 如果是手机端,添加触摸事件
touchArea.addEventListener('touchstart', function (event) {
console.log('Touch Start:', event.touches[0]);
});
touchArea.addEventListener('touchmove', function (event) {
console.log('Touch Move:', event.touches[0]);
event.preventDefault(); // 防止滚动
});
touchArea.addEventListener('touchend', function (event) {
console.log('Touch End');
});
} else {
// 如果是电脑端,添加鼠标事件
touchArea.addEventListener('mousedown', function (event) {
console.log('Mouse Down:', event);
});
touchArea.addEventListener('mousemove', function (event) {
console.log('Mouse Move:', event);
});
touchArea.addEventListener('mouseup', function (event) {
console.log('Mouse Up');
});
}