滚动页面到某个元素

MSDN上搬过来的,记录一下。

Syntax

object.scrollIntoView( [bAlignToTop])

Parameters

bAlignToTop Optional. Boolean that specifies one of the following values:
true
Default. Scrolls the object so that top of the object is visible at the top of the window.
false
Scrolls the object so that the bottom of the object is visible at the bottom of the window.

Return Value

No return value.

Remarks

The scrollIntoView method is useful for immediately showing the user the result of some action without requiring the user to manually scroll through the document to find the result.
Depending on the size of the given object and the current window, this method might not be able to put the item at the very top or very bottom, but will position the object as close to the requested position as possible.

Example

This example uses the scrollIntoView method to underline the content of the document’s fifth paragraph and scroll it into view at the top of the window.

var coll = document.all.tags("P");
if (coll.length >= 5) {
  coll(4).style.textDecoration = "underline";
  coll(4).scrollIntoView(true);
}

发表评论