MDN wants to talk to developers like you: https://qsurvey.mozilla.com/s3/8d22564490d8

scroll

Esta tradução está incompleta. Por favor, ajude a traduzir este artigo.

O evento de scroll é disparado quando o a view do documento ou um elemento for rolado.

Informações gerais

Especificação
DOM L3, CSSOM View
Interface
UIEvent
Bubbles
Not on elements, but bubbles to the default view when fired on the document
Cancelable
No
Target
defaultView, Document, Element
Default Action
None

Properties

Property Type Description
target Somente leitura EventTarget The event target (the topmost target in the DOM tree).
type Somente leitura DOMString The type of event.
bubbles Somente leitura Boolean Whether the event normally bubbles or not
cancelable Somente leitura Boolean Whether the event is cancellable or not?
view Somente leitura WindowProxy document.defaultView (window of the document)
detail Somente leitura long (float) 0.

Exemplo

Since scroll events can fire at a high rate, the event handler shouldn't execute computationally expensive operations such as DOM modifications. Instead, it is recommended to throttle the event using requestAnimationFrame, setTimeout or customEvent, as follows:

Scroll optimization with window.requestAnimationFrame

// Reference: http://www.html5rocks.com/en/tutorials/speed/animations/

var last_known_scroll_position = 0;
var ticking = false;

function doSomething(scroll_pos) {
  // do something with the scroll position
}

window.addEventListener('scroll', function(e) {
  last_known_scroll_position = window.scrollY;
  if (!ticking) {
    window.requestAnimationFrame(function() {
      doSomething(last_known_scroll_position);
      ticking = false;
    });
  }
  ticking = true;
});

 

More similar examples on resize event.

Browser compatibility

iOS UIWebView

In iOS UIWebViews, scroll events are not fired while scrolling is taking place; they are only fired after the scrolling has completed. See Bootstrap issue #16202. Safari and WKWebViews are not affected by this bug.

Etiquetas do documento e colaboradores

 Colaboradores desta página: lai32290
 Última atualização por: lai32290,