官术网_书友最值得收藏!

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;}
}
主站蜘蛛池模板: 建昌县| 武安市| 三明市| 台北县| 黑河市| 光山县| 象州县| 修武县| 禹城市| 濉溪县| 泗阳县| 太仆寺旗| 柘城县| 双峰县| 洞口县| 铜陵市| 阜康市| 岑巩县| 子长县| 武隆县| 商水县| 阳高县| 大姚县| 浙江省| 个旧市| 项城市| 喀什市| 江西省| 哈密市| 汝阳县| 南宫市| 商河县| 广南县| 平凉市| 东兴市| 伊川县| 平度市| 衡阳市| 萨嘎县| 黎平县| 富平县|