- AMP:Building Accelerated Mobile Pages
- Ruadhan O'Donoghue
- 412字
- 2021-07-08 10:04:09
Placeholders
An element with the placeholder attribute acts as a placeholder for its parent. Placeholders are displayed immediately for an element, before the element has been downloaded or initialized. When the actual element is ready, the placeholder is hidden, and the element content is displayed.
Thus, placeholders can be used to stand in place of a slower-to-load element. You might use this, for instance, to display a fast-loading, low-resolution image in place of a video or high-resolution image. The latter is the same trick that sites such as Medium (medium.com) and Facebook use to deliver a quick page-loading experience. They show a small, low-resolution image that has been blown up to the size of the actual banner image. The user gets to see the proper page layout almost immediately, with a blurred version of the main feature image. This is swapped out when the high-resolution version is ready.
Let's implement this for our article's main feature image. First, prepare the small placeholder version of the image. Open the original image, in our case /ch3/img/feature.jpg, in an image editor application or online service, and resize it to 40 pixels wide. Save the image as feature-tiny.jpg.
After running it through an optimizer, your placeholder image should be down to around 200-300 B in size. This will load quickly even on slow networks.
Now we need to write the AMP-HTML. To set an element as a placeholder, we just need to add the placeholder attribute. So, to add a placeholder for our feature image, we could use this code (/ch3/placeholder.html):
<amp-img src = "img/feature.jpg"
srcset = "img/feature-1200.jpg 1200w,
img/feature-lrg.jpg 1080w,
img/feature-med.jpg 768w,
img/feature.jpg 320w"
width = "768"
height = "305"
layout = "responsive"
noloading>
<amp-img placeholder
src = "img/feature-tiny.jpg"
width = "768"
height = "305"
layout = "responsive">
</amp-img>
</amp-img>
Yes, we're using an amp-img placeholder for an amp-img! In general, though, you might be more likely to use placeholders for videos or other large elements.
We've also added a noloading attribute to the feature image element. AMP displays a loading indicator by default for components like amp-img. Adding the noloading attribute hides this indicator. The idea here is that since we are using a placeholder image anyway, we don't really need the loading indicator, so we disable it.
We can confirm the placeholder is working for poor connections in the developer tools:

We can simulate a slow connection in Chrome's DevTools as shown in the previous image (or indeed you can set your own mobile device to use 2G). You should see that the placeholder image, feature-tiny.jpg, is loaded and displayed very quickly, and is eventually replaced with the full-resolution image.
- MySQL數據庫應用與管理 第2版
- PHP 7底層設計與源碼實現
- JIRA 7 Administration Cookbook(Second Edition)
- PyQt從入門到精通
- ASP.NET Core Essentials
- Unreal Engine 4 Shaders and Effects Cookbook
- C語言課程設計
- Creating Stunning Dashboards with QlikView
- OpenCV with Python Blueprints
- Flink技術內幕:架構設計與實現原理
- Android智能手機APP界面設計實戰教程
- Java多線程并發體系實戰(微課視頻版)
- Java EE程序設計與開發實踐教程
- R語言:邁向大數據之路
- MonoTouch應用開發實踐指南:使用C#和.NET開發iOS應用