jQuery "$(window).scrollTop()", Javascript "window.scrollY" 비교

카테고리 IT/코딩 Javascript

무찌마, 댓글

초고 2024. 1. 21.

$(window).scrollTop() and window.scrollY are both used to determine the vertical scroll position of a web page. However, there are some differences between them.

$(window).scrollTop() is a jQuery method that returns the current vertical position of the scroll bar for the first element in the set of matched elements or sets the vertical position of the scroll bar for every matched element. It is equivalent to $(document).scrollTop().

window.scrollY is a JavaScript property that returns the number of pixels that the document is currently scrolled vertically. It is supported by most modern browsers.

In terms of functionality, both methods are similar. However, $(window).scrollTop() is a jQuery method and requires the jQuery library to be loaded, whereas window.scrollY is a native JavaScript property and does not require any external libraries.

Performance-wise, window.scrollY is faster than $(window).scrollTop() because it is a native browser property and does not require the overhead of loading the jQuery library.

In summary, if you are using jQuery in your project, you can use $(window).scrollTop() to get or set the vertical scroll position of a web page. However, if you are not using jQuery, you can use window.scrollY instead, which is faster and does not require any external libraries.

댓글