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

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;}
}
主站蜘蛛池模板: 宁阳县| 桑植县| 漯河市| 思南县| 平山县| 浦城县| 阆中市| 兴化市| 屯昌县| 喀喇| 玉环县| 镇平县| 临泉县| 太湖县| 新巴尔虎右旗| 常德市| 凤凰县| 清丰县| 定兴县| 彰化市| 滕州市| 新绛县| 乌恰县| 水富县| 安阳市| 许昌市| 微博| 马尔康县| 东安县| 德兴市| 赤城县| 昌邑市| 桃园县| 仙桃市| 育儿| 济宁市| 静乐县| 蚌埠市| 蒲城县| 铁岭县| 宿松县|