Created
January 18, 2018 03:33
-
-
Save geoffreak/207039eda4ae72b1251a4ea9977ba27c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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