Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
228 views
in Technique[技术] by (71.8m points)

javascript - How can I set window.innerHeight to fullsize?

// collect all the divs
var divs = document.getElementsByClassName('star');
// get window width and height
var winWidth = window.innerWidth;
var winHeight = window.innerHeight;

// i stands for "index". you could also call this banana or haircut. it's a variable
for ( var i=0; i < divs.length; i++ ) {
    
    // shortcut! the current div in the list
    var thisDiv = divs[i];
    
    // get random numbers for each element
    randomTop = getRandomNumber(0, winHeight);
    randomLeft = getRandomNumber(0, winWidth);
    
    // update top and left position
    thisDiv.style.top = randomTop +"px";
    thisDiv.style.left = randomLeft +"px";
    
}

// function that returns a random number between a min and max
function getRandomNumber(min, max) {
    
  return Math.random() * (max - min) + min;
    
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can get full screen with and height by this:

let screenWidth = screen.availWidth;
let screenheight = screen.availHeight;

And set your div width and height like this:

thisDiv.style.width = "200px";

Edit:

Sorry, maybe I didn't catch what do you want to do. Do you want to show divs randomly across the browser window? Then using window.innerWidth as you did, would be fine in first place, and you shouldn't change anything, but obviously something doesn't work fine. But refer to question, do you want to resize the browser window? For this there is a method that could resize the window, but it works only if windows was opened by ``window.open(...```. As I know, you can't resize the main browser or one Tab from javascript.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...