这是
实验性技术
检查
浏览器兼容性表格
要小心谨慎在生产中使用这之前。
contentBoxSize
只读特性在
ResizeObserverEntry
interface returns an array containing the new content box size of the observed element when the callback is run.
var myContentBoxSize = ResizeObserverEntry.contentBoxSize;
An object containing the new content box size of the observed element. This object contains two properties:
blockSize
writing-mode
, this is the vertical dimension, or height; if the writing-mode is vertical, this is the horizontal dimension, or width.
inlineSize
writing-mode
, this is the horizontal dimension, or width; if the writing-mode is vertical, this is the vertical dimension, or height.
注意 : For more explanation of writing modes and block and inline dimensions, read Handling different text directions .
The following snippet is taken from the
resize-observer-border-radius.html
(
see source
) example. This example includes a green box, sized as a percentage of the viewport size. When the viewport size is changed, the box's rounded corners change in proportion to the size of the box. We could just implement this using
border-radius
with a percentage, but that quickly leads to ugly-looking elliptical corners; this solution gives you nice square corners that scale with the box size.
const resizeObserver = new ResizeObserver(entries => {
for (let entry of entries) {
if(entry.contentBoxSize) {
entry.target.style.borderRadius = Math.min(100, (entry.contentBoxSize.inlineSize/10) +
(entry.contentBoxSize.blockSize/10)) + 'px';
} else {
entry.target.style.borderRadius = Math.min(100, (entry.contentRect.width/10) +
(entry.contentRect.height/10)) + 'px';
}
}
});
resizeObserver.observe(document.querySelector('div'));
| 规范 | 状态 | 注释 |
|---|---|---|
|
重置大小观测器
The definition of 'target' in that specification. |
编者草案 | 初始定义。 |
| 桌面 | 移动 | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
contentBoxSize
|
Chrome No | Edge No | Firefox 69 | IE No | Opera No | Safari No | WebView Android No | Chrome Android No | Firefox Android No | Opera Android No | Safari iOS No | Samsung Internet Android No |
完整支持
不支持
实验。期望将来行为有所改变。
ResizeObserverEntry
borderBoxSize
contentBoxSize
contentRect
target