Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find the center of the browser window using JQuery

Tags:

jquery

How can I find out the coordinates of the center of the browser using jquery?

like image 547
Shahid Karimi Avatar asked Mar 27 '11 17:03

Shahid Karimi


2 Answers

Why use jQuery? This can be done with simple enough plain JavaScript:

var horizontalCenter = Math.floor(window.innerWidth/2);
var verticalCener = Math.floor(window.innerHeight/2);

To use these, you may also have to append the units, px to the variables.


Edited to use Math.floor() to force an integer value to the returned co-ordinates
like image 186
David Thomas Avatar answered Sep 30 '22 20:09

David Thomas


Untested, but this makes sense:

var intWidth = $(window).innerWidth();
var intHeight = $(window).innerHeight();

var xCenter = intWidth / 2;
var yCenter = intHeight / 2;

alert("The center of the window is: " + xCenter + "x " + yCenter + "y");
like image 24
Jesse Bunch Avatar answered Sep 30 '22 20:09

Jesse Bunch