Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stop javascript flicker when page's content is manipulated?

Tags:

javascript

When I makes sites that have javascript that manipulate the page, and this manipulation occurs on page load, I often get a nasty flicker effect.

For instance if I had an accordion, the full content would need to be loaded as html, and then once loaded it could be wrapped up with javascript. This means that for a moment the full content is visible, and then it 'flickers' as some of it is hidden.

One solution would be to hide any flickering content with css, and then show it (as necessary) with the javascript. The problem is then the page wont work properly for people with no javascript.

Is there a better way? Thanks

like image 243
Evanss Avatar asked Jun 28 '11 11:06

Evanss


1 Answers

I think the normal approach to this is to add a 'js' class to body as soon as possible:

<!doctype html>
...
<body>
<script>document.body.className='js';</script>

You'd then adopt some CSS rules to ensure content was hidden when JS is available, something like.

.js .accordion:nth-child(n+1) { display: none }
like image 167
searlea Avatar answered Oct 01 '22 03:10

searlea