-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
54 lines (48 loc) · 1.67 KB
/
script.js
File metadata and controls
54 lines (48 loc) · 1.67 KB
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
document.addEventListener('DOMContentLoaded', () => {
const notebookPage = document.querySelector('.notebook-page');
let isDragging = false;
let startX, startY, initialX, initialY;
function startDragging(e) {
isDragging = true;
if (e.type === 'touchstart') {
startX = e.touches[0].clientX - initialX;
startY = e.touches[0].clientY - initialY;
} else {
startX = e.clientX - initialX;
startY = e.clientY - initialY;
}
}
function drag(e) {
if (!isDragging) return;
e.preventDefault();
let clientX, clientY;
if (e.type === 'touchmove') {
clientX = e.touches[0].clientX;
clientY = e.touches[0].clientY;
} else {
clientX = e.clientX;
clientY = e.clientY;
}
const newX = clientX - startX;
const newY = clientY - startY;
notebookPage.style.left = `${newX}px`;
notebookPage.style.top = `${newY}px`;
}
function stopDragging() {
isDragging = false;
}
// Mouse events
notebookPage.addEventListener('mousedown', startDragging);
document.addEventListener('mousemove', drag);
document.addEventListener('mouseup', stopDragging);
// Touch events
notebookPage.addEventListener('touchstart', startDragging);
document.addEventListener('touchmove', drag);
document.addEventListener('touchend', stopDragging);
// Set initial position
const rect = notebookPage.getBoundingClientRect();
initialX = rect.left;
initialY = rect.top;
notebookPage.style.left = `${initialX}px`;
notebookPage.style.top = `${initialY}px`;
});