简体   繁体   English

如何在将类添加到dom元素后强制ie8重绘

[英]How to force ie8 to repaint after adding a class to a dom element

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? 在ie8中,如果元素在更改类名时没有与关联的css“重新绘制”,那么如何强制浏览器刷新而不是杀死ie8性能?

This post ( How can I force WebKit to redraw/repaint to propagate style changes? ) suggested calling offsetHeight which forces a repaint. 这篇文章( 我如何强制WebKit重绘/重绘以传播样式更改? )建议调用offsetHeight来强制重绘。

This post ( http://www.tek-tips.com/viewthread.cfm?qid=1688809 ) had a comment that suggested adding and removing a class from body element. 这篇文章( http://www.tek-tips.com/viewthread.cfm?qid=1688809 )有一条评论建议在body元素中添加和删除一个类。

Both of these approaches killed ie8 performance and the first had side-effects on my layout. 这两种方法都杀死了ie8性能,第一种方法对我的布局产生了副作用。

What's the best approach? 什么是最好的方法?

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. 我为我的ie8问题提出的解决方案是在我正在改变的元素的近父级上添加/删除一个类。 Since I'm using modernizer, I check for ie8 and then do this add/remove dance to get the new css to paint. 因为我正在使用现代化器,所以我检查ie8然后执行此操作添加/删除舞蹈以获得新的css进行绘制。

        $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');
        }

The correct answer to this question is that you need to actually change the content rule. 这个问题的正确答案是您需要实际更改content规则。

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

Notice the extra space I added after Hey! 请注意我在Hey!之后添加的额外空间Hey! on .red-element .red-element

Ridiculously, this works: 可笑的是,这有效:

function ie8Repaint(element) {
    element.html(element.html().replace('>', '>'));
}

Nothing actually changes in the DOM so it won't affect any other browsers. DOM中没有任何实际更改,因此它不会影响任何其他浏览器。

Fire a move or resize event on it. 在其上触发移动或调整大小事件。

var ie8 = whateverYouUseToDetectIE();
var element = document.getElementById("my-element");
element.className += " new-class";
if (ie8) {
    element.fireEvent("resize");
}

This expands on the other answers here. 这扩展了其他答案。 You need to both add a new class ( Doug's answer ) and ensure the class has a different content value ( Adam's answer ). 您需要添加一个新类( Doug的答案 )并确保该类具有不同的内容值( Adam的答案 )。 Changing the class alone may not be enough. 仅仅改变班级可能还不够。 The content change is needed to force IE8 to repaint. 需要更改内容以强制IE8重新绘制。 Here is a related blog post . 这是一篇相关的博客文章

Here is the JQuery to change add the new class: 这是更改添加新类的JQuery:

$(".my-css-class").addClass("my-css-class2");

Here is CSS with the 2nd class having a slightly different content value: 这是第二类具有略微不同内容值的CSS:

.my-css-class:before {content: "\e014";}
.my-css-class2:before {content: "\e014 ";}

I had a lot of difficulty and tried everything to no avail...until I tried this for IE8 我遇到了很多困难并尝试了一切无济于事......直到我为IE8尝试了这个

function toggleCheck(name) {
  el = document.getElementById(name);
  if( hasClass(el, 'checked') ) {
    el.className = el.className.replace(/checked/,'unchecked');
  } else if( hasClass(el, 'unchecked') ) {
    el.className = el.className.replace(/unchecked/,'checked');
  }
  document.body.className = document.body.className;
}
function hasClass(element, cls) {
  return (' ' + element.className + ' ').indexOf(' ' + cls + ' ') > -1;
}

And now my CSS changes for my checkboxes work beautifully for IE8, Chrome, and Firefox! 现在我的CSS更改为我的复选框工作精美的IE8,Chrome和Firefox!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM