Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to force ie8 to repaint after adding a class to a dom element

Tags:

In ie8 if elements don't 'repaint' with the associated css when you change the classname, how can you force the browser to refresh and not kill ie8 performance?

This post (How can I force WebKit to redraw/repaint to propagate style changes?) suggested calling offsetHeight which forces a repaint.

This post (http://www.tek-tips.com/viewthread.cfm?qid=1688809) had a comment that suggested adding and removing a class from body element.

Both of these approaches killed ie8 performance and the first had side-effects on my layout.

What's the best approach?

like image 517
Doug Avatar asked Dec 09 '12 01:12

Doug


2 Answers

The solution I came up with for my ie8 issue was to add/remove a class on a near parent of the element i'm changing. Since I'm using modernizer, I check for ie8 and then do this add/remove dance to get the new css to paint.

        $uicontext.addClass('new-ui-look');
        if ($('html').is('.ie8')) {
            // IE8: ui does not repaint when css class changes
            $uicontext.parents('li').addClass('z').removeClass('z');
        }
like image 79
Doug Avatar answered Sep 27 '22 21:09

Doug


The correct answer to this question is that you need to actually change the content rule.

.black-element:before { content:"Hey!"; color: #000;}
.red-element:before { content:"Hey! "; color: #f00;}

Notice the extra space I added after Hey! on .red-element

like image 22
Adam Avatar answered Sep 27 '22 21:09

Adam