问题
I have some scrolling actions after keydown, keypress pressing. So this is what it looks like:
$(document).keydown(function(e) {
e.stopPropagation();
if (e.keyCode === 40) {
// some scrolling actions in specifie div
} else if (e.keyCode === 38) {
// some scrolling actions in specifie div
}
});
Everithing is working fine but when im scrolling, keypressing with my div scrolls also whole page. Is there any option to stop this body scrolling?
回答1:
You need .preventDefault()
in there...
$(document).keydown(function(e) {
e.stopPropagation();
if (e.keyCode === 40) {
e.preventDefault();
console.log(40);
} else if (e.keyCode === 38) {
e.preventDefault();
console.log(38);
}
});
回答2:
If your body is what currently has the scrolling animation then use stop()
. http://api.jquery.com/stop/
$('body').stop();
来源:https://stackoverflow.com/questions/14320634/body-stop-scrolling-after-keydown-keypress-with-jquery