Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get the current browser window center

How do I get the browser window height so I can find the center position?

I only need the window height, not the web page height.

I tried $(window).height() / 2 but it only works if the browser has focus from the top of the page. If I scroll down I get the wrong center.

like image 831
BiberFiver Avatar asked Dec 21 '22 10:12

BiberFiver


1 Answers

To get the y value of the center of the current viewable area, use:

$(window).scrollTop() + $(window).height() / 2

I tried it on this page, by opening up the Web Inspector and entering:

$('<p>').text('test').appendTo('body').css({position: 'absolute', top: $(window).scrollTop() + $(window).height() / 2});
like image 110
Benjamin Atkin Avatar answered Dec 24 '22 02:12

Benjamin Atkin