简体   繁体   English

批量更新跨多个元素的 Javascript 中的 CSS 值?

[英]Updating CSS Values in Javascript across multiple elements in batch?

I have a web app I'm working on that requires frequent updates to CSS values across multiple elements.我有一个正在开发的网络应用程序,它需要经常更新跨多个元素的 CSS 值。 I'm trying to reduce reflow and was wondering if there is a way to accomplish this in batch?我正在尝试减少回流,并想知道是否有办法批量完成此操作? Here's an example:下面是一个例子:

for(let j = 0; j<defholdids.length; j++) {
    $('#'+defholdids[j]).css({
      'z-index':'4',
      'color':'rgba(0,0,0,0.7)',
    });

defholdids is an array with the element ID's stored. defholdids是一个存储元素 ID 的数组。 The loop iterates through and updates the css values.循环遍历并更新 css 值。 To my understanding, a reflow is triggered each time an element css value is updated.据我了解,每次更新元素 css 值时都会触发回流。 Is there a way to update the CSS values across all the elements AND THEN 'reflow' rather than triggering a 'reflow' each iteration?有没有办法更新所有元素的 CSS 值,然后“回流”而不是每次迭代都触发“回流”?

I'm working with Electron to create a desktop app, not sure if that makes a difference.我正在与 Electron 合作创建一个桌面应用程序,不确定这是否有所作为。

How about assigning one class name for the elements that require cssupdates and update them at once.如何为需要 cssupdates 的元素分配一个类名并立即更新它们。 For example例如

   $('.class-name-goes-here').css({
      'z-index':'4',
      'color':'rgba(0,0,0,0.7)',
    });

You're nearly always better off by introducing a (second) class then adding/toggling/removing that class on the appropriate element(s), instead of directly manipulating the .css() of elements.通过引入(第二个)类然后在适当的元素上添加/切换/删除该类,而不是直接操作元素的.css() ,您几乎总是更好。

In Joseph's answer he introduces .class-name-goes-here (which I'll shorten to .main-class在约瑟夫的回答中,他介绍了.class-name-goes-here (我将其.main-class.main-class
If you introduce a second/additional class:如果您引入第二个/附加类:

.aux-class {
  z-index:4;
  color: rgba(0,0,0,0.7);
}

you can do $('.main-class').addClass('aux-class');你可以做$('.main-class').addClass('aux-class');
This would apply the change to all of the .main-class elements all at once.这会将更改同时应用于所有.main-class元素。

You can change it back later just by $('.main-class').removeClass('aux-class');你可以稍后通过$('.main-class').removeClass('aux-class');把它改回来$('.main-class').removeClass('aux-class');

If you have well-known & well-maintained state you can use .toggleClass() instead of separate adds and removes.如果您有众所周知且维护良好的状态,则可以使用.toggleClass()而不是单独的添加和删除。

This doesn't prevent you from doing further manipulations with $(element).css() but those changes would have to be reverted "manually".这不会阻止您使用$(element).css()进行进一步操作,但必须“手动”还原这些更改。 I would probably introduce another different (third? fourth?) class instead.我可能会介绍另一个不同的(第三?第四?)类。

It's also good to name the class for the state (or object) it represents.为它代表的状态(或对象)命名类也很好。
Those names depend on what the things are and what you're doing with them.这些名称取决于事物是什么以及你用它们做什么。 Maybe instead of .main-class and .aux-class good names could be .toggleable and .active , so possibly也许代替.main-class.aux-class好名字可能是.toggleable.active ,所以可能

<section>
  <div class="toggleable">
    ...some content...
  </div>

  <div class="something-else">
    ...some other content...
  </div>

  <div class="toggleable">
    ...even more content...
  </div>

  <div>
    ...no particular class on this div...
  </div>
</section>

Then, on a button press (for example) you would do然后,在按下按钮时(例如)你会做
$('.toggleable').addClass('active')

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

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