{"id":8981,"date":"2019-09-30T00:20:54","date_gmt":"2019-09-29T16:20:54","guid":{"rendered":"https:\/\/www.zhangxinxu.com\/wordpress\/?p=8981"},"modified":"2019-10-13T22:55:08","modified_gmt":"2019-10-13T14:55:08","slug":"native-img-loading-lazy","status":"publish","type":"post","link":"https:\/\/www.zhangxinxu.com\/wordpress\/2019\/09\/native-img-loading-lazy\/","title":{"rendered":"\u6d4f\u89c8\u5668IMG\u56fe\u7247\u539f\u751f\u61d2\u52a0\u8f7dloading=&#8221;lazy&#8221;\u5b9e\u8df5\u6307\u5357"},"content":{"rendered":"<p>by <a href=\"https:\/\/www.zhangxinxu.com\/\">zhangxinxu<\/a> from <a href=\"https:\/\/www.zhangxinxu.com\/wordpress\/?p=8981\">https:\/\/www.zhangxinxu.com\/wordpress\/?p=8981<\/a><br \/>\n\u672c\u6587\u6b22\u8fce\u5206\u4eab\u4e0e\u805a\u5408\uff0c\u5168\u6587\u8f6c\u8f7d\u5c31\u4e0d\u5fc5\u4e86\uff0c\u5c0a\u91cd\u7248\u6743\uff0c\u5708\u5b50\u5c31\u8fd9\u4e48\u5927\uff0c\u82e5\u6025\u7528\u53ef\u4ee5\u8054\u7cfb\u6388\u6743\u3002<\/p>\n<p><script>window.wxShareImgUrl = 'https:\/\/www.zhangxinxu.com\/study\/image\/share\/201909-loading-lazy.png';<\/script><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"\/\/image.zhangxinxu.com\/image\/blog\/201909\/old-tape-loading.png\" width=\"396\" height=\"275\" alt=\"\u539f\u751f\u61d2\u52a0\u8f7d\" class=\"alignright thumb clip size-medium\" \/><\/p>\n<h3>\u4e00\u3001Lazy loading Chrome 76\u652f\u6301\u5566<\/h3>\n<p>\u5e74\u521d\u5c31\u542c\u8bf4Chrome\u6d4f\u89c8\u5668\u7684IMG\u56fe\u7247\u548cIFRAME\u6846\u67b6\u4f1a\u652f\u6301\u539f\u751f\u61d2\u52a0\u8f7d\u7279\u6027\uff0c\u4f7f\u7528<code>loading=\"lazy\"<\/code>\u8bed\u6cd5\u3002<\/p>\n<p>\u76fc\u661f\u661f\u76fc\u6708\u4eae\uff0c\u7ec8\u4e8e\uff0c\u517c\u5bb9\u6027\u8868\u4e2d\u7684\u683c\u5b50\u7eff\u5566\uff0c\u7eff\u8272\u771f\u597d\uff0cChrome76\u652f\u6301\uff01<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"\/\/image.zhangxinxu.com\/image\/blog\/201909\/2019-09-29_212826.png\" width=\"575\" height=\"381\" alt=\"\u56fe\u7247\u539f\u751f\u61d2\u52a0\u8f7d\u517c\u5bb9\u6027\" class=\"alignnone size-medium\" \/><\/p>\n<p>\u6211<span class=\"hidden\" hidden>\u5f20\u946b\u65ed<\/span>\u4e00\u770b\u81ea\u5df1\u7684\u6d4f\u89c8\u5668\uff0c\u563f\u563f\u563f\uff0c\u5df2\u7ecf\u5347\u7ea7\u5230\u6700\u8fd1\u7248\u672c\uff0c\u517c\u5bb9\u6027\u8db3\u591f\u4e86\uff0c\u53ef\u4ee5\u73a9\u8d77\u6765\u5566\uff1a<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"\/\/image.zhangxinxu.com\/image\/blog\/201909\/2019-09-29_213301.png\" width=\"494\" height=\"251\" alt=\"\u6211Chrome\u6d4f\u89c8\u5668\u7248\u672c\" class=\"alignnone size-medium\" \/><\/p>\n<h3>\u4e8c\u3001\u61d2\u52a0\u8f7d\u7279\u6027\u7684\u7814\u7a76<\/h3>\n<p>\u5982\u679c\u4f60\u60f3\u8981\u8ba9\u4e00\u5f20\u56fe\u7247\u5728\u9760\u8fd1\u6d4f\u89c8\u5668\u89c6\u7a97\u7684\u65f6\u5019\u624d\u52a0\u8f7d\uff0c\u4e0b\u9762\u4ee3\u7801\u5c31\u53ef\u4ee5\u4e86\uff1a<\/p>\n<pre>&lt;img src=\".\/example.jpg\" <span style=\"color:#cd0000;\">loading=\"lazy\"<\/span> alt=\"zhangxinxu\"&gt;<\/pre>\n<p>\u65e0\u9700\u4efb\u4f55\u5176\u4ed6\u7684JavaScript\u4ee3\u7801\u5c31\u53ef\u4ee5\u5b9e\u73b0\u61d2\u52a0\u8f7d\u529f\u80fd\u3002<\/p>\n<p>\u773c\u89c1\u4e3a\u5b9e\uff0c\u60a8\u53ef\u4ee5\u72e0\u72e0\u5730\u70b9\u51fb\u8fd9\u91cc\uff1a<a href=\"https:\/\/www.zhangxinxu.com\/study\/201909\/img-lazy-loading-demo.html\" rel=\"noopener\" target=\"_blank\">\u56fe\u7247\u539f\u751f\u61d2\u52a0\u8f7dlazy loading\u6f14\u793ademo<\/a><\/p>\n<p>demo\u9875\u9762\u5171\u670930\u5f20 180*180\u7684\u5782\u76f4\u5c55\u793a\u7684\u56fe\u7247\u3002<\/p>\n<p>\u6211\u4eec\u6253\u5f00\u63a7\u5236\u53f0\uff0c\u5237\u65b0\u9875\u9762\uff0c\u53ef\u4ee5\u770b\u5230\u52a0\u8f7d\u4e86\u670917\u5f20\u56fe\u7247\uff0c\u5269\u4e0b\u768413\u5f20\u6ca1\u6709\u52a0\u8f7d\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"\/\/image.zhangxinxu.com\/image\/blog\/201909\/2019-09-29_221742.png\" width=\"547\" height=\"564\" alt=\"\u52a0\u8f7d\u4e8617\u4e2a\u8d44\u6e90\" class=\"alignnone size-medium\" \/><\/p>\n<p>\u5c45\u7136\u52a0\u8f7d\u4e8617\u5f20\u56fe\u7247\uff0c\u8fd9\u4e2a\u6570\u91cf\u8fdc\u8fdc\u8d85\u51fa\u4e86\u81ea\u5df1\u7684\u9884\u4f30\uff01<img loading=\"lazy\" decoding=\"async\" src=\"\/\/image.zhangxinxu.com\/image\/emtion\/emoji\/1f632.svg\" width=\"20\" height=\"20\" align=\"absmiddle\"><\/p>\n<p>\u56e0\u4e3a\u6309\u7167demo\u9875\u9762\u73b0\u5728\u7684\u5e03\u5c40\uff0c\u4e00\u5c4f\u53ea\u80fd\u663e\u793a4\u5f20\u56fe\u7247\uff0c\u73b0\u5728\u4e00\u53e3\u6c14\u52a0\u8f7d\u4e8617\u5f20\uff0c\u96be\u9053\u662f\u52a0\u8f7d4\u5c4f\u9ad8\u5ea6\u4ee5\u5185\u7684\u56fe\u7247\u7d20\u6750\uff1f<\/p>\n<p>\u4e8e\u662f\u6211<span class=\"hidden\" hidden>\u5f20\u946b\u65ed<\/span>\u628a\u9875\u9762\u5c4f\u5e55\u663e\u793a\u9ad8\u5ea6\u53d8\u5c0f\uff0c\u518d\u5237\u65b0\u770b\u770b\uff0c\u7ed3\u679c\u5982\u4e0b\uff1a<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"\/\/image.zhangxinxu.com\/image\/blog\/201909\/2019-09-29_222745.png\" width=\"487\" height=\"688\" alt=\"\u52a0\u8f7d\u4e8613\u5f20\u56fe\u7247\u7d20\u6750\" class=\"alignnone size-medium\" \/><\/p>\n<p>\u53ef\u89c1\u52a0\u8f7d\u7d20\u6750\u7684\u4e2a\u6570\u786e\u5b9e\u548c\u5c4f\u5e55\u7684\u53ef\u7528\u9ad8\u5ea6\u6709\u5173\uff0c\u4f46\u662f\u5e76\u4e0d\u662f\u6309\u7167\u5c4f\u5e55\u9ad8\u5ea6\u6bd4\u4f8b\u6765\u8ba1\u7b97\u7684\uff0c\u8ff7\u832b\u4e86\uff0c\u6ca1\u529e\u6cd5\uff0c\u770b\u770b<a href=\"https:\/\/whatpr.org\/html\/3752\/urls-and-fetching.html#lazy-loading-attribute\" rel=\"noopener nofollow\" target=\"_blank\">\u89c4\u8303\u6587\u6863<\/a>\uff0c\u91cc\u9762\u662f\u600e\u4e48\u8bf4\u7684\uff0c\u7136\u800c\u5e76\u6ca1\u6709\u627e\u5230\u76f8\u5173\u4fe1\u606f\uff0c\u5f88\u53ef\u80fd\u52a0\u8f7d\u7684\u7ec6\u8282\u662f\u6d4f\u89c8\u5668\u5382\u5546\u81ea\u5df1\u51b3\u5b9a\u7684\u3002<\/p>\n<p>\u56e0\u6b64\uff0c\u53ef\u4ee5\u5f97\u5230\u7b2c\u4e00\u4e2a\u7ed3\u8bba\uff1a<\/p>\n<p><strong style=\"padding: 5px;outline:2px dotted yellowgreen;\">1. Lazy loading\u52a0\u8f7d\u6570\u91cf\u4e0e\u5c4f\u5e55\u9ad8\u5ea6\u6709\u5173\uff0c\u9ad8\u5ea6\u8d8a\u5c0f\u52a0\u8f7d\u6570\u91cf\u8d8a\u5c11\uff0c\u4f46\u5e76\u4e0d\u662f\u7ebf\u6027\u5173\u7cfb\u3002<\/strong><\/p>\n<h4>\u7591\u95ee\uff1a\u4e0e\u7f51\u901f\u6709\u5173\uff1f<\/h4>\n<p>\u96be\u9053\u4e0e\u7f51\u901f\u6709\u5173\uff0c\u4e3a\u4e86\u5feb\u901f\u6eda\u52a8\u6eda\u52a8\u6761\u7684\u65f6\u5019\u6709\u66f4\u597d\u7684\u4f53\u9a8c\uff0c\u800c\u4e0d\u4f1a\u51fa\u73b0\u7a7a\u767d\u5360\u4f4d\uff1f<\/p>\n<p>\u6211\u53c8\u628a\u7f51\u901f\u8c03\u6162\u6d4b\u8bd5\u4e86\u4e0b\uff0c\u7ed3\u679c\u53d1\u73b0\u679c\u7136\u6709\u5173\uff01<img loading=\"lazy\" decoding=\"async\" src=\"\/\/image.zhangxinxu.com\/image\/emtion\/emoji\/1f632.svg\" width=\"20\" height=\"20\" align=\"absmiddle\"><\/p>\n<p>\u5728\u6162\u7f51\u901f\u73af\u5883\u4e0b\uff0c\u52a0\u8f7d\u56fe\u7247\u6570\u91cf\u66f4\u591a\u4e86\uff0c\u4e3a\u4e86\u907f\u514d\u8bef\u5dee\uff0c\u6211\u53cd\u590d\u5bf9\u6bd4\u6d4b\u8bd5\uff0c\u8bc1\u5b9e\u4e86\u8fd9\u4e2a\u7ed3\u8bba\uff1a<\/p>\n<p><strong style=\"padding: 5px;outline:2px dotted yellowgreen;\">2. Lazy loading\u52a0\u8f7d\u6570\u91cf\u4e0e\u7f51\u901f\u6709\u5173\uff0c\u7f51\u901f\u8d8a\u6162\uff0c\u52a0\u8f7d\u6570\u91cf\u8d8a\u591a\uff0c\u4f46\u5e76\u4e0d\u662f\u7ebf\u6027\u5173\u7cfb\u3002<\/strong><\/p>\n<p>\u4f8b\u5982\uff0c\u5728\u9ed8\u8ba4\u72b6\u6001\u4e0b\uff0c\u56fe\u7247\u52a0\u8f7d\u6570\u91cf\u7a33\u5b9a\u572816\u4e2a\uff0c\u4f46\u662f\uff0c\u5982\u679c\u662fFast 3G\uff0c\u5219\u52a0\u8f7d\u6570\u91cf\u7a33\u5b9a\u572821\u4e2a\uff1a<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"\/\/image.zhangxinxu.com\/image\/blog\/201909\/2019-09-29_225004.png\" width=\"497\" height=\"351\" alt=\"21\u4e2a\u56fe\u7247\u8d44\u6e90\u52a0\u8f7d\" class=\"alignnone size-medium\" \/><\/p>\n<p>\u5982\u679c\u662fSlow 3G\uff0c\u5219\u52a0\u8f7d\u6570\u91cf\u7a33\u5b9a\u572829\u4e2a\uff1a<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"\/\/image.zhangxinxu.com\/image\/blog\/201909\/2019-09-29_225241.png\" width=\"571\" height=\"315\" alt=\"Slow 3G\u52a0\u8f7d29\u4e2a\u56fe\u7247\u8d44\u6e90\" class=\"alignnone size-medium\" \/><\/p>\n<h4>\u7528\u6237\u884c\u4e3a\u4e0e\u52a0\u8f7d\u6d4b\u8bd5<\/h4>\n<p>\u5f53\u6211\u4eec\u6eda\u52a8\u5c4f\u5e55\u7684\u65f6\u5019\uff0c\u6ca1\u6709\u52a0\u8f7d\u7684\u56fe\u7247\u5f00\u59cb\u6309\u7167\u4f4d\u7f6e\u4f9d\u6b21\u52a0\u8f7d\u4e86\uff0c\u6ca1\u6709\u4efb\u4f55\u7b49\u5f85\u548c\u7f13\u51b2\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"\/\/image.zhangxinxu.com\/image\/blog\/201909\/2019-09-29_230106.png\" width=\"645\" height=\"219\" alt=\"\u6eda\u52a8\u540e\u52a0\u8f7d\u7684\u7d20\u6750\" class=\"alignnone size-medium\" \/><\/p>\n<p>\u56e0\u6b64\u53ef\u4ee5\u5f97\u5230\u7ed3\u8bba\uff1a<\/p>\n<p><strong style=\"padding: 5px;outline:2px dotted yellowgreen;\">3. \u6eda\u52a8\u5373\u4f1a\u89e6\u53d1\u56fe\u7247\u61d2\u52a0\u8f7d\uff0c\u4e0d\u4f1a\u8bf4\u6eda\u52a8\u4e00\u5c4f\u540e\u518d\u53bb\u52a0\u8f7d\u3002<\/strong><\/p>\n<p>\u53e6\u5916\uff0c\u6d4b\u8bd5\u7684\u65f6\u5019\u8fd8\u53d1\u73b0\u53e6\u5916\u4e00\u4e2a\u73b0\u8c61\uff1a<\/p>\n<p><strong style=\"padding: 5px;outline:2px dotted yellowgreen;\">4. \u7a97\u53e3resize\u5c3a\u5bf8\u53d8\u5316\u4e5f\u4f1a\u89e6\u53d1\u56fe\u7247\u61d2\u52a0\u8f7d\uff0c\u5f53\u5c4f\u5e55\u9ad8\u5ea6\u4ece\u5c0f\u53d8\u5927\u7684\u65f6\u5019\u3002<\/strong><\/p>\n<p>\u4f8b\u5982\u6211\u4eec\u628a\u63a7\u5236\u53f0\u4ece\u4e0b\u65b9\u53d8\u5230\u53f3\u4fa7\uff0c\u6b64\u65f6\u6d4f\u89c8\u5668\u9ad8\u5ea6\u56fe\u7247\u53d8\u9ad8\uff0c\u4f1a\u53d1\u73b0\u53c8\u591a\u4e86\u51e0\u4e2a\u56fe\u7247\u8bf7\u6c42\uff1a<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"\/\/image.zhangxinxu.com\/image\/blog\/201909\/2019-09-29_230535.png\" width=\"264\" height=\"340\" alt=\"\u6d4f\u89c8\u5668\u63a7\u5236\u53f0\u4f4d\u7f6e\u53d8\u5316\" class=\"alignnone size-medium\" \/><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"\/\/image.zhangxinxu.com\/image\/blog\/201909\/2019-09-29_230700.png\" width=\"615\" height=\"217\" alt=\"resize\u4e0e\u6eda\u52a8\u8bf7\u6c42\" class=\"alignnone size-medium\" \/><\/p>\n<h4>\u6eda\u52a8\u8bb0\u5fc6\u4e0e\u8df3\u8fc7\u52a0\u8f7d\uff1f<\/h4>\n<p>\u6d4f\u89c8\u5668\u666e\u901a\u5237\u65b0\u7684\u65f6\u5019\u4f1a\u8bb0\u4f4f\u4e0a\u4e00\u6b21\u6eda\u52a8\u7684\u4f4d\u7f6e\uff0c\u5982\u679c\u9875\u9762\u4e4b\u524d\u6eda\u52a8\u4f4d\u7f6e\u662f\u5728\u5f88\u4e0b\u9762\uff0c\u8fd9\u4e2a\u65f6\u5019\u9875\u9762\u5237\u65b0\uff0c\u90a3\u4e48\u5f00\u5934\u90a3\u4e9b\u56fe\u7247\u4f1a\u4e0d\u4f1a\u4e0d\u52a0\u8f7d\u5462\uff1f<\/p>\n<p>\u6211\u6d4b\u8bd5\u4e86\u4e0b\uff0c\u7ed3\u679c\u679c\u4e0d\u5176\u7136\uff0c\u5f00\u59cb\u7684\u56fe\u7247\u5e76\u4e0d\u4f1a\u52a0\u8f7d\uff0c\u53ea\u4f1a\u52a0\u8f7d\u4e0b\u9762\u7684\u56fe\u7247\uff0c\u770b\u6765\u6d4f\u89c8\u5668\u8fd8\u662f\u633a\u806a\u660e\u7684\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"\/\/image.zhangxinxu.com\/image\/blog\/201909\/2019-09-29_231252.png\" width=\"393\" height=\"516\" alt=\"\u6eda\u52a8\u5237\u65b0\u52a0\u8f7d\u540e\u9762\u7684\u56fe\u7247\" class=\"alignnone size-medium\" \/><\/p>\n<p><strong style=\"padding: 5px;outline:2px dotted yellowgreen;\">5. \u6839\u636e\u6eda\u52a8\u4f4d\u7f6e\u4e0d\u540c\uff0cLazy loading\u4f1a\u5ffd\u7565\u5934\u5c3e\u7684\u56fe\u7247\u8bf7\u6c42\u3002<\/strong><\/p>\n<h3>\u4e09\u3001\u8bed\u6cd5\u548c\u53c2\u6570<\/h3>\n<p>HTML <code>loading<\/code>\u5c5e\u6027\u652f\u6301\u7684\u503c\u9664\u4e86<code>'lazy'<\/code>\u8fd8\u6709\u4e0b\u9762\u8fd9\u51e0\u4e2a\uff1a<\/p>\n<dl>\n<dt><strong>lazy<\/strong><\/dt>\n<dd>\u56fe\u7247\u6216\u6846\u67b6\u61d2\u52a0\u8f7d\uff0c\u4e5f\u5c31\u662f\u5143\u7d20\u8d44\u6e90\u5feb\u8981\u88ab\u770b\u5230\u7684\u65f6\u5019\u52a0\u8f7d\u3002<\/dd>\n<dt><strong>eager<\/strong><\/dt>\n<dd>\u56fe\u7247\u6216\u6846\u67b6\u65e0\u89c6\u4e00\u5207\u8fdb\u884c\u52a0\u8f7d\u3002<\/dd>\n<dt><strong>auto<\/strong><\/dt>\n<dd>\u9ed8\u8ba4\u503c\u3002\u56fe\u7247\u6216\u6846\u67b6\u57fa\u4e8e\u6d4f\u89c8\u5668\u81ea\u5df1\u7684\u7b56\u7565\u8fdb\u884c\u52a0\u8f7d\u3002<\/dd>\n<\/dl>\n<p>\u5982\u679cHTMLImageElement\u6216\u8005HTMLIFrameElement\u5143\u7d20\u6ca1\u6709\u663e\u5f0f\u5730\u8bbe\u7f6e<code>loading<\/code>\u5c5e\u6027\u6216\u8005<code>loading<\/code>\u5c5e\u6027\u7684\u503c\u4e0d\u5408\u6cd5\uff0c\u5219\u90fd\u88ab\u5f53\u4f5c<code>'auto'<\/code>\u5904\u7406\u3002<\/p>\n<h3>\u56db\u3001\u4e0eJS\u6709\u5173\u7684\u5b9e\u8df5\u6307\u5357<\/h3>\n<h4>1. \u5982\u4f55\u5224\u65ad\u5f53\u524d\u6d4f\u89c8\u5668\u662f\u5426\u652f\u6301loading=&#8221;lazy&#8221;\uff1f<\/h4>\n<p>\u4e0b\u9762\u4e09\u79cd\u65b9\u6cd5\u90fd\u53ef\u4ee5\uff1a<\/p>\n<pre>var isSupportLoading = 'loading' in document.createElement('img');<\/pre>\n<p>\u6216\u8005\uff1a<\/p>\n<pre>var isSupportLoading = 'loading' in new Image();<\/pre>\n<p>\u6216\u8005\uff1a<\/p>\n<pre>var isSupportLoading = 'loading' in HTMLImageElement.prototype;<\/pre>\n<h4>2. \u5982\u4f55\u83b7\u53d6loading\u5c5e\u6027\u503c<\/h4>\n<p>\u5047\u8bbe<code>&lt;img&gt;<\/code>\u5143\u7d20\u7684DOM\u53d8\u91cf\u540d\u662f<code>img<\/code>\uff0c\u5219\u8be5\u56fe\u7247\u5143\u7d20\u7684<code>loading<\/code>\u5c5e\u6027\u503c\u76f4\u63a5\u4e0b\u9762\u8bed\u6cd5\u5c31\u53ef\u4ee5\u83b7\u53d6\u4e86\uff1a<\/p>\n<pre>var attrLoading = img.loading;<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"\/\/image.zhangxinxu.com\/image\/blog\/201909\/2019-09-29_232826.png\" width=\"338\" height=\"118\" alt=\"loading\u5c5e\u6027\u503c\u83b7\u53d6\u7ed3\u679c\" class=\"alignnone size-medium\" \/><\/p>\n<p>\u5982\u679c\u6d4f\u89c8\u5668\u5e76\u4e0d\u652f\u6301\u539f\u751f\u7684<code>loading<\/code>\u61d2\u52a0\u8f7d\uff0c\u5219\u4f1a\u8fd4\u56de<code>undefined<\/code>\uff0c\u4f8b\u5982\u5728Firefox\u6d4f\u89c8\u5668\u4e0b\uff1a<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"\/\/image.zhangxinxu.com\/image\/blog\/201909\/2019-09-29_233006.png\" width=\"331\" height=\"173\" alt=\"loading\u5c5e\u6027\u65e0\u6cd5\u83b7\u53d6\" class=\"alignnone size-medium\" \/><\/p>\n<h4>3. loading\u81ea\u5b9a\u4e49\u6269\u5c55\u5e76\u4e0d\u4f1a\u8986\u76d6<\/h4>\n<p>\u4f8b\u5982\uff1a<\/p>\n<pre>HTMLElement.prototype.loading = function () {};\r\nimg = document.querySelector('[loading]');\r\nimg.loading;<\/pre>\n<p>\u6211\u4eec\u5728HTMLElement\u539f\u578b\u4e0a\u6269\u5c55\u4e00\u4e2a\u540d\u4e3a<code>loading<\/code>\u7684\u51fd\u6570\uff0c\u5e76\u4e0d\u4f1a\u5f71\u54cd\u56fe\u7247\u5143\u7d20\u539f\u751f\u7684<code>loading<\/code>\u5c5e\u6027\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"\/\/image.zhangxinxu.com\/image\/blog\/201909\/2019-09-29_233541.png\" width=\"371\" height=\"116\" alt=\"Chrome\u4e0b\u56fe\u7247\u7684loading\u5c5e\u6027\u8fd8\u5728\" class=\"alignnone size-medium\" \/><\/p>\n<p>\u4f46\u662f\uff0c\u5bf9\u4e8e\u4e0d\u652f\u6301\u7684\u6d4f\u89c8\u5668\uff0c\u4f8b\u5982Firefox\uff0c\u5219\u660e\u663e\u53d7\u5230\u4e86\u5f71\u54cd\uff1a<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"\/\/image.zhangxinxu.com\/image\/blog\/201909\/2019-09-29_233611.png\" width=\"385\" height=\"119\" alt=\"Firefox\u6d4f\u89c8\u5668\u4e0b\u7684\u56fe\u7247loading\" class=\"alignnone size-medium\" \/><\/p>\n<h4>4. \u5982\u4f55\u517c\u5bb9\u4e0d\u652f\u6301\u7684\u6d4f\u89c8\u5668\uff1fPolyfill\uff1f<\/h4>\n<p>GitHub\u4e0a\u6709\u4e2a\u9879\u76ee\u7528\u6765\u517c\u5bb9\u539f\u751f\u7684\u61d2\u52a0\u8f7dloading\u5c5e\u6027\u7684\uff0c\u9879\u76ee\u5730\u5740\uff1a<a href=\"https:\/\/github.com\/mfranzke\/loading-attribute-polyfill\" rel=\"noopener nofollow\" target=\"_blank\">https:\/\/github.com\/mfranzke\/loading-attribute-polyfill<\/a><\/p>\n<p>\u6211\u770b\u4e86\u4e0b\u539f\u7406\uff0c\u662f\u9700\u8981\u628a\u56fe\u7247HTML\u4ee3\u7801\u653e\u5728<code>&lt;noscript&gt;<\/code>\u6807\u7b7e\u4e2d\uff1a<\/p>\n<pre>&lt;noscript class=\"loading-lazy\"&gt;\r\n    &lt;img\r\n        src=\"example.jpg\"\r\n        loading=\"lazy\"\r\n        alt=\"zhangxinxu\"\r\n        width=\"250\"\r\n        height=\"150\"\r\n    \/&gt;\r\n&lt;\/noscript&gt;<\/pre>\n<p>\u6211\u5934\u4e0a\u7acb\u9a6c\u51fa\u73b0\u4e86\u4e00\u8fde\u4e32\u7684\u95ee\u53f7\uff0c\u6055\u6211\u611a\u949d\uff0c\u8fd9\u662f\u4e2a\u9524\u5b50\u6548\u679c\u7684polyfill\uff0c\u65e0JavaScript\u7279\u6027\u4e0b\u8868\u73b0\u786e\u5b9e\u7b26\u5408\u89c4\u8303\uff0c\u4f46\u662f\u6709JavaScript\u7684\u65f6\u5019\uff0c\u4f60\u8fd9\u4e32\u4ee3\u7801\u57fa\u672c\u4e0a\u5c31\u6ca1\u6709\u5b9e\u7528\u4ef7\u503c\uff0c\u56e0\u4e3a\u56fe\u7247\u7d20\u6750\u672c\u8eab\u662f\u5360\u636e\u5c3a\u5bf8\u5927\u5c0f\u7684\uff0c\u4e0e\u5e03\u5c40\u5bc6\u5207\u76f8\u5173\u7684\uff0c\u653e\u5728<code>&lt;noscript&gt;<\/code>\u6807\u7b7e\u4e2d\uff0c\u5360\u636e\u5bbd\u9ad8\u5c3a\u5bf8\u90fd\u662f0\uff0c\u4f53\u9a8c\u4e00\u56e2\u7cdf\uff0c\u5b8c\u5168\u4e0d\u80fd\u63a5\u53d7\u3002<\/p>\n<p>\u552f\u4e00\u53ef\u884c\u65b9\u6848\u662fHTML\u4ee3\u7801\u8f93\u51fa\u7684\u65f6\u5019\uff0c\u6839\u636e\u6d4f\u89c8\u5668\uff08\u6700\u597dJS\u5bf9\u8c61\u5224\u65ad\uff09\u8f93\u51fa\u4e0d\u540c\u7684HTML\u4ee3\u7801\uff0c\u4f8b\u5982\u652f\u6301native loading\u7684HTML\u8fd9\u6837\uff1a<\/p>\n<pre>&lt;img src=\"example.jpg\" loading=\"lazy\" alt=\"zhangxinxu\" width=\"250\" height=\"150\"&gt;<\/pre>\n<p>\u4e0d\u652f\u6301\u7684\u8fd9\u6837\uff1a<\/p>\n<pre>&lt;img data-src=\"example.jpg\" loading=\"lazy\" alt=\"zhangxinxu\" width=\"250\" height=\"150\"&gt;<\/pre>\n<p>\u7136\u540e\u518d\u4f7f\u7528\u4f20\u7edf\u7684JavaScript\u61d2\u52a0\u8f7d\u6eda\u52a8\u52a0\u8f7d\u5b9e\u73b0\u5373\u53ef\u3002<\/p>\n<h3>\u4e94\u3001\u7814\u7a76\u4e0e\u5b9e\u8df5\u7ed3\u679c\u603b\u7ed3<\/h3>\n<p>\u6700\u540e\uff0c\u603b\u7ed3\u4e0b\uff0c\u539f\u751f\u61d2\u52a0\u8f7d\u76845\u4e2a\u884c\u4e3a\u7279\u6027\uff1a<\/p>\n<ol>\n<li>Lazy loading\u52a0\u8f7d\u6570\u91cf\u4e0e\u5c4f\u5e55\u9ad8\u5ea6\u6709\u5173\uff0c\u9ad8\u5ea6\u8d8a\u5c0f\u52a0\u8f7d\u6570\u91cf\u8d8a\u5c11\uff0c\u4f46\u5e76\u4e0d\u662f\u7ebf\u6027\u5173\u7cfb\u3002<\/li>\n<li>Lazy loading\u52a0\u8f7d\u6570\u91cf\u4e0e\u7f51\u901f\u6709\u5173\uff0c\u7f51\u901f\u8d8a\u6162\uff0c\u52a0\u8f7d\u6570\u91cf\u8d8a\u591a\uff0c\u4f46\u5e76\u4e0d\u662f\u7ebf\u6027\u5173\u7cfb\u3002<\/li>\n<li>Lazy loading\u52a0\u8f7d\u6ca1\u6709\u7f13\u51b2\uff0c\u6eda\u52a8\u5373\u4f1a\u89e6\u53d1\u65b0\u7684\u56fe\u7247\u8d44\u6e90\u52a0\u8f7d\u3002<\/li>\n<li>Lazy loading\u52a0\u8f7d\u5728\u7a97\u53e3resize\u5c3a\u5bf8\u53d8\u5316\u65f6\u5019\u4e5f\u4f1a\u89e6\u53d1\uff0c\u4f8b\u5982\u5c4f\u5e55\u9ad8\u5ea6\u4ece\u5c0f\u53d8\u5927\u7684\u65f6\u5019\u3002<\/li>\n<li>Lazy loading\u52a0\u8f7d\u4e5f\u6709\u53ef\u80fd\u4f1a\u5148\u52a0\u8f7d\u540e\u9762\u7684\u56fe\u7247\u8d44\u6e90\uff0c\u4f8b\u5982\u9875\u9762\u52a0\u8f7d\u65f6\u6eda\u52a8\u9ad8\u5ea6\u5f88\u9ad8\u7684\u65f6\u5019\u3002<\/li>\n<\/ol>\n<p>\u4e0eJavaScript\u6709\u5173\u7684\u51e0\u4e2a\u884c\u4e3a\u7279\u5f81\uff1a<\/p>\n<ol>\n<li>\u5224\u65ad\u6d4f\u89c8\u5668\u662f\u5426\u652f\u6301\u539f\u751floading\uff0c\u6700\u597d\u4f7f\u7528<code>'loading' in XXX<\/code>\u5224\u65ad\u3002<\/li>\n<li>\u83b7\u53d6loading\u5c5e\u6027\u503c\u53ef\u4ee5\u76f4\u63a5img.loading\uff1b<\/li>\n<li>\u539f\u751floading\u4e0d\u53ef\u5199\uff0c\u4f8b\u5982HTMLImageElement.prototype.loading\u4f1a\u62a5\u9519Illegal invocation\u3002<\/li>\n<li>Polyfill\u5c31\u662f\u573a\u68a6\uff0c\u53ea\u80fd\u7b49\u6d4f\u89c8\u5668\u652f\u6301\u3002<\/li>\n<\/ol>\n<p>\u4ee5\u4e0a~<\/p>\n<p>\u8fd8\u67091\u5206\u949f24:00\uff0c\u563f\u563f\u563f\u3002<\/p>\n<p>\u8282\u524d\u6700\u540e\u4e00\u5929\uff0c\u8d76\u4e00\u7bc7\u8fd8\u7b97\u524d\u6cbf\u7684\u7814\u7a76\u5c0f\u6587\uff0c\u4ee5\u6b64\u6765\u795d\u613f\u5927\u5bb6\u56fd\u5e86\u8282\u8fc7\u5f97\u5f00\u5fc3\u5feb\u4e50\uff0c\u5feb\u4e50\u5f00\u5fc3\u3002<\/p>\n<p>\u5bf9\u4e86\uff0c\u6211\u7684\u65b0\u4e66\u300aCSS\u9009\u62e9\u5668\u4e16\u754c\u300b\u4e0a\u5e02\u4e86\uff0c\u56fd\u5e86\u56de\u6765\u8bb0\u5f97\u4e70\u4e00\u672c\u652f\u6301\u4e0b\uff0c\u6069\u2026\u2026\u4e0d\u5bf9\uff0c\u4e70\u4e00\u672c\u8ba9\u81ea\u5df1CSS\u6280\u672f\u63d0\u9ad8\u4e0b\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"\/\/image.zhangxinxu.com\/image\/emtion\/emoji\/1f913.svg\" width=\"20\" height=\"20\" align=\"absmiddle\"><\/p>\n<p>\u672c\u6587\u4e3a\u539f\u521b\u6587\u7ae0\uff0c\u6b22\u8fce\u5206\u4eab\uff0c\u52ff\u5168\u6587\u8f6c\u8f7d\uff0c\u5982\u679c\u5185\u5bb9\u4f60\u5b9e\u5728\u559c\u6b22\uff0c\u53ef\u4ee5\u52a0\u5165\u6536\u85cf\u5939\uff0c\u6c38\u4e0d\u8fc7\u671f\uff0c\u800c\u4e14\u8fd8\u4f1a\u53ca\u65f6\u66f4\u65b0\u77e5\u8bc6\u70b9\u4ee5\u53ca\u4fee\u6b63\u9519\u8bef\uff0c\u9605\u8bfb\u4f53\u9a8c\u4e5f\u66f4\u597d\u3002<br \/>\n\u672c\u6587\u5730\u5740\uff1a<a href=\"https:\/\/www.zhangxinxu.com\/wordpress\/?p=8981\">https:\/\/www.zhangxinxu.com\/wordpress\/?p=8981<\/a><\/p>\n<p>\uff08\u672c\u7bc7\u5b8c\uff09<\/p>\n","protected":false},"excerpt":{"rendered":"<p><img loading=\"lazy\" decoding=\"async\" src=\"\/\/image.zhangxinxu.com\/image\/blog\/201909\/old-tape-loading.png\" width=\"160\" height=\"111\" alt=\"\u539f\u751f\u61d2\u52a0\u8f7d\u5934\u56fe\u5c01\u9762\u56fe\" class=\"alignright size-medium\" \/><\/p>\n<p>\u672c\u6587\u4ecb\u7ecd\u81ea\u5df1\u5728\u7814\u7a76\u4e0e\u5b9e\u8df5Lazy loading\u539f\u751f\u61d2\u52a0\u8f7d\u5f97\u5230\u7684\u7ed3\u8bba\u4e0e\u77e5\u8bc6\uff0cscroll\u6eda\u52a8\uff0cresize\u7f29\u653e\uff0c\u7f51\u901f\uff0c\u5c4f\u5e55\u9ad8\u5ea6\u90fd\u662f\u5f71\u54cd\u7279\u6027\uff0c\u540c\u65f6\u4e5f\u4f1a\u4ecb\u7ecd\u5728JavaScript\u4e2d\u7684\u4e00\u4e9b\u5b9e\u7528tips\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1097,180],"tags":[1499,9,532,1498,1008,355,75,529,1066],"_links":{"self":[{"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/posts\/8981"}],"collection":[{"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/comments?post=8981"}],"version-history":[{"count":0,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/posts\/8981\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/media?parent=8981"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/categories?post=8981"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/tags?post=8981"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}