forked from mistic100/Photo-Sphere-Viewer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtestMove2.html
158 lines (154 loc) · 4.66 KB
/
testMove2.html
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
<script src="https://unpkg.com/vconsole@latest/dist/vconsole.min.js"></script>
<title></title>
<style>
:root{
--foo: red;
--border: 1px solid green;
}
*{
margin: 0;
padding: 0;
}
body{
box-sizing: border-box;
}
.contain{
margin: 0 auto;
display: flex;
align-items: center;
justify-content: center;
width: 300px;
margin: auto;
height: 100vh;
}
.parent{
box-sizing: border-box;
width: 300px;
height: 279.157px;
border: var(--border);
transform-origin: 0 0;
position: relative;
margin: 0 auto;
display: flex;
align-items: center;
justify-content: center
}
.imgStyle{
width: 100%;
height: 100%;
}
</style>
</head>
<body style="position: relative;">
<div class="contain">
<div class="parent">
<img src="https://jx-uav.oss-cn-shenzhen.aliyuncs.com/c017e642209fa751a7782b05839308c4.PNG" class="imgStyle" />
</div>
</div>
<script type="text/javascript">
function getElementPosition(element) {
var rect = element.getBoundingClientRect();
var scrollTop = window.pageYOffset || document.documentElement.scrollTop;
var scrollLeft = window.pageXOffset || document.documentElement.scrollLeft;
return {
x: rect.left + scrollLeft,
y: rect.top + scrollTop
};
}
// https://juejin.cn/post/7046034846388813861
// var vConsole = new window.VConsole();
const elementPosition = getElementPosition(parentNode);
///////////////////////////////////////
let orgData = null, nowScale = 1, x, y, twoSig, leftNum = elementPosition.x, topNum = elementPosition.y,leftNumfalse, topNumfalse, nowScalefalse;
let SingleFinger = null, singLeft, singTop, oneSig;
const parentNode = document.querySelector('.parent');
console.log(',parentNodeparentNode',parentNode);
function getOrigin(e){
if(e.touches.length == 2){
// 双指
const org = {
x: (e.touches[0].pageX - 2 * leftNum + e.touches[1].pageX) / 2,
y: (e.touches[0].pageY - 2 * topNum + e.touches[1].pageY) / 2,
distance: Math.sqrt(Math.pow(e.touches[0].pageX - e.touches[1].pageX, 2) + Math.pow(e.touches[0].pageY - e.touches[1].pageY, 2))
}
return org;
}else if(e.touches.length == 1){
//单指
return {
x: e.touches[0].pageX,
y: e.touches[0].pageY
}
}
}
parentNode.addEventListener('touchstart', (e) => {
if(e.touches.length == 2){
let flag = true;
for(let i = 0; i < e.touches.length; i++){
if(e.touches[i].target.nodeName != 'IMG'){
flag = false;
}
}
if(flag) {
//获取双指刚放到屏幕的中心点
twoSig = true;
orgData = getOrigin(e);
x = getOrigin(e).x;
y = getOrigin(e).y;
}
}else if(e.touches.length == 1){
oneSig = true;
SingleFinger = getOrigin(e);
}
})
parentNode.addEventListener('touchmove', (e) => {
e.preventDefault();
if(e.touches.length == 2){
if(twoSig){
let flag = true;
for(let i = 0; i < e.touches.length; i++){
if(e.touches[i].target.nodeName != 'IMG'){
flag = false;
}
}
if(flag) {
// 获取新的两指中心点
const nowOrg = getOrigin(e);
const scale = nowOrg.distance / orgData.distance;
if(scale * nowScale < 1) return false;
// 获取放大后的中心点位置1
const obj = {
x: orgData.x * scale,
y: orgData.y * scale
}
parentNode.style.transform = `matrix(${scale * nowScale}, 0, 0, ${scale * nowScale}, ${nowOrg.x + leftNum - obj.x}, ${nowOrg.y + topNum - obj.y})`;
leftNumfalse = nowOrg.x + leftNum - obj.x;
topNumfalse = nowOrg.y + topNum - obj.y;
nowScalefalse = scale * nowScale;
}
}
}else if(e.touches.length == 1){
if(oneSig){
const nowObj = {
x: e.touches[0].pageX,
y: e.touches[0].pageY
}
parentNode.style.transform = `matrix(${nowScalefalse}, 0, 0, ${nowScalefalse}, ${nowObj.x - SingleFinger.x + leftNum}, ${nowObj.y - SingleFinger.y + topNum})`;
leftNumfalse = nowObj.x - SingleFinger.x + leftNum;
topNumfalse = nowObj.y - SingleFinger.y + topNum;
}
}
})
parentNode.addEventListener('touchend', (e) => {
parentNode.style.transform = `matrix(${nowScalefalse}, 0, 0, ${nowScalefalse}, ${leftNumfalse}, ${topNumfalse})`;
leftNum = leftNumfalse;
topNum = topNumfalse;
nowScale = nowScalefalse;
})
</script>
</body>
</html>