简体   繁体   English

html5游戏正在玩时如何停止滚动?

[英]How to stop scrolling while html5 game is playing?

http://nygaming20.wixsite.com/retrobolt/game-1 http://nygaming20.wixsite.com/retrobolt/game-1

The link above is a very simple html5 game,you'll notice that when you go up or down the page scrolls.This makes playing the game difficult and annoying so I need to find out how to fix it. 上面的链接是一个非常简单的html5游戏,您会注意到当您向上或向下滚动页面时,这会使游戏变得既困难又烦人,因此我需要找出解决方法。 The game is embed with iframe,here is what I have in my html code- 游戏嵌入了iframe,这是我在HTML代码中所拥有的内容-

<iframe src="http://noborder.netlify.com/" width="675" height="540" frameborder="0" marginheight="0" marginwidth="0">Loading...</iframe>

Here's a dropbox like to the game's html5 code in full. 这是一个完整的类似于游戏html5代码的保管箱。

https://www.dropbox.com/sh/brsxk97efuohs86/AAAUeIuJy_FoTKQDnGUpgL_Wa?dl=0 https://www.dropbox.com/sh/brsxk97efuohs86/AAAUeIuJy_FoTKQDnGUpgL_Wa?dl=0

You need to prevent key presses from triggering the default browser controls, so in your case using 您需要防止按键触发默认的浏览器控件,因此在您的情况下,请使用

window.addEventListener("keydown", function(e) {
    // space and arrow keys
    if([32, 37, 38, 39, 40].indexOf(e.keyCode) > -1) {
        e.preventDefault();
    }
}, false);

should be sufficient to prevent scrolling 应该足以防止滚动

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM