-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
91 lines (77 loc) · 2.56 KB
/
script.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
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
var radius = 240;
var autoRotate = true;
var rotateSpeed = -60;
var imgWidth = 120;
var imgHeight = 170;
// Start
setTimeout(init, 1000);
var odrag = document.getElementById("drag-container");
var ospin = document.getElementById("spin-container");
var aImg = document.getElementsByTagName("img");
var aVid = document.getElementsByTagName("video");
var aEle = [...aImg, ...aVid];
ospin.style.width = imgWidth + "px";
ospin.style.height = imgHeight + "px";
var ground = document.getElementById("ground");
ground.style.width = radius * 3 + "px";
ground.style.height = radius * 3 + "px";
function init(delayTime) {
for (var i = 0; i < aEle.length; i++) {
aEle[i].style.transform = "rotateY(" + (i * (360 / aEle.length)) + "deg) translateZ(" + radius + "px)";
aEle[i].style.transition = "transform 1s";
aEle[i].style.transitionDelay = delayTime || (aEle.length - i) / 4 + "s";
}
}
function applyTransform(obj) {
if (tY > 180) tY = 180;
if (tY < 0) tY = 0;
obj.style.transform = "rotateX(" + (-tY) + "deg) rotateY(" + (tX) + "deg)";
}
function playSpin(yes) {
ospin.style.animationPlayState = (yes ? "running" : "paused");
}
var sX, sY, nX, nY, desX = 0, desY = 0, tX = 0, tY = 10;
if (autoRotate) {
var animationName = (rotateSpeed > 0 ? "spin" : "spinRevert");
ospin.style.animation = `${animationName} ${Math.abs(rotateSpeed)}s infinite linear`;
}
document.onpointerdown = function (e) {
clearInterval(odrag.timer);
e = e || window.event;
sX = e.clientX;
sY = e.clientY;
document.onpointermove = function (e) {
e = e || window.event;
nX = e.clientX;
nY = e.clientY;
desX = nX - sX;
desY = nY - sY;
tX += desX * 0.1;
tY += desY * 0.1;
applyTransform(odrag);
sX = nX;
sY = nY;
};
document.onpointerup = function (e) {
odrag.timer = setInterval(function () {
desX *= 0.95;
desY *= 0.95;
tX += desX * 0.1;
tY += desY * 0.1;
applyTransform(odrag);
playSpin(false);
if (Math.abs(desX) < 0.5 && Math.abs(desY) < 0.5) {
clearInterval(odrag.timer);
playSpin(true);
}
}, 17);
document.onpointermove = document.onpointerup = null;
};
return false;
};
document.addEventListener('wheel', function (e) {
e = e || window.event;
var d = e.deltaY / 20 || -e.detail;
radius += d;
init(1);
});