- AMP:Building Accelerated Mobile Pages
- Ruadhan O'Donoghue
- 210字
- 2021-07-08 10:03:57
What's with the <style amp-boilerplate> code?
You might be wondering why you need to include the <style amp-boilerplate> code in every page. Even AMP's creator and tech lead, Malte Ubl, described this code as an atrocity! Unfortunately, it's a necessary evil; a hack developed by the AMP project team to avoid the infamous flash of unstyled content while the AMP page is loading.
It works like this. First, the AMP page content is hidden while it loads. Then, when the AMP-JS library has loaded, it will unhide the page after it has finished rendering it. This presents a problem, however. If the AMP-JS library was ever unavailable when a user requested the page, then the page would stay blank forever. This would be an unacceptable user experience, even if it happened only rarely.
The trick, then, is to use a CSS keyframe animation as a timeout function: if the AMP-JS library fails to make the content visible, then the CSS animation will make it visible automatically after a few seconds, and the user will still get to see some content. A simplified version of this trick, without the vendor prefixes, is shown as follows:
body {
animation: amp-timeout 0s 8s 1 normal forwards;
}
@key-frames amp-timeout {
0% {opacity: 0;}
100% {opacity: 1;}
}
- Cocos2d Cross-Platform Game Development Cookbook(Second Edition)
- OpenStack Cloud Computing Cookbook(Third Edition)
- Visual Basic編程:從基礎(chǔ)到實(shí)踐(第2版)
- Windows系統(tǒng)管理與服務(wù)配置
- 深入淺出WPF
- Webpack實(shí)戰(zhàn):入門、進(jìn)階與調(diào)優(yōu)
- Python算法詳解
- Mastering Akka
- 細(xì)說Python編程:從入門到科學(xué)計(jì)算
- C語言程序設(shè)計(jì)
- Arduino電子設(shè)計(jì)實(shí)戰(zhàn)指南:零基礎(chǔ)篇
- 數(shù)據(jù)科學(xué)中的實(shí)用統(tǒng)計(jì)學(xué)(第2版)
- Mastering OpenStack
- 測試工程師Python開發(fā)實(shí)戰(zhàn)
- HTML5程序設(shè)計(jì)基礎(chǔ)教程