Skip to content

scrollToElement

A função scrollToElement rola suavemente a página até um elemento alvo identificado por seletor ou referência.

Sintaxe

typescript
scrollToElement(target: string | HTMLElement, options?: ScrollIntoViewOptions): void;

Parâmetros

NomeTipoDescrição
targetstring | HTMLElementSeletor CSS ou elemento alvo
optionsScrollIntoViewOptions (opcional)Opções de rolagem

Retorno

TipoDescrição
voidNão retorna valor

Exemplos

typescript
scrollToElement("#topo");
// Rola até o elemento com id "topo"

const el = document.getElementById("rodape");
scrollToElement(el, { behavior: "smooth" });
// Rola suavemente até o rodapé

Notas

  • Útil para navegação em páginas longas ou âncoras.

Referências

Released under the MIT License.