Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/image/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 防止图像飞出窗口_Javascript_Image - Fatal编程技术网

Javascript 防止图像飞出窗口

Javascript 防止图像飞出窗口,javascript,image,Javascript,Image,我的代码中有一个图像,每5秒移动750px,但它会飞出页面,所以你再也看不到它了。如何防止它飞出页面 this is the code: 到目前为止,我试图设置页面的最大宽度/高度,但这不起作用,我想你可以通过跟踪图像或类似的方法来解决,但我不知道如何解决。如果小于750,请使用window.innerHeight和window.innerWidth function moveduck() { var top = Math.floor(Math.random() * Math.min(75

我的代码中有一个图像,每5秒移动750px,但它会飞出页面,所以你再也看不到它了。如何防止它飞出页面

this is the code:

到目前为止,我试图设置页面的最大宽度/高度,但这不起作用,我想你可以通过跟踪图像或类似的方法来解决,但我不知道如何解决。如果小于750,请使用window.innerHeight和window.innerWidth

function moveduck() {
  var top = Math.floor(Math.random() * Math.min(750, window.innerHeight));
  var left = Math.floor(Math.random() * Math.min(750, window.innerWidth));
  duck.style.top = top + 'px';
  duck.style.left = left + 'px';
  score++
};
另外,我认为你不需要除左上角以外的东西

谢谢你的提示(另外,我认为你不需要除左上角以外的东西)。我确实添加了代码,现在它不再从左向右飞了。但在页面底部,它仍然会飞出屏幕