有没有办法忽略浏览器,使iframe处于焦点状态时避免这种情况?
谢谢
function preventUseOfDefaultKeys(event) { // You can change the function name
if(event.keyCode == 38) { // 38 would be the up arrow
event.preventDefualt();
}
}
<body>
,而是写<body onkeydown="preventUseOfDefaultKeys(event);" >
。这应该有效,尽管我在我的系统上遇到了某些键的问题。我遇到了同样的问题。这是我找到的解决方案。
<body>
标签开头创建一个文本框:<input type="text" id="shazam" style="position: absolute; left: 50%; z-index: -1; border-color: transparent; outline: none; color: transparent; background-color: transparent;" />
'position'属性设置为absolute意味着它将忽略其他元素,'left'属性将使其(几乎)位于窗口的中心,并且'z-index'属性将使其首先渲染,让其他元素显示在其上方(在这种情况下是由libgdx生成的画布)。将outline和颜色都设置为透明只是为了确保这个输入框是不可见的。
<head>
标签中放入以下脚本:<script type="text/javascript">
window.onkeydown = function(e) {
if(e.keyCode == 32) {
document.getElementById("shazam").focus();
}
};
window.onscroll = function(e){
document.getElementById("shazam").style.top=window.pageYOffset+"px";
};
</script>
这将禁用空格键向下滚动,但画布外的任何内容也将无法接收到空格键。我只在Google Chrome上进行了测试。
我知道,这是一个丑陋的解决方法,但...好吧,它有效。
----编辑
现在,在使用鼠标滚动窗口后按下空格键时,窗口不会突然向上滚动。