-
Notifications
You must be signed in to change notification settings - Fork 2
/
sticky.js
105 lines (89 loc) · 3.06 KB
/
sticky.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
let sticky = document.querySelector("#sticky");
sticky.addEventListener("click", function(e){
addSticky();
});
function addSticky(imageElement) {
let stickyDiv = document.createElement("div");
stickyDiv.classList.add("sticky");
stickyDiv.innerHTML = `<div class="sticky-header">
<div class="minimize"></div>
<div class="close"></div>
</div>`;
let minimize = stickyDiv.querySelector(".minimize");
let close = stickyDiv.querySelector(".close");
let stickyHeader = stickyDiv.querySelector(".sticky-header");
let stickyContent;
if(imageElement){
let stickyImageDiv = document.createElement("div");
stickyImageDiv.classList.add("sticky-image-div");
stickyDiv.append(stickyImageDiv);
stickyImageDiv.append(imageElement);
stickyContent = stickyImageDiv;
}else{
// <div class="sticky-content" contenteditable="true"></div>
let stickyContentDiv = document.createElement("div");
stickyContentDiv.classList.add("sticky-content");
stickyContentDiv.setAttribute("contenteditable" , "true");
stickyContentDiv.setAttribute("spellcheck" , "false");
stickyDiv.append(stickyContentDiv);
stickyContent = stickyContentDiv;
}
minimize.addEventListener("click", function () {
stickyContent.style.display == "none"
? (stickyContent.style.display = "block")
: (stickyContent.style.display = "none");
});
close.addEventListener("click", function () {
stickyDiv.remove();
});
let stickyHold = false;
let initialX;
let initialY;
stickyHeader.addEventListener("mousedown", function (e) {
stickyHold=true;
initialX = e.clientX;
initialY = e.clientY;
});
stickyHeader.addEventListener("touchstart", function (e) {
stickyHold=true;
initialX = e.touches[0].clientX;
initialY = e.touches[0].clientY;
});
stickyHeader.addEventListener("mousemove", function (e) {
if(stickyHold){
let finalX = e.clientX;
let finalY = e.clientY;
let dx = finalX - initialX;
let dy = finalY - initialY;
let {top , left} = stickyDiv.getBoundingClientRect();
// sticky => top + dy
// sticky => left + dx
stickyDiv.style.top = top + dy + "px";
stickyDiv.style.left = left +dx + "px";
initialX = finalX;
initialY = finalY;
}
});
stickyHeader.addEventListener("touchmove", function (e) {
if(stickyHold){
let finalX = e.touches[0].clientX;
let finalY = e.touches[0].clientY;
let dx = finalX - initialX;
let dy = finalY - initialY;
let {top , left} = stickyDiv.getBoundingClientRect();
// sticky => top + dy
// sticky => left + dx
stickyDiv.style.top = top + dy + "px";
stickyDiv.style.left = left +dx + "px";
initialX = finalX;
initialY = finalY;
}
});
stickyHeader.addEventListener("mouseup", function (e) {
stickyHold = false;
});
stickyHeader.addEventListener("touchend", function (e) {
stickyHold = false;
});
document.body.append(stickyDiv);
}