【发布时间】:2018-09-22 22:05:24
【问题描述】:
基于这个问题:How do I know the IntersectionObserver scroll direction?
我试图在可观察回调中重现一些布局/重排案例,但我做不到,所以我尝试简化用例并最终提出这个问题。
我正在阅读 Paul Irish what-forces-layout.md 的要点,我的问题很简单。
在 body 元素上没有回调的输入肯定会触发布局,请参见下面的示例:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<input type="text">
<script type="text/javascript">
var elementB = document.querySelector('input');
elementB.focus();
</script>
</body>
</html>
see chrome performance record
但如果将focus 事件包装在点击回调中,则不会触发布局/重排。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<input type="text">
<script type="text/javascript">
var elementB = document.querySelector('input');
function onClick() {
elementB.focus();
}
document.addEventListener('click', onClick);
</script>
</body>
</html>
see chrome performance record
这就是我的问题,为什么不触发布局/重排?
【问题讨论】:
-
这种问题只有构建开发工具的人才可以肯定地回答,例如,可能是开发工具没有注册短路回流,因为布局没有'不改变,因此回流无关。
-
@Kaiido,有道理,跨浏览器也很难重现
标签: javascript google-chrome google-chrome-devtools reflow web-performance