Skip to content

Instantly share code, notes, and snippets.

@bramses
Created January 5, 2023 04:10
Show Gist options
  • Save bramses/9576769f1329c751180c80bf43466038 to your computer and use it in GitHub Desktop.
Save bramses/9576769f1329c751180c80bf43466038 to your computer and use it in GitHub Desktop.
swipe left to open menu; swipe right to close it
// swipe left on mobile to open menu
let touchstartX = 0;
let touchendX = 0;
function checkDirection() {
// check if greater than 100px
// if swipe right close menu
if (touchendX < touchstartX && touchstartX - touchendX > 100) {
document
.querySelector(".published-container")
.classList.remove("is-left-column-open");
}
if (touchendX > touchstartX && touchendX - touchstartX > 100) {
document
.querySelector(".published-container")
.classList.add("is-left-column-open");
}
}
document.addEventListener("touchstart", (e) => {
touchstartX = e.changedTouches[0].screenX;
});
document.addEventListener("touchend", (e) => {
touchendX = e.changedTouches[0].screenX;
checkDirection();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment