Window.scrollY

Sumário

Retorna o número de pixels que o documento já rolou verticalmente.

Sintaxe

var y = window.scrollY;
  • y é o número atual de pixels que o documento rolou a partir do topo.

Exemplo

// verificar e ir para a segunda página
if (window.scrollY) {
  window.scroll(0, 0);  // reinicia a posição do scroll para a posição superior esquerda do documento.
}

window.scrollByPages(1);

Notas

Utilize esta propriedade para verificar se o documento já foi rolado quando funções de rolagem relativa, tais como window.scrollBy, window.scrollByLines, ou window.scrollByPages, forem utilizadas.

A propriedade pageYOffset é um alias para scrollY:

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:

Para compatibilidade cross-browser, utilize window.pageYOffset em vez de window.scrollY. Além disso, versões inferiores do Internet Explorer 9 não suportam ambas as propriedades, e deve ser contornado verificando outras propriedades não padronizadas.

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;

Especificação

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

Browser compatibility

Update compatibility data on GitHub
DesktopMobile
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidFirefox for AndroidOpera for AndroidSafari on iOSSamsung Internet
scrollYChrome Full support 1
Full support 1
Full support 1
Alternate Name
Alternate Name Uses the non-standard name: pageYOffset
Edge Full support ≤18
Full support ≤18
Full support 12
Alternate Name
Alternate Name Uses the non-standard name: pageYOffset
Firefox Full support 1
Full support 1
Full support 1
Alternate Name
Alternate Name Uses the non-standard name: pageYOffset
IE Full support 9
Alternate Name
Full support 9
Alternate Name
Alternate Name Uses the non-standard name: pageYOffset
Opera Full support 9.6
Full support 9.6
Full support 4
Alternate Name
Alternate Name Uses the non-standard name: pageYOffset
Safari Full support 1
Full support 1
Full support 1
Alternate Name
Alternate Name Uses the non-standard name: pageYOffset
WebView Android Full support 1
Full support 1
Full support 1
Alternate Name
Alternate Name Uses the non-standard name: pageYOffset
Chrome Android Full support 18
Full support 18
Full support 18
Alternate Name
Alternate Name Uses the non-standard name: pageYOffset
Firefox Android Full support 4
Full support 4
Full support 4
Alternate Name
Alternate Name Uses the non-standard name: pageYOffset
Opera Android Full support 10.1
Full support 10.1
Full support 10.1
Alternate Name
Alternate Name Uses the non-standard name: pageYOffset
Safari iOS Full support 1
Full support 1
Full support 1
Alternate Name
Alternate Name Uses the non-standard name: pageYOffset
Samsung Internet Android Full support 1.0
Full support 1.0
Full support 1.0
Alternate Name
Alternate Name Uses the non-standard name: pageYOffset
Subpixel precisionChrome Full support YesEdge Full support ≤18Firefox Full support 55IE No support NoOpera Full support YesSafari Full support YesWebView Android Full support YesChrome Android Full support YesFirefox Android Full support 55Opera Android ? Safari iOS ? Samsung Internet Android Full support Yes

Legend

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

Veja também