Javascript 如何设置自定义窗口高度?
处理对象在屏幕上随机移动的项目。我试图在顶部有一个元素,它不会被这些对象覆盖。这是我当前的代码:Javascript 如何设置自定义窗口高度?,javascript,jquery,html,Javascript,Jquery,Html,处理对象在屏幕上随机移动的项目。我试图在顶部有一个元素,它不会被这些对象覆盖。这是我当前的代码: function makeNewPosition(){ // Get viewport dimensions var h = $(window).height() - 150; var w = $(window).width() - 100; var nh = Math.floor(Math.random() * h); var nw = Math.fl
function makeNewPosition(){
// Get viewport dimensions
var h = $(window).height() - 150;
var w = $(window).width() - 100;
var nh = Math.floor(Math.random() * h);
var nw = Math.floor(Math.random() * w);
return [nh,nw];
}
这工作得很好,但我需要封锁,比如说在屏幕顶部150px的物体无法到达的地方。由于某种原因,我一直无法做到这一点。我将如何修改代码来实现这一点
谢谢你的帮助 您需要将
150px
设置为从顶部的偏移量。否则,对象将不会触及屏幕的底部
150px,而不是顶部
function makeNewPosition() {
var h = $(window).height() - 150;
var w = $(window).width() - 100;
var nh = Math.floor(Math.random() * h) + 150;
var nw = Math.floor(Math.random() * w);
return [nh,nw];
}
如果您想让100px
的左右区域也保持不变,则需要减去200px
并将100px
添加到结果中
function makeNewPosition() {
var h = $(window).height() - 150;
var w = $(window).width() - 200;
var nh = Math.floor(Math.random() * h) + 150;
var nw = Math.floor(Math.random() * w) + 100;
return [nh,nw];
}
您需要将
150px
设置为从顶部的偏移量。否则,对象将不会触及屏幕的底部
150px,而不是顶部
function makeNewPosition() {
var h = $(window).height() - 150;
var w = $(window).width() - 100;
var nh = Math.floor(Math.random() * h) + 150;
var nw = Math.floor(Math.random() * w);
return [nh,nw];
}
如果您想让100px
的左右区域也保持不变,则需要减去200px
并将100px
添加到结果中
function makeNewPosition() {
var h = $(window).height() - 150;
var w = $(window).width() - 200;
var nh = Math.floor(Math.random() * h) + 150;
var nw = Math.floor(Math.random() * w) + 100;
return [nh,nw];
}
您需要将150添加到您的随机高度,这样无论生成的随机数是多少,您的
nh
将至少从150开始
var nh = Math.floor(Math.random() * h) + 150;
您需要将150添加到您的随机高度,这样无论生成的随机数是多少,您的
nh
将至少从150开始
var nh = Math.floor(Math.random() * h) + 150;
只需将这些150px作为基准值添加到随机新高度…?只需将这些150px作为基准值添加到随机新高度…?感谢@eisbehr的帮助!谢谢@eisbehr的帮助!