Table of Contents
总结:
Home Web Front-end HTML Tutorial 学习通过CSS硬件加速提升你网站的性能_html/css_WEB-ITnose

学习通过CSS硬件加速提升你网站的性能_html/css_WEB-ITnose

Jun 24, 2016 am 11:30 AM

知道我们可以通过卸载CSS图形特性到GPU(图形处理单元)来硬件加速CSS的图形特性,以获得在浏览器中的更好渲染效果吗?

如今大多数计算机都有适合显卡的硬件加速功能。因此,我们就可以为那些大功耗的利用GPU的能量,进而我们的网站和应用可以在CPU上的浏览器中运行得更快。

在这篇文章,我们将 CSS 硬件加速应用在桌面和移动浏览器上。

在桌面和手机浏览器上的CSS硬件加速

有没有想过一些 CSS 动画怎么如此顺利运行在浏览器中?

CSS 动画、 转换和过渡并不会自动进行GPU 加速,取而代之从浏览器的较慢的渲染引擎执行。那么怎么精确地强制浏览器切换到 GPU 模式呢?许多浏览器都提供了通过某些 CSS 规则进行 GPU 加速。

目前,一些浏览器像Chrome、 火狐、 Safari、 IE9 + 和最新版本的欧鹏浏览器都配备了硬件加速功能;它们只是当有迹象表明一个DOM元素要使用它的时候才会使用到。

例如:

  1. .cube {
  2.   -webkit-transform: translate3d(250px,250px,250px)
  3.   rotate3d(250px,250px,250px,-120deg)
  4.   scale3d(0.5, 0.5, 0.5);
  5.   } 

 

在某些情况下,您可能不希望 在元素上进行3D 转换,但是仍然利用 GPU 加速。那时就可以使用几个简单的 CSS 属性,诱使浏览器触发 GPU 加速。

即使我们不在 3D 空间中创建动画元素,我们也可以启用 3D 渲染。至少,"transform:translateZ(0);"可以宣称触发 GPU 加速在现代的桌面和移动浏览器中。这似乎是最有效的触发 GPU 加速的方法(包括所有的浏览器前缀):

  1.  .cube {
  2.   -webkit-transform: translateZ(0);
  3.   -moz-transform: translateZ(0);
  4.   -ms-transform: translateZ(0);
  5.   -o-transform: translateZ(0);
  6.   transform: translateZ(0);
  7.   /* Other transform properties here */
  8.   }

 

在 Chrome 和 Safari中, 当我们使用 CSS 转换或动画可能会看到闪烁的效果。下面的声明可以用于解决此问题: 

  1.  .cube {
  2.   -webkit-backface-visibility: hidden;
  3.   -moz-backface-visibility: hidden;
  4.   -ms-backface-visibility: hidden;
  5.   backface-visibility: hidden;
  6.   -webkit-perspective: 1000;
  7.   -moz-perspective: 1000;
  8.   -ms-perspective: 1000;
  9.   perspective: 1000;
  10.   /* Other transform properties here */
  11.   }

 

 另外一种似乎可以解决Webkit引擎的桌面和移动浏览器问题的方式是使用translate3d:

  1.   .cube {
  2.   -webkit-transform: translate3d(0, 0, 0);
  3.   -moz-transform: translate3d(0, 0, 0);
  4.   -ms-transform: translate3d(0, 0, 0);
  5.   transform: translate3d(0, 0, 0);
  6.   /* Other transform properties here */
  7.   }

 

 本地移动应用程序也能利用设备的GPU加速——这就是为什么人们都知道它们表现的比Web应用程序更好。使用硬件加速在移动设备上尤其有用,这是因为它可以有效减少设备上的资源消耗。

总结:

我们所涉及的方法应仅用于我们正在进行动画处理的元素。它们可以提升在2D动画上的表现,但是仅仅为了硬件加速而把它们应用在所有的上面是不明智的。

小心地使用它们的每一种方式,并且只在需要体验真正的表现性能的时候。不必要地使用 GPU 可以导致重大的性能问题,因为它增加了内存的使用 — — — — 它也会影响在移动设备上的电池寿命。

你有没有试过在项目中使用这些方法呢?如果有试过,请大家分享一下哪一个是最有效和表现最好的。

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1659
14
PHP Tutorial
1258
29
C# Tutorial
1232
24
Is HTML easy to learn for beginners? Is HTML easy to learn for beginners? Apr 07, 2025 am 12:11 AM

HTML is suitable for beginners because it is simple and easy to learn and can quickly see results. 1) The learning curve of HTML is smooth and easy to get started. 2) Just master the basic tags to start creating web pages. 3) High flexibility and can be used in combination with CSS and JavaScript. 4) Rich learning resources and modern tools support the learning process.

Understanding HTML, CSS, and JavaScript: A Beginner's Guide Understanding HTML, CSS, and JavaScript: A Beginner's Guide Apr 12, 2025 am 12:02 AM

WebdevelopmentreliesonHTML,CSS,andJavaScript:1)HTMLstructurescontent,2)CSSstylesit,and3)JavaScriptaddsinteractivity,formingthebasisofmodernwebexperiences.

The Roles of HTML, CSS, and JavaScript: Core Responsibilities The Roles of HTML, CSS, and JavaScript: Core Responsibilities Apr 08, 2025 pm 07:05 PM

HTML defines the web structure, CSS is responsible for style and layout, and JavaScript gives dynamic interaction. The three perform their duties in web development and jointly build a colorful website.

What is an example of a starting tag in HTML? What is an example of a starting tag in HTML? Apr 06, 2025 am 12:04 AM

AnexampleofastartingtaginHTMLis,whichbeginsaparagraph.StartingtagsareessentialinHTMLastheyinitiateelements,definetheirtypes,andarecrucialforstructuringwebpagesandconstructingtheDOM.

HTML, CSS, and JavaScript: Essential Tools for Web Developers HTML, CSS, and JavaScript: Essential Tools for Web Developers Apr 09, 2025 am 12:12 AM

HTML, CSS and JavaScript are the three pillars of web development. 1. HTML defines the web page structure and uses tags such as, etc. 2. CSS controls the web page style, using selectors and attributes such as color, font-size, etc. 3. JavaScript realizes dynamic effects and interaction, through event monitoring and DOM operations.

HTML: The Structure, CSS: The Style, JavaScript: The Behavior HTML: The Structure, CSS: The Style, JavaScript: The Behavior Apr 18, 2025 am 12:09 AM

The roles of HTML, CSS and JavaScript in web development are: 1. HTML defines the web page structure, 2. CSS controls the web page style, and 3. JavaScript adds dynamic behavior. Together, they build the framework, aesthetics and interactivity of modern websites.

The Future of HTML: Evolution and Trends in Web Design The Future of HTML: Evolution and Trends in Web Design Apr 17, 2025 am 12:12 AM

The future of HTML is full of infinite possibilities. 1) New features and standards will include more semantic tags and the popularity of WebComponents. 2) The web design trend will continue to develop towards responsive and accessible design. 3) Performance optimization will improve the user experience through responsive image loading and lazy loading technologies.

The Future of HTML, CSS, and JavaScript: Web Development Trends The Future of HTML, CSS, and JavaScript: Web Development Trends Apr 19, 2025 am 12:02 AM

The future trends of HTML are semantics and web components, the future trends of CSS are CSS-in-JS and CSSHoudini, and the future trends of JavaScript are WebAssembly and Serverless. 1. HTML semantics improve accessibility and SEO effects, and Web components improve development efficiency, but attention should be paid to browser compatibility. 2. CSS-in-JS enhances style management flexibility but may increase file size. CSSHoudini allows direct operation of CSS rendering. 3.WebAssembly optimizes browser application performance but has a steep learning curve, and Serverless simplifies development but requires optimization of cold start problems.

See all articles