Basic swipe gesture functionality for opening/closing sidebar

This commit is contained in:
Shane Nelson 2017-10-23 20:31:56 -04:00
parent c64384abc3
commit 6e4d2485c3
1 changed files with 44 additions and 11 deletions

View File

@ -82,6 +82,33 @@ $( document ).ready(function() {
sidebar.scrollTop(activeSection.offset().top); sidebar.scrollTop(activeSection.offset().top);
} }
var firstContact = null;
$(this).on('touchstart', function(e) {
firstContact = {
x: e.originalEvent.touches[0].clientX,
time: Date.now()
};
});
$(this).on('touchmove', function(e) {
if (!firstContact)
return;
var curX = e.originalEvent.touches[0].clientX;
var xDiff = curX - firstContact.x,
tDiff = Date.now() - firstContact.time;
if (tDiff < 250 && Math.abs(xDiff) >= 150) {
if (xDiff >= 0 && firstContact.x < Math.min(document.body.clientWidth * 0.25, 300))
showSidebar();
else if (xDiff < 0 && curX < 300)
hideSidebar();
firstContact = null;
}
});
// Theme button // Theme button
$("#theme-toggle").click(function(){ $("#theme-toggle").click(function(){
@ -335,20 +362,26 @@ function showTooltip(elem, msg) {
function sidebarToggle() { function sidebarToggle() {
var html = $("html"); var html = $("html");
if (html.hasClass("sidebar-hidden")) { if (html.hasClass("sidebar-hidden")) {
html.removeClass("sidebar-hidden").addClass("sidebar-visible"); showSidebar();
store.set('mdbook-sidebar', 'visible');
} else if (html.hasClass("sidebar-visible")) { } else if (html.hasClass("sidebar-visible")) {
html.removeClass("sidebar-visible").addClass("sidebar-hidden"); hideSidebar();
store.set('mdbook-sidebar', 'hidden');
} else { } else {
if ($("#sidebar").position().left === 0){ if ($("#sidebar").position().left === 0){
html.addClass("sidebar-hidden"); hideSidebar();
store.set('mdbook-sidebar', 'hidden');
} else { } else {
html.addClass("sidebar-visible"); showSidebar();
}
}
}
function showSidebar() {
$('html').removeClass('sidebar-hidden').addClass('sidebar-visible');
store.set('mdbook-sidebar', 'visible'); store.set('mdbook-sidebar', 'visible');
} }
}
function hideSidebar() {
$('html').removeClass('sidebar-visible').addClass('sidebar-hidden');
store.set('mdbook-sidebar', 'hidden');
} }
function run_rust_code(code_block) { function run_rust_code(code_block) {