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.
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});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With