简体   繁体   English

如何在使用 javaScript 附加 css 时减少 dom 操作

[英]How to reduce dom manipulation while appending css using javaScript

I have some dynamically created html whole css properties are in an object. So they need to be given using Javascript. So I have used the method below for appending to styles to the dynamically created dom element.我有一些动态创建的 html 整个 css 属性在 object 中。因此需要使用 Javascript 给它们。所以我使用下面的方法将 styles 附加到动态创建的 dom 元素。 I believe it's costly as each time a I'm doing a dom manipulation for appending styles. In the below code itself 10 lines are doing css dom manipulation.我相信这是昂贵的,因为每次我都在为附加 styles 进行 dom 操作。在下面的代码本身中,有 10 行正在执行 css dom 操作。

function createNavbar() {
  let nav;
  nav = '<div class="navbar" id="nav"></div>';
  this.body.insertAdjacentHTML("beforeend", nav);
  let navBar = document.getElementById("nav");
  navBar.style.display = "block"; // style appending begins
  navBar.style.background = this.navColors.notice.bg;
  navBar.style.color = this.navColors.notice.textColor;
  navBar.style.borderWidth = "1px";
  navBar.style.borderStyle = "solid";
  navBar.style.borderColor = this.navColors.notice.borderColor;
  navBar.style.top = this.navPositionValue[this.position].top;
  navBar.style.right = this.navPositionValue[this.position].right;
  navBar.style.bottom = this.navPositionValue[this.position].bottom;
  navBar.style.left = this.navPositionValue[this.position].left; // style appending ends
}

Found a better way from this SO Question , so I used the below line to replace the 10 line code ( used the cssText property )这个 SO Question中找到了更好的方法,所以我使用下面的行来替换 10 行代码(使用 cssText 属性)

navBar.style.cssText = `display: block; color: ${this.navColors.notice.textColor}; background: ${this.navColors.notice.bg}; border: 1px solid ${this.navColors.notice.borderColor}; top: ${this.navPositionValue[this.position].top}; right: ${this.navPositionValue[this.position].right}; bottom: ${this.navPositionValue[this.position].bottom}; left: ${this.navPositionValue[this.position].left};`

By using this method I hope the Dom manipulation is reduced to 1 instead of 10, but the line is pretty long and hard to read.通过使用这种方法,我希望 Dom 操作减少到 1 而不是 10,但是这行代码很长而且难以阅读。 I would like to know if there is any better way than this to append long lines or many css in js我想知道是否有比这更好的方法来处理 append 长行或 js 中的许多 css

just add a class with common styles then add the class to the dom.只需添加一个 class 和 common styles,然后将 class 添加到 dom。 isn't that the easiest way?这不是最简单的方法吗?

Your code above will only cause one update of the page because of how the Browser works.由于浏览器的工作方式,您上面的代码只会导致页面更新一次 You do not have to worry about optimizing it.您不必担心优化它。

Basically, all your changes will be applied at once after your function has ended, meaning that even if you change element.style multiple times inside one function, it is still just one operation.基本上,您的所有更改将在您的 function 结束立即应用,这意味着即使您在一个 function 中多次更改element.style ,它仍然只是一个操作。

For more information as to why and how this works I recommend this talk by Jake Archibald about how the Event Loop works.有关为什么以及如何工作的更多信息,我推荐Jake Archibald关于事件循环如何工作的演讲。

You can add linebreaks inside template literals to increase readability.您可以在模板文字中添加换行符以提高可读性。

Another option is to destructure some of the objects so we don't have really long variable names that we have to repeat.另一种选择是解构一些对象,这样我们就没有必须重复的非常长的变量名。

 const navBar = document.querySelector(".navBar"); const navColors = { notice: { textColor: "red", bg: "green", borderColor: "blue", } }; const navPositionValue = { top: "5px", right: "10px", bottom: "15px", left: "20px", }; // destructering so we dont have to repeat "navColors.notice" or "navPositionValue" for each value const {textColor, bg, borderColor} = navColors.notice; const {ttop, right, bottom, left} = navPositionValue; navBar.style.cssText = ` display: block; color: ${textColor}; background: ${bg}; border: 1px solid ${borderColor}; top: ${ttop}; right: ${right}; bottom: ${bottom}; left: ${left}; `;
 .navBar { padding: 20px; }
 <div class="navBar"> I'm a navBar </div>

You can use template literals您可以使用模板文字

And you can marge css border并且你可以marge css border

function createNavbar() {
  let nav = `
        <div class="navbar" id="nav" style = "
          display: block;
          background: ${this.navColors.notice.bg};
          color: ${this.navColors.notice.textColor};
          border: 1px solid ${this.navColors.notice.borderColor};
          top: ${this.navPositionValue[this.position].top};
          right: ${this.navPositionValue[this.position].right};
          bottom: ${this.navPositionValue[this.position].bottom};
          left: ${this.navPositionValue[this.position].left};
        ">
        </div>
  `
  document.body.insertAdjacentHTML("beforeend", nav);
}

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

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