-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDraggableSlides.html
More file actions
118 lines (103 loc) · 2.95 KB
/
DraggableSlides.html
File metadata and controls
118 lines (103 loc) · 2.95 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
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Draggable Slides</title>
<style>
body, html {
margin: 0;
padding: 0;
height: 100%;
overflow: hidden;
}
.container {
display: flex;
overflow-x: hidden;
scroll-snap-type: x mandatory;
-webkit-overflow-scrolling: touch;
height: 100%;
}
.slide {
flex: 0 0 auto;
width: 100%;
scroll-snap-align: start;
height: 100%;
background-color: #f0f0f0;
border: 1px solid #ccc;
}
.menu {
display: flex;
background-color: #333;
padding: 10px;
}
.menu-item {
color: white;
margin-right: 10px;
cursor: pointer;
}
.menu-item.active {
color: green;
}
</style>
</head>
<body>
<div class="menu">
<div class="menu-item active" onclick="changeSlide(0)">Slide 1</div>
<div class="menu-item" onclick="changeSlide(1)">Slide 2</div>
<div class="menu-item" onclick="changeSlide(2)">Slide 3</div>
<div class="menu-item" onclick="changeSlide(3)">Slide 4</div>
<div class="menu-item" onclick="changeSlide(4)">Slide 5</div>
</div>
<div class="container">
<div class="slide">Slide 1</div>
<div class="slide">Slide 2</div>
<div class="slide">Slide 3</div>
<div class="slide">Slide 4</div>
<div class="slide">Slide 5</div>
</div>
<script>
let isDragging = false;
let startX;
let scrollLeft;
const container = document.querySelector('.container');
const menuItems = document.querySelectorAll('.menu-item');
container.addEventListener('mousedown', (e) => {
isDragging = true;
startX = e.pageX - container.offsetLeft;
scrollLeft = container.scrollLeft;
updateActiveMenuItem();
});
container.addEventListener('mouseleave', () => {
isDragging = false;
});
container.addEventListener('mouseup', () => {
isDragging = false;
updateActiveMenuItem();
});
container.addEventListener('mousemove', (e) => {
if (!isDragging) return;
e.preventDefault();
const x = e.pageX - container.offsetLeft;
const walk = (x - startX) * 2; // Adjust scrolling speed
container.scrollLeft = scrollLeft - walk;
});
function changeSlide(index) {
container.scrollLeft = index * container.offsetWidth;
updateActiveMenuItem();
}
function updateActiveMenuItem() {
const scrollPosition = container.scrollLeft;
const slideWidth = container.offsetWidth;
const activeIndex = Math.round(scrollPosition / slideWidth);
menuItems.forEach((item, index) => {
if (index === activeIndex) {
item.classList.add('active');
} else {
item.classList.remove('active');
}
});
}
</script>
</body>
</html>