element.scrollIntoView

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since September 2020.

La méthode Element.scrollIntoView() fait défiler la page de manière à rendre l'élément visible.

Syntaxe

js
element.scrollIntoView();
element.scrollIntoView(alignToTop); // Paramètre booléen
element.scrollIntoView(scrollIntoViewOptions); // Paramètre d'objet

Paramètres

alignToTop Facultatif

est une valeur Boolean optionnelle qui :

  • si elle vaut true (vrai), aligne l'élément avec le haut de la zone visible de l'ancêtre défilable. Correspond à scrollIntoViewOptions: {block: "start", inline: "nearest"}. C'est la valeur par défaut.
  • Si elle vaut false (faux), celui-ci sera aligné en bas de la zone visible de l'ancêtre défilable. Correspond à scrollIntoViewOptions: {block: "end", inline: "nearest"}.
scrollIntoViewOptions Facultatif Expérimental

Est un objet qui a les propriétés suivantes :

behavior Facultatif

Définit l'animation de transition qui peut être "auto", "instant" ou "smooth". Par défaut : "auto".

block Facultatif

L'une des options "start", "center", "end" ou"nearest". Par défaut : "start".

inline Facultatif

L'une des options "start", "center", "end" ou "nearest". Par défaut : "nearest".

Exemple

js
var element = document.getElementById("box");

element.scrollIntoView();
element.scrollIntoView(false);
element.scrollIntoView({ block: "end" });
element.scrollIntoView({ behavior: "smooth", block: "end", inline: "nearest" });

Notes

L'élément peut ne pas être aligné complètement avec le haut ou le bas, selon la disposition des autres éléments.

Spécifications

Specification
CSSOM View Module
# dom-element-scrollintoview

Compatibilité des navigateurs

Report problems with this compatibility data on GitHub
desktopmobile
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
scrollIntoView
options parameter

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support
Partial support
Partial support
See implementation notes.
Has more compatibility info.

Voir aussi