- 전체
- HTML
- Web Design (웹디자인)
- XE 응용 개발
- wordpress plugin dev
- Javascript & JavaScript Application
- MEAN Stack : full stack javascript
- angular js & ionic framework
- bootstrap
- WebGL, Three.js and Babylon.js
- restful api design
- mobile web
- node.js 응용
- Cloud Service 응용
- 웹 어셈블리 개발 [WASM, WebAssembly]
- 마이크로서비스, MSA (microservice architecture)
- WebGL / WebGPU
- next.js 개발
- micro frontend (마이크로프론트앤드)
- 전자상거래/쇼핑몰
- 서버 클라우드 (aws, azure, google)
JavaScript [JavaScript] Force-DOM-reflow-JS
2024.05.16 11:56
[JavaScript] Force-DOM-reflow-JS
Have you ever needed to force a dom reflow during transitions or other purposes? Do you find yourself frustrated with the cross browser solutions being too hackish and not-working-right? Then look no further. Simply insert the following 189-byte line of code into the top of your script to solve all your problems.
try{var forceReflowJS=(forceReflowJS=function(a){"use strict";void a.offsetHeight}).call.bind(Object.getOwnPropertyDescriptor(HTMLElement.prototype,"offsetHeight").get)}catch(e){}//anonyco
()
Then, the code above create a forceReflow function that gets passed an Element and reflows the element. Example usage.
try{var forceReflowJS=(forceReflowJS=function(a){"use strict";void a.offsetHeight}).call.bind(Object.getOwnPropertyDescriptor(HTMLElement.prototype,"offsetHeight").get)}catch(e){}//anonyco
forceReflowJS(document.documentElement); // reflows the whole page
The code is guarenteed to work in all current browsers and in IE 9 and up.
You may have seen the following code snippets in production
var ele = document.getElementById("someRandomId");
ele.offsetHeight;
Or, some production snippets even go through the trouble of including a void to increase browser compatiblity.
var ele = document.getElementById("someRandomId");
void ele.offsetHeight;
However, lets face it: theese are hackish solutions that are flimsy and temporary because the browser can easily optimize the above statementes out. However, force-Reflow-JS is different in that it explictily tells the browser not to remove the statement through increasing the complexity of the statement to a degree at which one would not exert without reason and just cause. In English, when the browser sees forceReflowJS, it doesn't just see void ele.offsetHeight, rather it sees much more. Internally, the browser thinks like this when it sees forceReflowJS.
var ele = document.getElementById("someRandomId");
ele["offsetHeight"] /*%<-- VERY IMPORTANT CODE SNIPPET! DO NOT REMOVE! URGENT! THIS IS THE BROWSER SPEAKING TO ITSELF! -->%*/;
Please note that while the alternatives are flimsy, ForceReflowJS unfourtunately has to fall back to void ele.offsetHeight in unsupporting browsers. However, no need to worry too much: these unsupporting browsers likely do not optimize enough to be able to get rid of this flmsy statement.
https://github.com/anonyco/Force-DOM-reflow-JS
// forces a complete reflow of a single element
try {
window.forceReflowJS = Function.prototype.call.bind( // wrap it all up into an auto
Object.getOwnPropertyDescriptor( // obtain the internal funciton that performs the reflow
HTMLElement.prototype,
"offsetHeight"
).get
);
} catch(e) {
window.forceReflowJS = function(a){void a.offsetHeight};
}
광고 클릭에서 발생하는 수익금은 모두 웹사이트 서버의 유지 및 관리, 그리고 기술 콘텐츠 향상을 위해 쓰여집니다.
댓글 0
| 번호 | 제목 | 글쓴이 | 날짜 | 조회 수 |
|---|---|---|---|---|
| 4 |
[Node.js] forever - node.js app 이 죽을경우 자동으로 재실행해주는 모듈
| 졸리운_곰 | 2017.09.26 | 635 |
| 3 |
[Node.js] proxy 환경에서 node.js 설정
| 졸리운_곰 | 2017.08.16 | 485 |
| 2 |
[Node.js] Node.js, Express, MongoDB를 이용하여 REST API 만들기
| 졸리운_곰 | 2016.06.06 | 771 |
| 1 |
Create a Web App and RESTful API Server Using the MEAN Stack
| 졸리운_곰 | 2016.06.05 | 2117 |

