Skip to content

Commit 1f6c997

Browse files
tadyjpkazupon
authored andcommitted
toggle sidebar by swipe (#767) (#221)
1 parent 8a490ff commit 1f6c997

File tree

1 file changed

+22
-1
lines changed

1 file changed

+22
-1
lines changed

themes/vue/source/js/common.js

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@
9292
}
9393

9494
/**
95-
* Mobile burger menu button for toggling sidebar
95+
* Mobile burger menu button and gesture for toggling sidebar
9696
*/
9797

9898
function initMobileMenu () {
@@ -109,6 +109,27 @@
109109
sidebar.classList.remove('open')
110110
}
111111
})
112+
113+
// Toggle sidebar on swipe
114+
var start = {}, end = {}
115+
116+
document.body.addEventListener('touchstart', function (e) {
117+
start.x = e.changedTouches[0].clientX
118+
start.y = e.changedTouches[0].clientY
119+
})
120+
121+
document.body.addEventListener('touchend', function (e) {
122+
end.y = e.changedTouches[0].clientY
123+
end.x = e.changedTouches[0].clientX
124+
125+
var xDiff = end.x - start.x
126+
var yDiff = end.y - start.y
127+
128+
if (Math.abs(xDiff) > Math.abs(yDiff)) {
129+
if (xDiff > 0) sidebar.classList.add('open')
130+
else sidebar.classList.remove('open')
131+
}
132+
})
112133
}
113134

114135
/**

0 commit comments

Comments
 (0)