计时 javascript 和 src 更改?
好的,所以我有这个 javascript,当我按下箭头键时,可以使用箭头键发送这些 javascript。我有一个协作脚本,可以使正确的('img')命令移动图像,但我试图在图像随着 src 更改和延迟而移动时更改图像。帮助?
<script type="text/javascript">
document.onkeydown = KeyCheck;
function KeyCheck(event) {
var spacebar=32
var KeyID = event.keyCode;
switch(KeyID) {
case 39:
right('img');
document.getElementById('img').src = 'guyr.png';
setTimeout("right('img');
document.getElementById('img').src = 'runr.png';
setTimeout("right('img');
document.getElementById('img').src = 'guyr.png';",100);",100);
break;
}
}
</script>
ok so i have this javascript to use the arrow keys to send these javascripts when i press arrowkeys. i have a collaborating script that makes the right('img') command move the image but im trying to change the image as it moves with src changes and delays. Help?
<script type="text/javascript">
document.onkeydown = KeyCheck;
function KeyCheck(event) {
var spacebar=32
var KeyID = event.keyCode;
switch(KeyID) {
case 39:
right('img');
document.getElementById('img').src = 'guyr.png';
setTimeout("right('img');
document.getElementById('img').src = 'runr.png';
setTimeout("right('img');
document.getElementById('img').src = 'guyr.png';",100);",100);
break;
}
}
</script>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
更改图像的 src 属性将触发异步 GET 请求,并且在浏览器缓存图像之前一开始可能会很慢。看起来你正在实现某种游戏?我不会更改单个图像的
src
,而是最初隐藏所有图像,并切换要显示/隐藏的图像的visibility
CSS 属性。这样,您就可以预取加载期间需要的所有图像,并简单地显示/隐藏元素作为onkeydown
事件的处理程序。Changing the
src
attribute of an image is going to trigger an asynchronous GET request, and may be slow at first before the browser has cached the images. It looks like you are implementing a game of some sort? Instead of changing thesrc
of a single image, I would keep all of the images hidden initially, and toggle thevisibility
CSS attribute of images you want to show/hide. That way, you can pre-fetch all of the images that you'll need during load time, and simply show/hide elements as the handlers for youronkeydown
events.如果您想做某种游戏或动画,请尝试阅读一些相关内容,了解如何构建游戏循环、动画等。
但是,如果您只是做某种测试,请尝试这样的事情,这不是一个很好的选择。漂亮的代码,但动画的想法应该可行:
一个函数调用另一个函数,通过参数接收它,以便继续动画。
If you want to do some sort of a game or animation, try to read a little about it, how to structure your game loop, animations and etc.
But if you are doing just some sort of test try something like this, is not a beautiful code but the idea of an animation should work:
One function calls another, receiving it by parameter, in order to to continue the animation.