Skip to content

Instantly share code, notes, and snippets.

@geoffreak
Created January 18, 2018 03:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save geoffreak/207039eda4ae72b1251a4ea9977ba27c to your computer and use it in GitHub Desktop.
Save geoffreak/207039eda4ae72b1251a4ea9977ba27c to your computer and use it in GitHub Desktop.
const hasNextChapter = true;
const hasNextPage = true;
const hasPreviousChapter = true;
const hasPreviousPage = true;
const goToNextChapter = function() {};
const goToNextPage = function() {};
const goToPreviousChapter = function() {};
const goToPreviousPage = function() {};
let down = {};
$(window).on("keyup", function(e) {
const code = e.keyCode ? e.keyCode : e.which; //which code?
return delete down[code];
});
$(window).on("keydown", function(e) {
const code = e.keyCode ? e.keyCode : e.which; //which code?
if (down[code]) {
return;
}
down[code] = true;
if (code === 39 || code === 68 || code === 37 || code === 65 || code === 40 || code === 83 || code === 38 || code === 87) {
e.preventDefault();
}
switch (code) {
case 39:
case 68: // next (right arrow, d)
if (e.shiftKey && hasNextChapter) {
return goToNextChapter();
} else if (!e.shiftKey && hasNextPage) {
return goToNextPage();
}
break;
case 37:
case 65: // next (left arrow, a)
if (e.shiftKey && hasPreviousChapter) {
return goToPreviousChapter();
} else if (!e.shiftKey && hasPreviousPage) {
return goToPreviousPage();
}
break;
case 40:
case 83: // down (down arrow, s)
return $("html, body").animate({
scrollTop: '+=400'
}, 150, 'swing', function() {
return delete down[code];
});
case 38:
case 87: // up (up arrow, w)
return $("html, body").animate({
scrollTop: '-=400'
}, 150, 'swing', function() {
return delete down[code];
});
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment