W3cubDocs

/DOM

ScrollToOptions.behavior

The behavior property of the ScrollToOptions dictionary specifies whether the scrolling should animate smoothly, or happen instantly in a single jump.

This is actually defined on the ScrollOptions dictionary, which is implemented by ScrollToOptions.

Syntax

behavior: ScrollBehavior

Value

An enum, the value of which can be one of the following:

  • smooth: The scrolling animates smoothly.
  • auto: The scrolling happens in a single jump.

Examples

In our scrolltooptions example (see it live) we include a form that allows the user to enter three values — two numbers representing the left and top properties (i.e. the positions to scroll to along the X and Y axes), and a checkbox indicating whether they want smooth scrolling enabled or not.

When the form is submitted, an event handler is run that puts the entered values into a ScrollToOptions dictionary, and then invokes the Window.ScrollTo() method, passing the dictionary as a parameter:

form.addEventListener('submit', (e) => {
  e.preventDefault();
  scrollOptions = {
    left: leftInput.value,
    top: topInput.value,
    behavior: scrollInput.checked ? 'smooth' : 'auto'
  }

  window.scrollTo(scrollOptions);
});

Specifications

Browser compatibility

No compatibility data found. Please contribute data for "api.ScrollToOptions.behavior" (depth: 1) to the MDN compatibility data repository.

See also

ScrollToOptions

© 2005–2018 Mozilla Developer Network and individual contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/ScrollToOptions/behavior