scrollIntoView Method

本文介绍了如何使用scrollIntoView方法使指定元素滚动至浏览器窗口顶部或底部,并附带示例代码。此方法有助于改善用户体验,确保用户无需手动滚动即可看到操作结果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Causes the  object   to scroll into view, aligning it either at the top or bottom of the window.

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);
}

Standards Information

There is no public standard that applies to this method.

Applies To

 A ,  ADDRESS ,  APPLET ,  AREA ,  B ,  BIG ,  BLOCKQUOTE ,  BR ,  BUTTON ,  CAPTION ,  CENTER ,  CITE ,  CODE ,  COL ,  COLGROUP ,  COMMENT ,  controlRange ,  CUSTOM ,  DD ,  DFN ,  DIR ,  DIV ,  DL ,DT ,  EM ,  EMBED ,  FIELDSET ,  FONT ,  FORM ,  hn ,  HR ,  I ,  IFRAME ,  IMG ,  INPUT type=button ,  INPUT type=checkbox ,  INPUT type=file ,  INPUT type=image ,  INPUT type=password ,INPUT type=radio ,  INPUT type=reset ,  INPUT type=submit ,  INPUT type=text ,  KBD ,  LABEL ,  LEGEND ,  LI ,  LISTING ,  MAP ,  MARQUEE ,  MENU ,  NOBR ,  OBJECT ,  OL ,  P ,  PLAINTEXT ,  PRE ,S ,  SAMP ,  SELECT ,  SMALL ,  SPAN ,  STRIKE ,  STRONG ,  SUB ,  SUP ,  TABLE ,  TBODY ,  TD ,  TEXTAREA ,  TextRange ,  TFOOT ,  TH ,  THEAD ,  TR ,  TT ,  U ,  UL ,  VAR ,  WBR ,  XMP ,ControlRangeCollection Constructor ,  Element Constructor ,  TextRange Constructor



兼容性



参考资料:





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值