简体   繁体   English

在动态html中呈现外部,内部,内联CSS样式之间的区别

[英]Difference between rendering external, internal, inline css styles in dynamic html

There is article about how browsers internal works . 有一篇关于浏览器内部工作原理的文章。 Under the 'Dynamic Pages' there is explanation of how dynamic css applied to web page using javascript. 在“动态页面”下,说明了如何使用JavaScript将动态CSS应用于网页。

Pages can change because of JavaScript or because of user interaction which triggers parts of the rendering process: 页面可能由于JavaScript或触发交互过程的部分用户交互而改变:

在此处输入图片说明

(Source: https://developer.mozilla.org/en/Introduction_to_Layout_in_Mozilla ) (来源: https : //developer.mozilla.org/en/Introduction_to_Layout_in_Mozilla

  • If DOM elements are added or removed, the typical response of the browser is to follow the rendering process described earlier in almost serial order 如果添加或删除了DOM元素,则浏览器的典型响应是按照几乎先前的顺序执行前面描述的渲染过程。

  • If the Style attribute on an element is changed, the style for the element needs to be recomputed, the page re-flown and re-painted 如果更改了元素的样式属性,则需要重新计算该元素的样式,并重新绘制页面并重新绘制

    • Browsers may optimize this by batching style re-computes by queuing them 浏览器可以通过对样式进行重新批处理来对它们进行排队,从而对其进行优化
    • However, scripts often read back changes that they have just made which requires the re-styling queue to be flushed 但是,脚本通常会回读它们刚进行的更改,这需要刷新重新样式化队列
    • For better performance, make style changes as a batch and then read them in a batch so that the queue is flushed less frequently 为了获得更好的性能,请批量更改样式,然后批量读取它们,以减少刷新队列的频率
  • Some style changes are cheaper: 一些样式更改比较便宜:

    • Changing size / location would not require style re-compute but only re-flowing and re-painting 更改大小/位置将不需要重新计算样式,而仅需要重排和重新绘制

    • Color change does not require re-flowing, but only re-painting 颜色变化不需要重涂,而只需重涂

    • Scrolling also does not require re-computation, but only re-painting this is typically done incrementally and may not even require full repainting (but things like fixed background images would necessitate full repainting). 滚动也不需要重新计算,但是通常仅以增量方式进行重新绘制,甚至可能不需要完全重新绘制(但固定背景图像之类的东西则需要完全重新绘制)。 So moving elements by scrolling programmatically can be faster than moving elements by modifying their style attribute 因此,通过程序滚动来移动元素比通过修改样式属性来移动元素要快

  • Re-Flow - because of position or size changes - is typically recursive (root to leafs) 由于位置或大小的变化,重流通常是递归的(从根到叶子)

    • Some attribute changes in a child can trigger changes in the entire ancestry all the way up to the root. 子级中的某些属性更改可以触发整个祖先直到根的更改。 Example: Height changes 示例:高度变化

    • Some attribute changes in a parent can trigger changes in all the descendants right down to leaves. 父级中的某些属性更改可以触发所有后代的更改,直到叶子。 Example: Width changes 示例:宽度变化

    • Browsers can detect that only a section of the tree may change and do re-flow only on that sub-tree 浏览器可以检测到只有一部分树可以更改,并且仅在该子树上进行重排

However I did not understand does same order of procedure applies (external, internal and inline) to all type of css styles changes when it is dynamically applied using javascript? 但是我不明白,当使用javascript动态应用css样式更改时,是否对所有类型的css样式更改应用相同的过程顺序(外部,内部和内联)? Or it is some how different? 还是有些不同? If it is different which parts of the rendering process will be applied to external? 如果不同,则渲染过程的哪些部分将应用于外部? internal? 内部? and inline? 和内联?

if you use external css then flow would be the same as shown in your link. 如果使用外部CSS,则流量将与链接中所示的相同。 Otherwise if you use internal or inline css, ultimately CSS is in HTML page only so it would load in first step of HTML. 否则,如果您使用内部或内联CSS,最终CSS仅在HTML页面中,因此它将在HTML的第一步中加载。

However it is not recommended to add styles inline or internal as it would take so much time to show your basic page too. 但是,不建议添加内联或内部样式,因为这也会花费很多时间来显示基本页面。

Yes. 是。 The procedure would be the same for the most part. 大部分过程是相同的。 but remember, only the styles on elements that have been updated will be repainted. 但是请记住,只有已更新的元素上的样式才会重新绘制。 You wouldn't necessarily have to repaint the entire page, only the areas that have changed. 您不必重新绘制整个页面,只需重新绘制已更改的区域即可。

It is always a nice practice to keep the styles separate from the mark-up. 始终将样式与标记分开是一种很好的做法。 The HTML should live within it's file as the structure of the page, then the CSS should live within it's file(s) to maintain the control of the design in an external resource for easier editing. HTML应该作为页面的结构存在于其文件中,然后CSS应该存在于其文件中,以在外部资源中维护对设计的控制,以便于编辑。 The same is true for scripting as well. 脚本编写也是如此。

暂无
暂无

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

相关问题 使用javascript将外部和内部样式转换为嵌入式样式 - Convert external and internal styles into inline styles with javascript 如何使用客户端 javascript 将外部/内部 css 样式转换为内联样式属性 - How to convert external/internal css styles into inline style-attribute using client-side javascript jQuery - 如何使用元素的HTML获取所有样式/ css(在内部/外部文档中定义) - jQuery - How to get all styles/css (defined within internal/external document) with HTML of an element jQuery .html()并插入内联CSS样式? - jQuery .html() and inject inline CSS styles? 将外部CSS样式表转换为内联样式,或转换为<style> tag, via Javascript - Convert external CSS stylesheet into inline styles, or into a <style> tag, via Javascript JavaScript创建的内联样式和JavaScript创建的样式表之间的性能差异 - Performance difference between JavaScript created inline styles and JavaScript created stylesheets JSX CSS到内联样式 - JSX css to inline styles CSS Transitions:在“ height”样式属性的内联样式之间进行动画处理 - CSS Transitions: Animating between inline styles for “height” style attribute 如何使用内联 css styles 插入 HTML 并使用 ZDE9B9ED78D7E2E1DCEEFFEE780E2F919 进行渲染 - How to insert HTML with inline css styles and render using javascript 使用npm包将css文件转换为HTML内联样式 - convert css file into inline styles in HTML using npm package
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM