{"id":12082,"date":"2026-02-25T11:50:41","date_gmt":"2026-02-25T03:50:41","guid":{"rendered":"https:\/\/www.zhangxinxu.com\/wordpress\/?p=12082"},"modified":"2026-02-27T10:29:27","modified_gmt":"2026-02-27T02:29:27","slug":"image-preview-best-practice","status":"publish","type":"post","link":"https:\/\/www.zhangxinxu.com\/wordpress\/2026\/02\/image-preview-best-practice\/","title":{"rendered":"\u70b9\u51fb\u56fe\u7247\u653e\u5927\u67e5\u770b\u4ea4\u4e92\u6548\u679c\u7684\u6700\u4f73\u5b9e\u73b0"},"content":{"rendered":"<p>by <a href=\"https:\/\/www.zhangxinxu.com\/\">zhangxinxu<\/a> from <a href=\"https:\/\/www.zhangxinxu.com\/wordpress\/?p=12082\">https:\/\/www.zhangxinxu.com\/wordpress\/?p=12082<\/a><br \/>\n\u672c\u6587\u53ef\u5168\u6587\u8f6c\u8f7d\uff0c\u4f46\u9700\u8981\u4fdd\u7559\u539f\u4f5c\u8005\u3001\u51fa\u5904\u4ee5\u53ca\u6587\u4e2d\u94fe\u63a5\uff0cAI\u6293\u53d6\u4fdd\u7559\u539f\u6587\u5730\u5740\uff0c\u4efb\u4f55\u7f51\u7ad9\u5747\u53ef\u6458\u8981\u805a\u5408\uff0c\u5546\u7528\u8bf7\u8054\u7cfb\u6388\u6743\u3002<\/p>\n<h3>\u4e00\u3001\u5148\u770b\u8ddf\u968f\u653e\u5927\u6548\u679c<\/h3>\n<p>\u8bf7\u770b\u4e0b\u9762\u7684MP4\u5f55\u5c4f\u6548\u679c\uff08\u4e0d\u52a8\u70b9\u51fb\u64ad\u653e\uff09\uff1a<\/p>\n<p><video src='\/\/image.zhangxinxu.com\/video\/blog\/202602\/image-preview.mp4' type=\"video\/mp4\" width=\"672\" height=\"382\" preload=\"auto\" muted autoplay autobuffer loop x-webkit-airplay=\"allow\" x5-video-player-type=\"h5\" x5-video-player-fullscreen=\"true\" x5-video-orientation=\"portraint\" webkit-playsinline=\"true\" playsinline=\"true\" \u200b\u200b\u200b\u200bposter=\"\/\/image.zhangxinxu.com\/image\/blog\/201806\/poster.png\"><\/video><\/p>\n<p>\u9664\u4e86\u89c6\u9891\u770b\u5230\u7684\u6548\u679c\uff0c\u76f8\u5173\u5b9e\u73b0\u8fd8\u652f\u6301\uff1a<\/p>\n<ol>\n<li>ESC\u5173\u95ed\uff1b<\/li>\n<li>\u5730\u5740\u680f\u56de\u9000\u5173\u95ed\uff1b<\/li>\n<\/ol>\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\/202602\/js-image-fullscreen-demo.php\" rel=\"noopener\" target=\"_blank\">\u70b9\u51fb\u7f29\u7565\u56fe\u4ee5\u52a8\u753b\u6548\u679c\u5448\u73b0\u5927\u56fedemo<\/a><\/p>\n<h3>\u4e8c\u3001\u4e00\u6b65\u6b65\u539f\u7406\u8bf4\u660e<\/h3>\n<h4>1. \u8ddf\u968f\u653e\u5927\u6548\u679c\u662f\u5982\u4f55\u5b9e\u73b0\u7684\uff1f<\/h4>\n<p>\u8fd9\u4e2a\u4f7f\u7528\u7684\u662fstartViewTransition\u5b9e\u73b0\u7684\uff0c\u8fd9\u4e2a\u662f\u9875\u9762\u7ea7\u522b\u7684transition\u8fc7\u6e21\u6548\u679cAPI\u7684\u8bed\u6cd5\u4e4b\u4e00\uff0c\u975e\u5e38\u597d\u7528\u3002<\/p>\n<p>\u6211\u4eec\u53ef\u4ee5\u65e0\u9700\u5173\u6ce8\u52a8\u753b\u7ec6\u8282\uff0c\u53ea\u9700\u8981\u7b26\u5408\u524d\u540e\u9875\u9762\u7684\u5feb\u7167\uff0c\u6d4f\u89c8\u5668\u81ea\u52a8\u5c31\u4f1a\u8865\u5168\u5176\u4e2d\u7684\u52a8\u753b\u6548\u679c\uff0c\u6709\u70b9\u7c7b\u4f3c\u4e8ekeynote\u4e2d\u7684\u795e\u5947\u79fb\u52a8\u3002<\/p>\n<p>\u65e0\u8bba\u662f\u5220\u9664\u3001\u79fb\u52a8\u3001\u8fd8\u662f\u8fd9\u91cc\u7684\u653e\u5927\u6548\u679c\uff0c\u90fd\u4f1a\u6709\u5f88\u68d2\u7684\u6548\u679c\u3002<\/p>\n<p>\u8fd9\u4e2a\u6211\u5728\u4e4b\u524d\u8be6\u7ec6\u4ecb\u7ecd\u8fc7\uff0c\u53ef\u4ee5\u8bbf\u95ee\u8fd9\u91cc\uff1a\u201c<a href=\"https:\/\/www.zhangxinxu.com\/wordpress\/2024\/08\/view-transitions-api\/\">\u9875\u9762\u7ea7\u53ef\u89c6\u52a8\u753bView Transitions API\u521d\u4f53\u9a8c<\/a>\u201d<\/p>\n<p>\u6b64\u7279\u6027\u6211\u5df2\u7ecf\u5927\u91cf\u5728\u751f\u4ea7\u73af\u5883\u4f7f\u7528\u4e86\u3002<\/p>\n<p>\u5728\u672c\u6548\u679c\u4e2d\uff0c\u53ea\u9700\u8981\u5c06viewTransitionName\u5728\u5408\u9002\u7684\u65f6\u673a\u5728\u7f29\u7565\u56fe\u548c\u9884\u89c8\u56fe\u5143\u7d20\u4e0a\u8fdb\u884c\u8bbe\u7f6e\uff0c\u5c31\u4f1a\u81ea\u52a8\u6709\u76f8\u5173\u7684\u6548\u679c\u4e86\u3002<\/p>\n<pre>originImg.style.viewTransitionName = \"dialogImg\";\r\n\/\/ \u653e\u5927\u6267\u884c\u7684\u65f6\u5019\r\ndocument.startViewTransition(() =&gt; {\r\n  originImg.style.viewTransitionName = \"\";\r\n  cloneImg.style.viewTransitionName = \"dialogImg\";\r\n});<\/pre>\n<h4>2. \u4e3a\u4f55\u4f7f\u7528dialog\u5143\u7d20\uff1f<\/h4>\n<p>\u4f7f\u7528<code>&lt;dialog&gt;<\/code>\u5143\u7d20\u4e3b\u8981\u662f\u4e24\u4e2a\u539f\u56e0\uff1a<\/p>\n<ol>\n<li>\u9876\u5c42\u7279\u6027\uff1b<\/li>\n<li>\u65e0\u969c\u788d\u8bbf\u95ee\u5929\u7136\u652f\u6301\uff1b<\/li>\n<\/ol>\n<p>\u9876\u5c42\u7279\u6027\u53ef\u4ee5\u8ba9\u6211\u4eec\u65e0\u9700\u5173\u5fc3\u5c42\u7ea7\uff0c\u4fdd\u8bc1\u5927\u56fe\u6548\u679c\u6c38\u8fdc\u5728\u4e0a\u9762\uff0c\u9002\u7528\u573a\u666f\u66f4\u5e7f\u6cdb\u3002<\/p>\n<p><code>&lt;dialog&gt;<\/code>\u5143\u7d20\u5929\u7136\u805a\u7126\uff0c\u4e14\u652f\u6301ESC\u5173\u95ed\uff0c\u53ef\u4ee5\u8282\u7ea6\u5f00\u53d1\u6210\u672c\u3002<\/p>\n<h4>3. \u5730\u5740\u680f\u56de\u9000\u5982\u4f55\u5b9e\u73b0\uff1f<\/h4>\n<p>\u6bcf\u6b21\u5f39\u6846\u663e\u793a\uff0c\u6211\u4eec\u4f7f\u7528<code>history.pushState<\/code>\u6dfb\u52a0\u4e00\u6761\u5386\u53f2\u8bb0\u5f55\uff0c\u5f53\u53d1\u751f<code>popstate<\/code>\u53d8\u5316\u7684\u65f6\u5019\uff0c\u5224\u65ad\u5f53\u524d\u7684\u5f39\u6846\u72b6\u6001\uff0c\u5982\u679c\u5f39\u6846\u6b63\u5e38\u5c55\u793a\uff0c\u5219\u6267\u884c\u5173\u95ed\u64cd\u4f5c\u3002<\/p>\n<p>\u4e3a\u4e86\u4fdd\u8bc1\u5386\u53f2\u51c6\u786e\u56de\u9000\uff0c\u53ef\u4ee5\u5728<code>history.pushState<\/code>\u6267\u884c\u7684\u65f6\u5019\u4f20\u9012\u72b6\u6001\u5bf9\u8c61\uff0c\u5728\u5f39\u6846\u5173\u95ed\u4e4b\u540e\uff0c\u5bf9\u8be5\u72b6\u6001\u5bf9\u8c61\u8fdb\u884c\u5224\u5b9a\uff0c\u5982\u679c\u5339\u914d\uff0c\u5219\u6267\u884c<code>history.back()<\/code>\u3002<\/p>\n<p>\u5b8c\u6574\u7684\u4ea4\u4e92\u903b\u8f91\u53c2\u89c1\uff1a<\/p>\n<pre>\/\/ modal\u5c31\u662f\u5f39\u6846\u5143\u7d20\r\nconst handlePopState = () => {\r\n  if (modal.isConnected) {\r\n    modal.dispatchEvent(new Event(\"click\"));\r\n  }\r\n};\r\n\/\/ \u5f39\u6846\u663e\u793a\u7684\u65f6\u5019\r\n\/\/ \u589e\u52a0\u5386\u53f2\u8bb0\u5f55\r\nhistory.pushState({ modal: true }, '', location.href);\r\n\/\/ \u76d1\u542c\u5730\u5740\u680f\u53d8\u5316\r\nwindow.addEventListener(\"popstate\", handlePopState);\r\n\r\n\/\/ \u5f39\u6846\u5143\u7d20\u79fb\u9664\u7684\u65f6\u5019\r\n\/\/ \u79fb\u9664\u5730\u5740\u680f\u53d8\u5316\u76d1\u542c\r\nwindow.removeEventListener(\"popstate\", handlePopState);\r\n\/\/ \u5386\u53f2\u56de\u9000\r\nif (history.state && history.state.modal) {\r\n  history.back();\r\n}<\/pre>\n<h4>4. \u662f\u5426\u53ef\u4ee5\u8fdb\u884c\u5c01\u88c5\uff1f<\/h4>\n<p>\u81ea\u7136\u53ef\u4ee5\u3002<\/p>\n<p>\u73b0\u5728\u7684DOM\u80fd\u529b\u5df2\u7ecf\u5f88\u5f3a\u5927\u4e86\uff0c\u6211\u4eec\u65e0\u9700\u5173\u5fc3\u70b9\u51fb\u4e8b\u4ef6\u7b49\u884c\u4e3a\uff0c\u4e5f\u4e0d\u9700\u8981\u7528\u5230Web Components\u8fd9\u4e48\u91cd\u7684\u4e1c\u897f\uff0c\u53ea\u9700\u8981\u901a\u8fc7\u4e00\u4e2a\u7b80\u5355\u7684\u5c5e\u6027\uff0c\u5c31\u53ef\u4ee5\u8ba9\u5143\u7d20\u62e5\u6709\u70b9\u51fb\u67e5\u770b\u5927\u56fe\u7684\u6548\u679c\u4e86\u3002<\/p>\n<p>\u6211\u82b1\u4e86\u70b9\u65f6\u95f4\uff0c\u628a\u8fd9\u4e2a\u4ea4\u4e92\u6548\u679c\u5c01\u88c5\u5728\u4e86\u4e00\u4e2aJS\u4e2d\uff0c\u5927\u5bb6\u53ea\u9700\u8981\u5f15\u7528\u8fd9\u4e2aJS\u6587\u4ef6\uff0c\u65e0\u9700\u5176\u4ed6\u4efb\u4f55\u8bbe\u7f6e\uff0c\u5c31\u53ef\u4ee5\u6709\u5bf9\u5e94\u7684\u6548\u679c\u4e86\u3002<\/p>\n<h3>\u4e09\u3001\u4ea4\u4e92\u5c01\u88c5\u4e0egitee\u5f00\u6e90<\/h3>\n<p>\u5c0f\u73a9\u5177\u6211\u90fd\u662f\u653e\u5728gitee\u4e0a\u7684\uff1a<a href=\"https:\/\/gitee.com\/zhangxinxu\/image-preview\" rel=\"noopener\" target=\"_blank\">https:\/\/gitee.com\/zhangxinxu\/image-preview<\/a><\/p>\n<p>\u4f7f\u7528\u5f88\u65b9\u4fbf\uff1a<\/p>\n<ol>\n<li>\u5f15\u5165 <code>image-preview.js<\/code> \u6587\u4ef6\uff0c\u6ce8\u610f\u8bbe\u7f6e <code>type=\"module\"<\/code><\/li>\n<li>\u9700\u8981\u653e\u5927\u7684\u56fe\u7247\u5143\u7d20\u8bbe\u7f6e <code>is-preview<\/code> \u5c5e\u6027\u5373\u53ef<\/li>\n<li>\u5982\u679c\u5e0c\u671b\u4e00\u6b21\u6027\u9884\u89c8\u591a\u4e2a\u56fe\u7247\uff0c\u8bbe\u7f6e\u76f8\u540c\u7684 <code>is-preview<\/code> \u5c5e\u6027\u503c\u5373\u53ef\u81ea\u52a8\u6210\u7ec4<\/li>\n<\/ol>\n<h4>\u5982\u679c\u5e0c\u671b\u7f29\u7565\u56fe\u548c\u5927\u56fe\u4e0d\u662f\u4e00\u4e2a\u5730\u5740<\/h4>\n<p>\u5982\u679c\u5e0c\u671b\u7f29\u7565\u56fe\u662f\u5c0f\u56fe\uff0c\u70b9\u51fb\u67e5\u770b\u7684\u662f\u5927\u56fe\uff0c\u53ef\u4ee5\u4f7f\u7528srcset\u5c5e\u6027\uff0c\u4f8b\u5982\uff1a<\/p>\n<pre>&lt;img src=\"large.jpg\" srcset=\"normal.jpg\"&gt;<\/pre>\n<p>\u672c\u6587\u7684demo\u9875\u9762\u6709\u76f8\u5173\u793a\u610f\uff0c\u672cJS\u4f1a\u5728\u9f20\u6807\u60ac\u505c\u56fe\u7247\u7684\u65f6\u5019\uff0c\u63d0\u524d\u9884\u52a0\u8f7d\u5927\u56fe\u3002<\/p>\n<p>\u5173\u4e8esrcset\u66f4\u591a\u77e5\u8bc6\uff0c\u53ef\u4ee5\u53c2\u89c1\u6b64\u6587\uff1a\u201c<a href=\"https:\/\/www.zhangxinxu.com\/wordpress\/2014\/10\/responsive-images-srcset-size-w-descriptor\/\" rel=\"noopener\" target=\"_blank\">\u54cd\u5e94\u5f0f\u56fe\u7247srcset\u5168\u65b0\u91ca\u4e49sizes\u5c5e\u6027w\u63cf\u8ff0\u7b26<\/a>\u201d<\/p>\n<p>\u5728\u6211\u7684\u4e66\u7c4d\u300aHTML\u5e76\u4e0d\u7b80\u5355\u300b\u4e2d\u5219\u6709\u66f4\u52a0\u8be6\u7ec6\u7684\u4ecb\u7ecd\uff1a<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/imgservices-1252317822.image.myqcloud.com\/coco\/s08122025\/0d8c8283.i9cems.jpg\" width=\"120\" height=\"161\" alt=\"HTML\u5e76\u4e0d\u7b80\u5355\" class=\"alignnone size-medium\" \/><\/p>\n<h4>\u5176\u4ed6\u8bf4\u660e<\/h4>\n<p>\u6ce8\u610f\uff0c\u4ed3\u5e93\u4ee3\u7801\u4f7f\u7528\u4e86CSS\u5d4c\u5957\u3001HTML5 dialog\u3001Page Transition API\u7b49\u65b0\u7279\u6027\uff0c\u8fc7\u4e8e\u9648\u65e7\u7684\u6d4f\u89c8\u5668\u8fd0\u884c\u53ef\u80fd\u4f1a\u6709\u95ee\u9898\u3002<\/p>\n<p>\u4e0d\u8fc7\u8fd9\u4e9b\u95ee\u9898\u90fd\u53ef\u4ee5\u8f7b\u677e\u9002\u914d\uff0c\u5982\u679c\u4f60\u6709\u76f8\u5173\u9700\u6c42\uff0c\u53ef\u4ee5fork\u9879\u76ee\uff0c\u81ea\u884c\u4fee\u6539\uff0c\u4f8b\u5982CSS\u5d4c\u5957\u8bed\u6cd5\u6539\u4e3a\u666e\u901a\u8bed\u6cd5\uff0cdialog\u5143\u7d20\u8865\u5168\u7f3a\u5931\u7684CSS\u3002<\/p>\n<h3>\u56db\u3001\u65b0\u5e74\u5feb\u4e50\uff0c\u5f00\u5de5\u5927\u5409<\/h3>\n<p>\u597d\u4e86\uff0c\u6625\u8282\u56de\u6765\u7684\u7b2c\u4e00\u7bc7\u6587\u7ae0\u3002<\/p>\n<p>\u7528\u4e86\u5f88\u591a\u5b66\u5230\u7684\u65b0\u7279\u6027\uff0c\u611f\u53d7\u5230\u4e86\u5b66\u4e60\u7684\u4ef7\u503c\uff0c\u548c\u65b0\u6280\u672f\u5e26\u6765\u7684\u5f00\u53d1\u4f53\u9a8c\u548c\u7528\u6237\u4f53\u9a8c\u7684\u63d0\u5347\u3002<\/p>\n<p>\u5728\u65b0\u7684\u4e00\u5e74\uff0c\u795d\u5927\u5bb6\u4e07\u4e8b\u987a\u5229\uff0c\u8282\u8282\u9ad8\u5347\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/image.zhangxinxu.com\/image\/blog\/202602\/2026-2-12_181701.jpeg\" width=\"116\" height=\"177\" alt=\"\u6625\u8282\u5feb\u4e50\" class=\"alignnone size-medium\" \/><\/p>\n<p>\u672c\u6587\u4e3a\u539f\u521b\u6587\u7ae0\uff0c\u4f1a\u7ecf\u5e38\u66f4\u65b0\u77e5\u8bc6\u70b9\u4ee5\u53ca\u4fee\u6b63\u4e00\u4e9b\u9519\u8bef\uff0c\u56e0\u6b64\u8f6c\u8f7d\u8bf7\u4fdd\u7559\u539f\u51fa\u5904\uff0c\u65b9\u4fbf\u6eaf\u6e90\uff0c\u907f\u514d\u9648\u65e7\u9519\u8bef\u77e5\u8bc6\u7684\u8bef\u5bfc\uff0c\u540c\u65f6\u6709\u66f4\u597d\u7684\u9605\u8bfb\u4f53\u9a8c\u3002<br \/>\n\u672c\u6587\u5730\u5740\uff1a<a href=\"https:\/\/www.zhangxinxu.com\/wordpress\/?p=12082\">https:\/\/www.zhangxinxu.com\/wordpress\/?p=12082<\/a><\/p>\n<p>\uff08\u672c\u7bc7\u5b8c\uff09<\/p>\n","protected":false},"excerpt":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/image.zhangxinxu.com\/image\/blog\/202602\/2026-2-25_114406.png\" width=\"160\" height=\"109\" alt=\"\u56fe\u7247\u653e\u5927\u5c01\u9762\u56fe\" class=\"alignright size-medium\" loading=\"lazy\" \/><\/p>\n<p>\u524d\u7aef\u6280\u672f\u4e0d\u65ad\u53d1\u5c55\uff0c\u8fd8\u5728\u4f7f\u7528div fixed\u8499\u5c42\uff0canimation\u52a8\u753b\u5b9e\u73b0\u56fe\u7247\u5927\u56fe\u67e5\u770b\u6548\u679c\u5417\uff1fout\u4e86\uff0c\u73b0\u5728\u6709\u4e86\u66f4\u7b80\u5355\uff0c\u4ea4\u4e92\u6548\u679c\u4e5f\u66f4\u597d\u7684\u5b9e\u73b0\u65b9\u6cd5\u4e86\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[180,7],"tags":[1957,1394,1478,835,833,950,949,288,1989,57],"_links":{"self":[{"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/posts\/12082"}],"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=12082"}],"version-history":[{"count":7,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/posts\/12082\/revisions"}],"predecessor-version":[{"id":12099,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/posts\/12082\/revisions\/12099"}],"wp:attachment":[{"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/media?parent=12082"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/categories?post=12082"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/tags?post=12082"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}