Skip to content

Commit 91e202d

Browse files
committed
added swipe for sidebar
1 parent 45db4d3 commit 91e202d

File tree

3 files changed

+139
-17
lines changed

3 files changed

+139
-17
lines changed

static/css/main.css

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -132,13 +132,13 @@ html {
132132

133133
div[class*="sidebar"] {
134134
position: fixed;
135-
top: 5%;
135+
top: 0;
136136
left: 0;
137137
width: 270px;
138-
height: 95%;
138+
height: 100%;
139139
overflow-y: auto;
140140
background-color: rgba(21, 21, 30, 1);
141-
z-index: 99999999;
141+
transition: left 0.25s ease;
142142
}
143143

144144
a[class*="navbar-item"], a#open-btn.navbar-item, form[class*="navbar-item"], input[class*="navbar-item"]{
@@ -154,14 +154,14 @@ a.navbar-item:hover, a#open-btn.navbar-item:hover, form[class*="navbar-item"]:ho
154154
left: 0px;
155155
top: 0px;
156156
background-color: rgba(21, 21, 30, 1);
157-
z-index: 99999999;
158157
}
159158
div[class*="navbar-start"]{
160159
width: 230px;
161160
position: fixed;
162161
left: 39px;
163162
top: 0px;
164163
background-color: rgba(21, 21, 30, 1);
164+
transition: left 0.25s ease;
165165
}
166166

167167
#sidebar-ul {
@@ -208,16 +208,14 @@ div[class*="navbar-start"]{
208208
}
209209

210210

211-
@media screen and (max-width: 600px) {
211+
@media screen and (max-width: 1200px) {
212212
div[class*="sidebar"] {
213213
left: -300px;
214214
/* position: absolute; */
215-
z-index: 99999999;
216215
}
217216
div[class*="navbar-start topic"]{
218217
left: -300px;
219218
/* position: absolute; */
220-
z-index: 99999999;
221219
}
222220

223221
#open-btn {

templates/topics.html

Lines changed: 67 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,19 +40,81 @@
4040
if (back_button.length > 0) {
4141
back_button[0].parentNode.setAttribute('name', 'back');
4242
}
43+
44+
enableSwipeNav();
4345
});
4446
</script>
4547
<script>
4648
function openNav() {
4749
const sidebarParent = document.querySelector('.sidebar');
4850
const navbarParent = document.querySelector('.navbar-start.topic');
49-
const isOpen = sidebarParent.style.left === '0px';
51+
if (!sidebarParent || !navbarParent) {
52+
return;
53+
}
54+
55+
const sidebarLeft = getComputedStyle(sidebarParent).left;
56+
const isOpen = sidebarLeft === '0px';
5057

5158
// Toggle the sidebar state
5259
sidebarParent.style.left = isOpen ? '-300px' : '0px';
5360
sidebarParent.style.overflowY = isOpen ? 'hidden' : 'auto';
5461
navbarParent.style.left = isOpen ? '-300px' : '39px';
5562
}
63+
64+
// Swipe support for mobile: swipe right to open, left to close
65+
function enableSwipeNav() {
66+
const sidebarParent = document.querySelector('.sidebar');
67+
const navbarParent = document.querySelector('.navbar-start.topic');
68+
if (!sidebarParent || !navbarParent) {
69+
return;
70+
}
71+
72+
let startX = 0;
73+
let endX = 0;
74+
const threshold = 50; // minimal swipe distance in px
75+
76+
const closeNav = () => {
77+
sidebarParent.style.left = '-300px';
78+
sidebarParent.style.overflowY = 'hidden';
79+
navbarParent.style.left = '-300px';
80+
};
81+
82+
const openNavSwipe = () => {
83+
sidebarParent.style.left = '0px';
84+
sidebarParent.style.overflowY = 'auto';
85+
navbarParent.style.left = '39px';
86+
};
87+
88+
document.addEventListener('touchstart', function (e) {
89+
if (window.innerWidth > 1200) {
90+
return;
91+
}
92+
startX = e.changedTouches[0].screenX;
93+
endX = startX;
94+
}, {passive: true});
95+
96+
document.addEventListener('touchmove', function (e) {
97+
if (window.innerWidth > 1200) {
98+
return;
99+
}
100+
endX = e.changedTouches[0].screenX;
101+
}, {passive: true});
102+
103+
document.addEventListener('touchend', function () {
104+
if (window.innerWidth > 1200) {
105+
return;
106+
}
107+
const deltaX = endX - startX;
108+
const sidebarLeft = getComputedStyle(sidebarParent).left;
109+
const isOpen = sidebarLeft === '0px';
110+
111+
if (deltaX > threshold && !isOpen) {
112+
openNavSwipe();
113+
} else if (deltaX < -threshold && isOpen) {
114+
closeNav();
115+
}
116+
}, {passive: true});
117+
}
56118
</script>
57119
</head>
58120
<body style="zoom: 100%">
@@ -61,8 +123,8 @@
61123
[([(= include webpage =)])]
62124
</div>
63125
<div class='sidebar'>
64-
<div class="container">
65-
<div id="navbarMenuHeroA">
126+
<ul id="sidebar-ul">
127+
<li id="sidebar-li"><div id="navbarMenuHeroA">
66128
<!-- <h2 class="has-text-white navbar-item is-centered navbar-end">Educative Viewer</h2> -->
67129
<a id="open-btn" class="navbar-item" onclick="openNav()" type="button"></a>
68130
<div class="navbar-start topic">
@@ -81,8 +143,8 @@
81143
[([(= endif =)])]
82144
</div>
83145
</div>
84-
</div>
85-
<ul id="sidebar-ul">
146+
</li>
147+
<li id="sidebar-li">&nbsp;</li>
86148
[([(= for folder in folder_list =)])]
87149
<li id="sidebar-li">
88150
[([(= if [([(loop.index0)])] == [([(itr)])] =)])]

templates/topics_toc.html

Lines changed: 67 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,19 +40,81 @@
4040
if (back_button.length > 0) {
4141
back_button[0].parentNode.setAttribute('name', 'back');
4242
}
43+
44+
enableSwipeNav();
4345
});
4446
</script>
4547
<script>
4648
function openNav() {
4749
const sidebarParent = document.querySelector('.sidebar');
4850
const navbarParent = document.querySelector('.navbar-start.topic');
49-
const isOpen = sidebarParent.style.left === '0px';
51+
if (!sidebarParent || !navbarParent) {
52+
return;
53+
}
54+
55+
const sidebarLeft = getComputedStyle(sidebarParent).left;
56+
const isOpen = sidebarLeft === '0px';
5057

5158
// Toggle the sidebar state
5259
sidebarParent.style.left = isOpen ? '-300px' : '0px';
5360
sidebarParent.style.overflowY = isOpen ? 'hidden' : 'auto';
5461
navbarParent.style.left = isOpen ? '-300px' : '39px';
5562
}
63+
64+
// Swipe support for mobile: swipe right to open, left to close
65+
function enableSwipeNav() {
66+
const sidebarParent = document.querySelector('.sidebar');
67+
const navbarParent = document.querySelector('.navbar-start.topic');
68+
if (!sidebarParent || !navbarParent) {
69+
return;
70+
}
71+
72+
let startX = 0;
73+
let endX = 0;
74+
const threshold = 50; // minimal swipe distance in px
75+
76+
const closeNav = () => {
77+
sidebarParent.style.left = '-300px';
78+
sidebarParent.style.overflowY = 'hidden';
79+
navbarParent.style.left = '-300px';
80+
};
81+
82+
const openNavSwipe = () => {
83+
sidebarParent.style.left = '0px';
84+
sidebarParent.style.overflowY = 'auto';
85+
navbarParent.style.left = '39px';
86+
};
87+
88+
document.addEventListener('touchstart', function (e) {
89+
if (window.innerWidth > 1200) {
90+
return;
91+
}
92+
startX = e.changedTouches[0].screenX;
93+
endX = startX;
94+
}, {passive: true});
95+
96+
document.addEventListener('touchmove', function (e) {
97+
if (window.innerWidth > 1200) {
98+
return;
99+
}
100+
endX = e.changedTouches[0].screenX;
101+
}, {passive: true});
102+
103+
document.addEventListener('touchend', function () {
104+
if (window.innerWidth > 1200) {
105+
return;
106+
}
107+
const deltaX = endX - startX;
108+
const sidebarLeft = getComputedStyle(sidebarParent).left;
109+
const isOpen = sidebarLeft === '0px';
110+
111+
if (deltaX > threshold && !isOpen) {
112+
openNavSwipe();
113+
} else if (deltaX < -threshold && isOpen) {
114+
closeNav();
115+
}
116+
}, {passive: true});
117+
}
56118
</script>
57119
</head>
58120
<body style="zoom: 100%">
@@ -62,8 +124,8 @@
62124
[([(= include webpage =)])]
63125
</div>
64126
<div class='sidebar'>
65-
<div class="container">
66-
<div id="navbarMenuHeroA">
127+
<ul id="sidebar-ul">
128+
<li id="sidebar-li"><div id="navbarMenuHeroA">
67129
<!-- <h2 class="has-text-white navbar-item is-centered navbar-end">Educative Viewer</h2> -->
68130
<a id="open-btn" class="navbar-item" onclick="openNav()" type="button"></a>
69131
<div class="navbar-start topic">
@@ -82,8 +144,8 @@
82144
[([(= endif =)])]
83145
</div>
84146
</div>
85-
</div>
86-
<ul id="sidebar-ul">
147+
</li>
148+
<li id="sidebar-li">&nbsp;</li>
87149
[([(= for toc_item in toc_items =)])]
88150
<li id="sidebar-li">
89151
[([(= if toc_item.is_category =)])]

0 commit comments

Comments
 (0)