183

I need to grab the height of the window and the scrolling offset in jQuery, but I haven't had any luck finding this in the jQuery docs or Google.

I'm 90% certain there's a way to access height and scrollTop for an element (presumably including the window), but I just can't find the specific reference.

T J
43.2k13 gold badges87 silver badges142 bronze badges
asked Nov 19, 2008 at 23:17
2
  • 2
    How about without jQuery? Commented Jan 15, 2015 at 16:59
  • @Costa That has likely been asked elsewhere on StackOverflow, and if not it deserves to be its own question. Commented Feb 3, 2015 at 22:42

5 Answers 5

301

From jQuery Docs:

const height = $(window).height();
const scrollTop = $(window).scrollTop();

http://api.jquery.com/scrollTop/
http://api.jquery.com/height/

Michał Perłakowski
93.3k30 gold badges165 silver badges189 bronze badges
answered Nov 19, 2008 at 23:24
Sign up to request clarification or add additional context in comments.

4 Comments

Figures I just missed it in the docs; searched them, but their organization frankly makes no sense to me (still thinking in Mootools, I suppose). Thanks!
This doesn't work in any browser for jQuery 1.3.2 even though it was supposedly added in an earlier version. Either that or I have something wrong with my code here
$(window).height() gives the height viewport and not the scroll height. $(document).height() gives the real scroll height, as Aidamina suggested.
I'm often amazed at how many upvotes simple things like this attract, but since I've looked this up twice this week already, have another +1
41

from http://api.jquery.com/height/ (Note: The difference between the use for the window and the document object)

$(window).height(); // returns height of browser viewport
$(document).height(); // returns height of HTML document

from http://api.jquery.com/scrollTop/

$(window).scrollTop() // return the number of pixels scrolled vertically
answered Sep 9, 2010 at 13:27

Comments

12

Pure JS

window.innerHeight
window.scrollY

is more than 10x faster than jquery (and code has similar size):

enter image description here

Here you can perform test on your machine: https://jsperf.com/window-height-width

answered Jul 2, 2018 at 9:54

1 Comment

window.scrollY, get scroll top height
6
$(window).height()
$(window).width()

There is also a plugin to jquery to determine element location and offsets

http://plugins.jquery.com/project/dimensions

scrolling offset = offsetHeight property of an element

Omar
31.7k9 gold badges73 silver badges117 bronze badges
answered Nov 19, 2008 at 23:21

1 Comment

Thanks, Joseph. It's not exactly what I was looking for as I was trying to avoid using another plugin, but you got me searching in the right direction. In the end, it turns out what I really needed was '$(window).scrollTop()' to figure out how much of the page has scrolled past the viewport to adjust elements accordingly.
1

If you need to scroll to a point of an element. You can use Jquery function to scroll it up/down.

$('html, body').animate({
 scrollTop: $("#div1").offset().top
 }, 'slow');
answered May 15, 2020 at 9:03

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.