behavior 特性为 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 .

句法

behavior: ScrollBehavior
					

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.

范例

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();
  var scrollOptions = {
    left: leftInput.value,
    top: topInput.value,
    behavior: scrollInput.checked ? 'smooth' : 'auto'
  }
  window.scrollTo(scrollOptions);
});
					

规范

规范 状态 注释
CSSOM (CSS 对象模型) 视图模块
The definition of 'behavior' in that specification.
工作草案

浏览器兼容性

The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request. 更新 GitHub 上的兼容性数据
桌面 移动
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet
behavior Chrome 45 Edge 79 Firefox Yes IE 不支持 No Opera 32 Safari Yes 注意事项
Yes 注意事项
Safari does not have support for the smooth scroll behavior.
WebView Android 45 Chrome Android 45 Firefox Android Yes Opera Android 32 Safari iOS Yes 注意事项
Yes 注意事项
Safari does not have support for the smooth scroll behavior.
Samsung Internet Android 5.0

图例

完整支持

完整支持

不支持

不支持

见实现注意事项。

另请参阅

ScrollToOptions

元数据

  • 最后修改:
  1. ScrollToOptions
  2. 特性
    1. behavior
    2. left
    3. top