I volontari di MDN non hanno ancora tradotto questo articolo in Italiano. Unisciti a noi e traducilo tu stesso.
Puoi anche consultare l’articolo in English (US).

The read-only scrollY property of the Window interface returns the number of pixels that the document is currently scrolled vertically. This value is subpixel precise in modern browsers, meaning that it isn't necessarily a whole number. You can get the number of pixels the document is scrolled horizontally from the scrollX property.

Syntax

var y = window.scrollY

Value

In practice, the returned value is a double-precision floating-point value indicating the number of pixels the document is currently scrolled vertically from the origin, where a positive value means the content is scrolled to upward. If the document is rendered on a subpixel-precise device, then the returned value is also subpixel-precise and may contain a decimal component. If the document isn't scrolled at all up or down, then scrollY is 0.

If you need an integer value, you can use Math.round() to round it off.

In more technical terms, scrollY returns the Y coordinate of the top edge of the current viewport. If there is no viewport, the returned value is 0.

Example

// make sure and go down to the second page 
if (window.scrollY) {
  window.scroll(0, 0);  // reset the scroll position to the top left of the document.
}

window.scrollByPages(1);

Notes

Use this property to check that the document hasn't already been scrolled when using relative scroll functions such as scrollBy(), scrollByLines(), or scrollByPages().

The pageYOffset property is an alias for the scrollY property:

window.pageYOffset == window.scrollY; // always true

For cross-browser compatibility, use window.pageYOffset instead of window.scrollY. Additionally, older versions of Internet Explorer (< 9) do not support either property and must be worked around by checking other non-standard properties. A fully compatible example:

var supportPageOffset = window.pageXOffset !== undefined;
var isCSS1Compat = ((document.compatMode || "") === "CSS1Compat");

var x = supportPageOffset ? window.pageXOffset : isCSS1Compat ? document.documentElement.scrollLeft : document.body.scrollLeft;
var y = supportPageOffset ? window.pageYOffset : isCSS1Compat ? document.documentElement.scrollTop : document.body.scrollTop;

Specification

Specification Status Comment
CSS Object Model (CSSOM) View Module
The definition of 'window.scrollY' in that specification.
Working Draft  

Browser compatibility

Update compatibility data on GitHub
DesktopMobile
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidEdge MobileFirefox for AndroidOpera for AndroidSafari on iOSSamsung Internet
Basic supportChrome Full support Yes
Full support Yes
Full support Yes
Alternate Name
Alternate Name Uses the non-standard name: pageYOffset
Edge Full support Yes
Full support Yes
Full support Yes
Alternate Name
Alternate Name Uses the non-standard name: pageYOffset
Firefox Full support Yes
Full support Yes
Full support Yes
Alternate Name
Alternate Name Uses the non-standard name: pageYOffset
IE No support No
No support No
Full support 9
Alternate Name
Alternate Name Uses the non-standard name: pageYOffset
Opera Full support Yes
Full support Yes
Full support Yes
Alternate Name
Alternate Name Uses the non-standard name: pageYOffset
Safari Full support Yes
Full support Yes
Full support Yes
Alternate Name
Alternate Name Uses the non-standard name: pageYOffset
WebView Android Full support Yes
Full support Yes
Full support Yes
Alternate Name
Alternate Name Uses the non-standard name: pageYOffset
Chrome Android Full support Yes
Full support Yes
Full support Yes
Alternate Name
Alternate Name Uses the non-standard name: pageYOffset
Edge Mobile Full support Yes
Full support Yes
Full support Yes
Alternate Name
Alternate Name Uses the non-standard name: pageYOffset
Firefox Android Full support Yes
Full support Yes
Full support Yes
Alternate Name
Alternate Name Uses the non-standard name: pageYOffset
Opera Android ?
?
?
Alternate Name
Alternate Name Uses the non-standard name: pageYOffset
Safari iOS Full support Yes
Full support Yes
?
Alternate Name
Alternate Name Uses the non-standard name: pageYOffset
Samsung Internet Android ?
?
?
Alternate Name
Alternate Name Uses the non-standard name: pageYOffset
Subpixel precisionChrome Full support YesEdge Full support YesFirefox Full support 55IE No support NoOpera Full support YesSafari Full support YesWebView Android Full support YesChrome Android Full support YesEdge Mobile Full support YesFirefox Android Full support 55Opera Android ? Safari iOS ? Samsung Internet Android ?

Legend

Full support  
Full support
No support  
No support
Compatibility unknown  
Compatibility unknown
Uses a non-standard name.
Uses a non-standard name.

See also

Tag del documento e collaboratori

Hanno collaborato alla realizzazione di questa pagina: connorshea, amityb, Sheppy, sarafec, opr, alexandrudima, jpmedley, cvrebert, fscholz, u_7cc, teoli, iamanupmenon, ethertank, Shelby, Waldo, Mgjbot, Dria, JesseW
Ultima modifica di: connorshea,