Force browser to immediately repaint a dom element

偶尔善良 提交于 2019-12-02 09:25:36

The way you did it, you're not releasing control to the browser between the display of the preloader and the display of the 'big html'.

Rather than encapsulating this whole block inside a setTimeout(), you should just differ the rendering part.

Please try something along those lines:

// Define variables
let domPreloader = document.getElementById('preloader');
let domContainer = document.getElementById('container');

// Display preloader
domPreloader.style.webkitTransform = 'scale(1)';
domPreloader.style.display = 'block';

// Render a big html
setTimeout(render, 100);

function render() {
  const html = Array(100000).fill("<div>1</div>");
  domContainer.innerHTML = html;

  // Hide preloader
  domPreloader.style.display = 'none';
}

JSFiddle

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!