简体   繁体   English

是否通过香草JavaScript异步将CSS样式添加到HTML元素?

[英]Is adding css styling to a HTML element through vanilla JavaScript async?

fellow nerds, I recently stumbled across something rather strange, that essentially breaks the web app I'm working on... Which is that css attributes I add to an HTML element through (vanilla) javascript, seems to be async, to demonstrate that I have a small piece of code... 那些书呆子,最近我偶然发现了一个相当奇怪的东西,它从本质上破坏了我正在开发的Web应用程序...这就是我通过(原始)javascript添加到HTML元素的css属性似乎是异步的,以证明我有一小段代码...

   ...

    _( leftView ).addClass('notrans');
    _( rightView ).addClass('notrans');

    if ( fromLeft ) {
        if ( !rightFocused ) {
            _( leftView ).css({ 'left' : '-100%' })
            _( rightView ).css({ 'left' : '-50%' });
        } else _( leftView ).css({ 'left' : '-50%' });
    }

    if ( fromRight ) {
        if ( !leftFocused ) {
            _( leftView ).css({ 'left' : '100%' });
            _( rightView ).css({ 'left' : '150%' });
        } else _( rightView ).css({ 'left' : '100%' });
    }

    _( leftView ).removeClass('notrans');
    _( rightView ).removeClass('notrans');

    ...

NOTE: The small underscore you see everywhere, is a small library I made and tested... And I'm 99% sure that the CSS, add class and remove class functions work. 注意:您到处都可以看到的下划线是我制作和测试的一个小型库...而且我99%确信CSS,添加类和删除类函数都可以正常工作。

Now, the problem is that when I get to the remove class part of the code ( last two lines ), the CSS doesn't seem to have been applied yet. 现在的问题是,当我到达代码的remove class部分(最后两行)时,CSS似乎尚未被应用。 So when it finally does, I will already have removed the notrans class, and the elements will move with a transition instead of none. 因此,当最终完成时,我将已经删除了notrans类,并且元素将随着过渡而不是没有过渡而移动。 This can be fixed with a 1ms timeout to the removal of the classes but will break some other stuff down the line... So is there anybody that knows if adding CSS attributes through JS is async, and how to fix it?:) 可以通过删除类的1毫秒超时来解决此问题,但会破坏其他内容……因此,有人知道通过JS添加CSS属性是否异步,以及如何解决该问题吗?

Aske K. 阿斯克·K。

Yes and no. 是的,没有。

When you have JavaScript making changes to the CSS, the changes are queued and then all applied together when the JavaScript is done. 当您使用JavaScript对CSS进行更改时,更改将排队,然后在完成JavaScript后将所有更改一起应用。

Exception: certain properties cause a recalculation to be done. 例外:某些属性导致重新计算。 For example, getting measurements like offsetHeight causes the browser to apply any pending CSS changes, make the measurement, then continue. 例如,获取诸如offsetHeight之类的offsetHeight会导致浏览器应用所有未决的CSS更改,进行度量,然后继续。

However, I don't know for sure if transition s are properly applied even if a measurement is done. 但是,即使测量完成,我也不知道是否正确应用了transition The easy way would be to make the removeClass async, by sticking it in setTimeout(... , 1) or (better) requestAnimationFrame (which is called the very next frame, well after the CSS has updated) 最简单的方法是通过将removeClass粘贴在setTimeout(... , 1)或(更好) requestAnimationFrame (在CSS更新之后,称为下一帧)中使其异步。

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

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