Merge pull request #474 from stgn/sidebar-swipe

Basic swipe gesture functionality for opening/closing sidebar
This commit is contained in:
Michael Bryan 2018-01-02 22:10:30 +08:00 committed by GitHub
commit b5382f49c6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 44 additions and 11 deletions

View File

@ -74,6 +74,33 @@ $( document ).ready(function() {
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-toggle").click(function(){
@ -344,20 +371,26 @@ function showTooltip(elem, msg) {
function sidebarToggle() {
var html = $("html");
if (html.hasClass("sidebar-hidden")) {
html.removeClass("sidebar-hidden").addClass("sidebar-visible");
store.set('mdbook-sidebar', 'visible');
showSidebar();
} else if (html.hasClass("sidebar-visible")) {
html.removeClass("sidebar-visible").addClass("sidebar-hidden");
store.set('mdbook-sidebar', 'hidden');
hideSidebar();
} else {
if ($("#sidebar").position().left === 0){
html.addClass("sidebar-hidden");
store.set('mdbook-sidebar', 'hidden');
hideSidebar();
} else {
html.addClass("sidebar-visible");
showSidebar();
}
}
}
function showSidebar() {
$('html').removeClass('sidebar-hidden').addClass('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) {