这是
实验性技术
检查
浏览器兼容性表格
要小心谨慎在生产中使用这之前。
Range.getBoundingClientRect()
方法返回
DOMRect
object that bounds the contents of the range; this is a rectangle enclosing the union of the bounding rectangles for all the elements in the range.
This method is useful for determining the viewport coordinates of the cursor or selection inside a text box. See
Element.getBoundingClientRect()
for details on the returned value.
boundingRect = range.getBoundingClientRect()
<div id="highlight"></div> <p>This example positions a "highlight" rectangle behind the contents of a range. The range's content <b>starts here</b> and continues on until it <b>ends here</b>. The bounding client rectangle contains everything selected in the range.</p>
#highlight {
background: yellow;
position: absolute;
z-index: -1;
}
p {
width: 200px;
}
const range = document.createRange();
range.setStartBefore(document.getElementsByTagName('b').item(0), 0);
range.setEndAfter(document.getElementsByTagName('b').item(1), 0);
const clientRect = range.getBoundingClientRect();
const highlight = document.getElementById('highlight');
highlight.style.left = `${clientRect.x}px`;
highlight.style.top = `${clientRect.y}px`;
highlight.style.width = `${clientRect.width}px`;
highlight.style.height = `${clientRect.height}px`;
| 规范 | 状态 | 注释 |
|---|---|---|
|
CSSOM (CSS 对象模型) 视图模块
The definition of 'Range.getBoundingClientRect()' in that specification. |
工作草案 | 最初的规范。 |
| 桌面 | 移动 | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
getBoundingClientRect
|
Chrome Yes | Edge 12 | Firefox 4 | IE 9 | Opera 15 | Safari Yes | WebView Android Yes | Chrome Android Yes | Firefox Android 4 | Opera Android 14 | Safari iOS Yes | Samsung Internet Android Yes |
完整支持
实验。期望将来行为有所改变。
Range.getClientRects()
- finer-grained result for non-rectangular ranges (e.g., when the selection wraps onto the next line);
Element.getBoundingClientRect()
Document.caretPositionFromPoint()
- to get the (node, offset) from viewport coordinates.
范围
cloneContents()
cloneRange()
collapse()
compareBoundaryPoints()
compareNode()
comparePoint()
createContextualFragment()
deleteContents()
detach()
extractContents()
getBoundingClientRect()
getClientRects()
insertNode()
intersectsNode()
isPointInRange()
selectNode()
selectNodeContents()
setEnd()
setEndAfter()
setEndBefore()
setStart()
setStartAfter()
setStartBefore()
surroundContents()
toString()
AbortController
AbortSignal
AbstractRange
Attr
ByteString
CDATASection
CSSPrimitiveValue
CSSValue
CSSValueList
CharacterData
ChildNode
注释
CustomEvent
DOMConfiguration
DOMError
DOMErrorHandler
DOMException
DOMImplementation
DOMImplementationList
DOMImplementationRegistry
DOMImplementationSource
DOMLocator
DOMObject
DOMParser
DOMPoint
DOMPointInit
DOMPointReadOnly
DOMRect
DOMString
DOMTimeStamp
DOMTokenList
DOMUserData
Document
DocumentFragment
DocumentType
元素
ElementTraversal
Entity
EntityReference
事件
EventTarget
HTMLCollection
MutationObserver
节点
NodeFilter
NodeIterator
NodeList
NonDocumentTypeChildNode
ProcessingInstruction
PromiseResolver
StaticRange
文本
TextDecoder
TextEncoder
TimeRanges
TreeWalker
TypeInfo
USVString
UserDataHandler
XMLDocument