forked from think2011/collision-checker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
95 lines (82 loc) · 2.54 KB
/
index.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
<!DOCTYPE html>
<html lang="zh-cn">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width,minimum-scale=1.0,maximum-scale=1.0"/>
<title>collision-checker demo</title>
<style>
html, body {
margin: 0;
padding: 0;
overflow: hidden;
}
#box1 {
width: 100px;
height: 100px;
line-height: 50px;
text-align: center;
background: slateblue;
position: absolute;
top: 30%;
left: 30%;
transform: translate3d(0, 0, 0);
}
#box1.active {
background: salmon;
}
#box2 {
width: 100px;
height: 100px;
line-height: 100px;
text-align: center;
background: lightseagreen;
position: absolute;
top: 45%;
left: 50%;
cursor: pointer;
transition: opacity .3s ease !important;
transform: translate3d(0, 0, 0);
}
</style>
</head>
<body>
<div id="box1"></div>
<div id="box2">Drag me</div>
<script src="Dragify.js"></script>
<script src="collisionChecker.js"></script>
<script>
var EVENTS = collisionChecker.isTouch()
? ["touchstart", "touchmove", "touchend"]
: ["mousedown", "mousemove", "mouseup"]
var $ = document.querySelector.bind(document)
var $box1 = $('#box1')
var $box2 = $('#box2')
document.addEventListener(EVENTS[1], function (e) {
e.preventDefault()
})
new Dragify($box2)
.on('move', function () {
// var hitInfo = collisionChecker($box1) //如果不传,则用event代替
var hitInfo = collisionChecker($box1, $box2)
if (hitInfo.hit) {
var infoBox = []
console.log(hitInfo)
$box2.style.opacity = .7
$box1.classList.add('active')
Object.keys(hitInfo).forEach(function (v) {
var key = v
var value = hitInfo[key]
if (key !== 'hit' && value) {
infoBox.push(key)
}
})
$box1.innerHTML = infoBox.join('<br>')
} else {
$box2.style.opacity = 1
$box1.classList.remove('active')
$box1.innerHTML = ''
}
})
</script>
</body>
</html>