Skip to content

Commit 5ed2944

Browse files
committed
fix: should works on mobile
1 parent e4f0040 commit 5ed2944

File tree

4 files changed

+27
-7
lines changed

4 files changed

+27
-7
lines changed

example.html

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
<html lang="en">
33
<head>
44
<meta charset="UTF-8">
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0">
56
<title>Title</title>
67
<style>
78
body {
@@ -28,6 +29,8 @@
2829
</style>
2930
</head>
3031
<body>
32+
<script src="//cdn.jsdelivr.net/npm/eruda"></script>
33+
<script>eruda.init();</script>
3134
<div id="wrapper">
3235
<div id="rect"></div>
3336
</div>

example.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
*
66
*/
77

8-
import popup from './'
8+
import popup from './index'
99

1010
const span = document.createElement('span')
1111
span.textContent = '民主'

index.js

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,17 @@ function popup(popupList = [], node, { styleMapper = sty => sty, index = 0, from
1818
throw new Error('[click-popup] `popupList` should be an array which is not empty')
1919
}
2020

21+
const isSupportTouchStart = 'ontouchstart' in document.documentElement
22+
2123
const clickHandler = evt => {
22-
// const bodyRect = document.body.getBoundingClientRect()
23-
let left = evt.pageX //- bodyRect.left
24-
let top = evt.pageY - 3 //- bodyRect.top
24+
let { pageX, pageY } = evt
25+
if (evt.type === 'touchstart' && evt.touches && evt.touches[0]) {
26+
pageX = evt.touches[0].pageX
27+
pageY = evt.touches[0].pageY
28+
}
29+
30+
let left = pageX //- bodyRect.left
31+
let top = pageY - 3 //- bodyRect.top
2532

2633
const span = document.createElement('span')
2734
const node = popupList[index++ % popupList.length]
@@ -54,11 +61,20 @@ function popup(popupList = [], node, { styleMapper = sty => sty, index = 0, from
5461
}
5562
})
5663
}
57-
node.addEventListener('click', clickHandler, false)
5864

59-
return () => {
60-
node.removeEventListener('click', clickHandler, false)
65+
const eventName = isSupportTouchStart ? 'touchstart' : 'click'
66+
node.addEventListener(eventName, clickHandler, false)
67+
// node.addEventListener('touchstart', clickHandler, false)
68+
69+
const close = () => {
70+
node.removeEventListener(eventName, clickHandler, false)
6171
}
72+
73+
if (module.hot && process.env.NODE_ENV !== 'production') {
74+
module.hot.accept(['./'], close)
75+
}
76+
77+
return close
6278
}
6379

6480
export default popup

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
"precommit": "pretty-quick --staged",
1313
"build": "babel index.js -o index.es5.js",
1414
"build:demo": "gojs -b example.js",
15+
"demo": "gojs -i example.js -p 10000",
1516
"dev": "npm run build -- -w",
1617
"version": "npm run changelog",
1718
"commitmsg": "commitlint -e $GIT_PARAMS",

0 commit comments

Comments
 (0)