在keydown,使用jQuery按键后停止滚动
在keydown,按下按键后我有一些滚动动作。 所以这就是它的样子:
$(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工作得很好但是当我滚动时,用我的div按键也会整页滚动。 有没有选项可以阻止这个身体滚动?
你需要.preventDefault()
……
$(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); } });
如果你的身体是目前滚动的动画,那么使用stop()
。 http://api.jquery.com/stop/
$('body').stop();