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

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;}
}
主站蜘蛛池模板: 临城县| 东兰县| 永德县| 邯郸市| 内黄县| 东辽县| 兴海县| 鄱阳县| 南安市| 沙坪坝区| 万盛区| 阿勒泰市| 怀宁县| 刚察县| 东阳市| 隆安县| 桃园县| 徐州市| 五华县| 台东市| 吐鲁番市| 长岛县| 凯里市| 唐山市| 五指山市| 永昌县| 安宁市| 紫金县| 来安县| 宜兰县| 射洪县| 汕尾市| 泰来县| 保德县| 临西县| 大方县| 会宁县| 肇庆市| 黑山县| 三都| 枝江市|