{"id":11318,"date":"2024-08-23T22:18:10","date_gmt":"2024-08-23T14:18:10","guid":{"rendered":"https:\/\/www.zhangxinxu.com\/wordpress\/?p=11318"},"modified":"2025-01-15T16:04:54","modified_gmt":"2025-01-15T08:04:54","slug":"css-scroll-timeline","status":"publish","type":"post","link":"https:\/\/www.zhangxinxu.com\/wordpress\/2024\/08\/css-scroll-timeline\/","title":{"rendered":"\u4e0d\u80fd\u843d\u540e\uff0c\u597d\u597d\u7f15\u7f15CSS\u6eda\u52a8\u52a8\u753b"},"content":{"rendered":"<p>by <a href=\"https:\/\/www.zhangxinxu.com\/\">zhangxinxu<\/a> from <a href=\"https:\/\/www.zhangxinxu.com\/wordpress\/?p=11318\">https:\/\/www.zhangxinxu.com\/wordpress\/?p=11318<\/a><br \/>\n\u672c\u6587\u53ef\u5168\u6587\u8f6c\u8f7d\uff0c\u72ec\u7acb\u57df\u540d\u4e2a\u4eba\u7f51\u7ad9\u65e0\u9700\u6388\u6743\uff0c\u4f46\u9700\u8981\u4fdd\u7559\u539f\u4f5c\u8005\u3001\u51fa\u5904\u4ee5\u53ca\u6587\u4e2d\u94fe\u63a5\uff0c\u4efb\u4f55\u7f51\u7ad9\u5747\u53ef\u6458\u8981\u805a\u5408\uff0c\u5546\u7528\u8bf7\u8054\u7cfb\u6388\u6743\u3002<\/p>\n<p><script>window.wxShareImgUrl = 'https:\/\/www.zhangxinxu.com\/study\/image\/share\/202408-scroll-animation.jpg';<\/script><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/image.zhangxinxu.com\/image\/blog\/202408\/scroll-animation-cover.jpg\" width=\"415\" height=\"270\" alt=\"\u5c01\u9762\u5360\u4f4d\u56fe \u6eda\u52a8\u52a8\u753b\" class=\"alignright clip thumb size-medium\" \/><\/p>\n<p>\u751f\u547d\u4e0d\u505c\uff0c\u5b66\u4e60\u4e0d\u6b62\uff0cCSS\u6eda\u52a8\u52a8\u753b\u51fa\u6765\u5df2\u7ecf\u67091\u5e74\u4e86\u3002<\/p>\n<p>Safari\u6d4f\u89c8\u5668\u5230\u73b0\u5728\u8fd8\u6ca1\u6709\u652f\u6301\uff0c\u5982\u4e0b\u56fe\u6240\u793a\uff1a<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/image.zhangxinxu.com\/image\/blog\/202408\/2024-08-20_231251.png\" width=\"650\" height=\"232\" alt=\"\u6eda\u52a8\u52a8\u753b\" class=\"alignnone size-medium\" \/><\/p>\n<p>\u4f46\u662f\u6211\u5df2\u7ecf\u7b49\u4e0d\u53ca\u4e86\uff0c\u5f88\u591a\u4eba\u90fd\u5df2\u7ecf\u5f00\u59cb\u5728\u751f\u4ea7\u73af\u5883\u4f7f\u7528\u8fd9\u4e2a\u65b0\u7279\u6027\u4e86\uff0c\u6211\u4e5f\u4e0d\u80fd\u843d\u540e\uff0c\u5b66\u8d77\u6765\uff0c\u4e0d\u8981\u7ba1Safari\u4e86\u3002<\/p>\n<p>\u7136\u540e\uff0c\u6eda\u52a8\u52a8\u753b\u9700\u8981\u7684CSS\u5c5e\u6027\u4e0d\u4ec5\u662f<code>scroll-timeline<\/code>\u3001<code>view-timeline<\/code>\uff0c<code>animation-timeline<\/code>\u5c5e\u6027\u4e5f\u662f\u9700\u8981\u7684\uff0c\u8fd9\u4e5f\u662fCSS\u65b0\u7279\u6027\uff0c\u4f1a\u5728\u672c\u6587\u4e00\u540c\u4ecb\u7ecd\u3002<\/p>\n<p>\u597d\uff0c\u5f00\u59cb\u5427\u3002<\/p>\n<h3>\u4e00\u3001\u6e29\u6545\u800c\u77e5\u65b0<\/h3>\n<p>\u5176\u5b9e\u6eda\u52a8\u52a8\u753b\u6211\u5f88\u591a\u5e74\u524d\u4e5f\u8fd1\u4f3c\u5b9e\u73b0\u8fc7\uff0c\u8ba9\u6211\u627e\u627e\u2026\u2026<\/p>\n<p>\u54e6\uff0c\u627e\u5230\u4e86\uff0cCSS\u5b9e\u73b0\u6eda\u52a8\u6307\u793a\u5668\uff0c\u6548\u679c\u5982\u4e0bGIF\u793a\u610f\uff08\u6ce8\u610f\u4e0a\u8fb9\u7f18\u7684\uff09\u3002<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" src=\"\/\/image.zhangxinxu.com\/image\/blog\/201906\/scroll-indicator-better-s.gif\" width=\"366\" height=\"604\" alt=\"\u6211\u7684\u66f4\u597d\u7684CSS\u6eda\u52a8\u6307\u793a\u5668\" class=\"alignnone m-center size-medium\"><\/p>\n<p>\u8be6\u89c1\u201c<a href=\"https:\/\/www.zhangxinxu.com\/wordpress\/2019\/06\/better-css-scroll-indicator\/\">\u66f4\u597d\u7684\u7eafCSS\u6eda\u52a8\u6307\u793a\u5668\u6280\u672f\u5b9e\u73b0<\/a>\u201d\u4e00\u6587\u3002<\/p>\n<p>\u73b0\u5728\u6709\u4e86\u539f\u751f\u7684CSS\u6eda\u52a8\u52a8\u753b\uff0c\u6eda\u52a8\u6307\u793a\u5668\u7684\u5b9e\u73b0\u90a3\u5c31\u7b80\u5355\u591a\u4e86\u3002<\/p>\n<p>\u4ee3\u7801\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<pre>&lt;div class=\"scroller\"&gt;\r\n    &lt;ins&gt;&lt;\/ins&gt;\r\n    &lt;div style=\"height:400px;\"&gt;&lt;\/div&gt;\r\n&lt;\/div&gt;<\/pre>\n<pre>.scroller {\r\n    height: 200px;\r\n    border: 1px solid;\r\n    overflow: auto;\r\n    scroll-timeline: --indicator;\r\n}\r\n.scroller ins {\r\n    display: block;\r\n    border-top: solid green;\r\n    animation-name: widthExpand;\r\n    animation-duration: 1ms; \/* Firefox\u9700\u8981\u8bbe\u7f6e\u8fd9\u4e2a*\/\r\n    animation-timeline: --indicator;\r\n    position: sticky;\r\n    top: 0;\r\n}\r\n@keyframes widthExpand {\r\n    from { width: 0%; }    \r\n    to { width: 100%; }\r\n}<\/pre>\n<p>\u6b64\u65f6\uff0c\u6eda\u52a8\u5bb9\u5668\uff0c\u5c31\u53ef\u4ee5\u770b\u5230<code>&lt;ins&gt;<\/code>\u5143\u7d20\u7684\u5bbd\u5ea6\u968f\u7740\u6eda\u52a8\u8ddd\u79bb\u7684\u8fdb\u884c\u53d8\u4ece0%-100%\u53d8\u5316\u4e86\uff0c\u5982\u4e0bGIF\u5f55\u5c4f\u6240\u793a\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/image.zhangxinxu.com\/image\/blog\/202408\/scroll-indicator.gif\" width=\"495\" height=\"216\" alt=\"\u6eda\u52a8\u6307\u793a\u5668\u6548\u679c\u793a\u610f\" class=\"alignnone size-medium\" \/><\/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\/202408\/css-scroll-timeline-indicator-demo.php\" rel=\"noopener\" target=\"_blank\">\u4f7f\u7528\u539f\u751fCSS\u6eda\u52a8\u52a8\u753b\u5b9e\u73b0\u6eda\u52a8\u6307\u793a\u5668demo<\/a><\/p>\n<h4>\u548c\u4f20\u7edf\u52a8\u753b\u6548\u679c\u5b9e\u73b0\u7684\u533a\u522b<\/h4>\n<p>\u548c\u4f20\u7edfCSS animation\u52a8\u753b\u5b9e\u73b0\u7684\u533a\u522b\u5c31\u4e24\u70b9\uff1a<\/p>\n<ul>\n<li>\u4e00\u662f\u5728\u6eda\u52a8\u5bb9\u5668\u90a3\u91cc\u4f7f\u7528scroll-timeline\u5c5e\u6027\u5b9a\u4e49\u4e00\u4e2a\u6eda\u52a8\u65f6\u95f4\u7ebf\u7684CSS\u53d8\u91cf\uff1b<\/li>\n<li>\u4e8c\u662f\u5728\u9700\u8981\u52a8\u753b\u7684\u5143\u7d20\u90a3\u91cc\u4f7f\u7528animation-timeline\u6307\u5b9a\u4f7f\u7528\u7684\u52a8\u753b\u65f6\u95f4\u7ebf\u5373\u53ef\u3002<\/li>\n<\/ul>\n<h4>\u5173\u4e8eanimation-timeline\u5c5e\u6027<\/h4>\n<p>\u52a8\u753b\u65f6\u95f4\u7ebf\u5c5e\u6027animation-timeline\u4e5f\u662f\u4e2a\u65b0\u7684CSS\u5c5e\u6027\uff0c\u5176\u8bed\u6cd5\u8fd8\u6bd4\u8f83\u590d\u6742\uff0c\u4ee5\u4e0b\u662f\u4e00\u4e9b\u4f7f\u7528\u793a\u610f\uff1a<\/p>\n<pre>\/* \u5355\u4e2a\u5df2\u547d\u540d\u52a8\u753b\u65f6\u95f4\u7ebf *\/\r\nanimation-timeline: --timeline_name;\r\n\r\n\/* \u5355\u4e2a\u533f\u540d\u6eda\u52a8\u8fdb\u7a0b\u65f6\u95f4\u7ebf *\/\r\nanimation-timeline: scroll();\r\nanimation-timeline: scroll(scroller axis);\r\n\r\n\/* \u5355\u4e2a\u533f\u540d\u53ef\u89c6\u8fdb\u7a0b\u52a8\u753b\u65f6\u95f4\u7ebf *\/\r\nanimation-timeline: view();\r\nanimation-timeline: view(axis inset);\r\n\r\n\/* \u591a\u4e2a\u52a8\u753b *\/\r\nanimation-timeline: --progressBarTimeline, --carouselTimeline;\r\nanimation-timeline: none, --slidingTimeline;<\/pre>\n<p>\u5176\u4e2d\uff0c<code>scroll()<\/code>\u5c31\u662f\u6839\u636e\u6eda\u52a8\u4f4d\u7f6e\u786e\u5b9a\u52a8\u753b\u8fdb\u5ea6\u7684\uff0c\u800c<code>view()<\/code>\u5219\u662f\u6839\u636e\u52a8\u753b\u5143\u7d20\u5728\u6eda\u52a8\u5bb9\u5668\u4e2d\u7684\u4f4d\u7f6e\u786e\u5b9a\u52a8\u753b\u8fdb\u5ea6\u7684\uff0c\u5f80\u5f80\u9700\u8981\u914d\u5408<code>view-timeline<\/code>\u5c5e\u6027\u4e00\u8d77\u4f7f\u7528\uff0c\u8fd9\u4e2a\u5355\u72ec\u62ce\u4e00\u4e2a\u7ae0\u8282\u7b80\u5355\u4ecb\u7ecd\u4e0b\u3002<\/p>\n<h3>\u4e8c\u3001\u6eda\u52a8\u89c6\u533a\u5185\u7684\u52a8\u753b<\/h3>\n<p>\u4f8b\u5982\u8fd9\u4e2a\u5e38\u89c1\u7684\u6eda\u52a8\u52a8\u753b\u6548\u679c\uff0c\u56fe\u7247\u968f\u7740\u6eda\u52a8\u8fdb\u884c\uff0c\u653e\u5927\u540c\u65f6\u6de1\u51fa\u663e\u793a\uff0c\u5219\u5c31\u53ef\u4ee5\u4f7f\u7528<code>view-timeline<\/code>\u5c5e\u6027\u52a0<code>animation-timeline<\/code>\u5c5e\u6027\u5b9e\u73b0\uff0c\u4f8b\u5982\uff1a<\/p>\n<pre>&lt;div class=\"scroller\"&gt;\r\n    &lt;div style=\"height:100px;\"&gt;&lt;\/div&gt;\r\n    &lt;p&gt;\u6211\u662f\u56fe\u72471\uff0c\u662f\u4e0d\u662f\u5f88\u719f\u6089\uff0c\u4e13\u5c5e\u914d\u56fe\uff1a&lt;\/p&gt;\r\n    &lt;p&gt;&lt;img src=\"https:\/\/image.zhangxinxu.com\/image\/study\/s\/hanyun.jpg\" \/&gt;&lt;\/p&gt;\r\n    &lt;p&gt;\u6700\u8fd1\u4e0a\u67b6\u65b0\u4e66\u4f5c\u54c1\u5c01\u9762\u56fe\uff1a&lt;\/p&gt;\r\n    &lt;p&gt;&lt;img src=\"https:\/\/image.zhangxinxu.com\/image\/blog\/202407\/2024-7-23_144238.jpeg\" \/&gt;&lt;\/p&gt;\r\n    &lt;div style=\"height:100px;\"&gt;&lt;\/div&gt;\r\n&lt;\/div&gt;<\/pre>\n<pre>.scroller {\r\n    height: 200px;\r\n    max-width: 380px;\r\n    border: 1px solid;\r\n    overflow: auto;\r\n}\r\n.scroller img {\r\n    max-width: 100%;\r\n    animation: 1ms scaleUp both, 1ms fadeIn both;\r\n    animation-timeline: --scaleFade;\r\n    view-timeline: --scaleFade;\r\n}\r\n@keyframes scaleUp {\r\n    from { transform: scale(0); }    \r\n    to { transform: scale(1); }\r\n}\r\n@keyframes fadeIn {\r\n    from { opacity: 0; }    \r\n    to { opacity: 1; }\r\n}<\/pre>\n<p>\u6b64\u65f6\uff0c\u968f\u7740\u5bb9\u5668\u6eda\u52a8\uff0c\u56fe\u7247\u5c31\u4f1a\u6839\u636e\u81ea\u8eab\u5728\u6eda\u52a8\u89c6\u533a\u7684\u4f4d\u7f6e\u8fdb\u884c\u7f29\u653e\u548c\u6de1\u5165\u6de1\u51fa\u6548\u679c\u4e86\uff0c\u5982\u4e0bMP4\u5f55\u5c4f\u6240\u793a\uff08\u4e0d\u52a8\u70b9\u51fb\u64ad\u653e\uff09\uff1a<\/p>\n<p><video src='\/\/image.zhangxinxu.com\/video\/blog\/202408\/img-fade-scroll-view.mp4' type=\"video\/mp4\" width=\"404\" height=\"243\" 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>\u773c\u89c1\u4e3a\u5b9e\uff0c\u60a8\u53ef\u4ee5\u72e0\u72e0\u5730\u70b9\u51fb\u8fd9\u91cc\uff1a<a href=\"https:\/\/www.zhangxinxu.com\/study\/202408\/css-view-timeline-fadein-demo.php\" rel=\"noopener\" target=\"_blank\">CSS\u6eda\u52a8\u52a8\u753b\u5b9e\u73b0\u56fe\u7247\u6de1\u51fa\u7f29\u653e\u6548\u679cdemo<\/a><\/p>\n<p>\u5982\u679c\u4f60\u60f3\u7cbe\u786e\u63a7\u5236\u56fe\u7247\u5143\u7d20\u5728\u89c6\u7a97\u7684\u54ea\u4e2a\u4f4d\u7f6e\u5f00\u542f\u52a8\u753b\u3001\u7ed3\u675f\u52a8\u753b\uff0c\u53ef\u4ee5\u4f7f\u7528<code>animation-range<\/code>\u8fd9\u4e2a\u65b0\u7684CSS\u5c5e\u6027\u3002<\/p>\n<p>\u7136\u800c<code>animation-range<\/code>\u8fd9\u4e2a\u5c5e\u6027\u7684\u5b66\u4e60\u6210\u672c\u975e\u5e38\u9ad8\uff0c\u6211\u5efa\u8bae\u6682\u65f6\u5148\u4e0d\u8981\u6df1\u5165\u5b66\u4e60\u3002<\/p>\n<p>PS\uff1a<a href=\"https:\/\/scroll-driven-animations.style\/tools\/view-timeline\/ranges\/\" rel=\"noopener\" target=\"_blank\">\u9644\u4e0a animation-range\u89c6\u89c9\u5316\u8f85\u52a9\u5de5\u5177\u9875\u9762\uff08 View Timeline Ranges Visualizer\uff09<\/a>\u3002<\/p>\n<h3>\u4e09\u3001\u82e5\u6eda\u52a8\u5bb9\u5668\u5916\u5143\u7d20\u6709\u52a8\u753b\uff1f<\/h3>\n<p>\u672c\u6587\u76ee\u524d\u4e3a\u6b62\u5c55\u793a\u7684\u4e24\u4e2a\u6848\u4f8b\u5747\u662f\u6eda\u52a8\u5bb9\u5668\u5185\u5143\u7d20\u53d1\u751f\u4e86\u52a8\u753b\u3002<\/p>\n<p>\u5982\u679c\u5e0c\u671b\u6eda\u52a8\u5bb9\u5668\u5143\u7d20A\uff0c\u4f46\u662f\u5bb9\u5668\u5143\u7d20A\u4e4b\u5916\u7684\u5143\u7d20\u53d1\u751f\u5bf9\u5e94\u7684\u52a8\u753b\u6548\u679c\uff0c\u90a3\u4e48\u53ef\u4ee5\u5b9e\u73b0\u5417\uff1f<\/p>\n<p>?<\/p>\n<p>\u53ef\u4ee5\uff01<\/p>\n<p>\u4f7f\u7528CSS\u7684<code>timeline-scope<\/code>\u5c5e\u6027\u6539\u53d8\u52a8\u753b\u65f6\u95f4\u7ebf\u7684\u4f5c\u7528\u8303\u56f4\u3002<\/p>\n<p>\u5047\u8bbe\u6709\u4e2a\u6eda\u52a8\u5bb9\u5668\uff0c\u7136\u540e\u5bb9\u5668\u5916\u6709\u4e2a\u56fe\u7247\uff0cHTML\u4ee3\u7801\u793a\u610f\uff1a<\/p>\n<pre>&lt;div class=\"scroller\"&gt;\r\n    &lt;div style=\"height:400px;\"&gt;&lt;\/div&gt;\r\n&lt;\/div&gt;\r\n\r\n&lt;img class=\"target\" src=\"1.jpg\" \/&gt;<\/pre>\n<p>\u5219\u4e0b\u9762\u7684CSS\u4ee3\u7801\u5c31\u53ef\u4ee5\u5b9e\u73b0\u6eda\u52a8\u7684\u65f6\u5019\uff0c\u56fe\u7247\u65cb\u8f6c\u653e\u5927\uff0c\u540c\u65f6\u6de1\u51fa\u7684\u6548\u679c\u3002<\/p>\n<pre>body {\r\n    timeline-scope: --scaleFade;\r\n}\r\n.scroller {\r\n    height: 200px;\r\n    border: 1px solid;\r\n    overflow: auto;\r\n    scroll-timeline: --scaleFade;\r\n}\r\n.target {\r\n    animation: 1ms scaleRoate both, 1ms fadeIn both;\r\n    animation-timeline: --scaleFade;\r\n}\r\n@keyframes scaleRoate {\r\n    from { transform: scale(0) rotate(0deg); }    \r\n    to { transform: scale(1) rotate(360deg); }\r\n}\r\n@keyframes fadeIn {\r\n    from { opacity: 0; }    \r\n    to { opacity: 1; }\r\n}<\/pre>\n<p>\u52a8\u6001\u6548\u679c\u793a\u610f\uff08\u4e0d\u52a8\u8bf7\u70b9\u51fb\uff09\uff1a<\/p>\n<p><video src='\/\/image.zhangxinxu.com\/video\/blog\/202408\/timeline-scope.mp4' type=\"video\/mp4\" width=\"375\" height=\"435\" 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>\u5b9e\u5730\u611f\u53d7\u4e0b\u6548\u679c\uff0c\u60a8\u53ef\u4ee5\u72e0\u72e0\u5730\u70b9\u51fb\u8fd9\u91cc\uff1a<a href=\"https:\/\/www.zhangxinxu.com\/study\/202408\/css-timeline-scope-scroll-animation-demo.php\" rel=\"noopener\" target=\"_blank\">timeline-scope\u8ba9\u6eda\u52a8\u5bb9\u5668\u5916\u5143\u7d20\u52a8\u753bdemo<\/a><\/p>\n<p>\u4e5f\u5c31\u662f\uff0c\u5c06\u6eda\u52a8\u52a8\u753b\u65f6\u95f4\u7ebf <code>--scaleFade<\/code> \u7684\u4f5c\u7528\u8303\u56f4\u63d0\u9ad8\u5230\u4e86body\u5143\u7d20\u4e0b\u3002<\/p>\n<h3>\u56db\u3001\u53ef\u5426\u7528\u6765\u68c0\u6d4b\u662f\u5426\u53ef\u6eda\u52a8<\/h3>\n<p><code>scroll-timeline<\/code>\u5c5e\u6027\u8fd8\u6709\u4e00\u4e2a\u975e\u5e38\u91cd\u8981\u7684\u884d\u751f\u4f5c\u7528\uff0c\u5c31\u662f\u68c0\u6d4b\u4e00\u4e2adiv\u5143\u7d20\u662f\u5426\u6eda\u52a8\u6ea2\u51fa\uff08\u5185\u5bb9\u8d85\u8fc7\u5bb9\u5668\u7684\u9ad8\u5bbd\u9650\u5236\uff09\uff0c\u5177\u4f53\u5b9e\u73b0\u5982\u4e0b\u3002<\/p>\n<h4>1. \u5bb9\u5668overflow\u4e0d\u662fvisible<\/h4>\n<p>\u8fd9\u6837\uff0c\u5bb9\u5668\u624d\u6709\u53ef\u80fdscrollHeight\u5927\u4e8eclientHeight\u3002<\/p>\n<p>\u5047\u8bbeHTML\u5982\u4e0b\uff1a<\/p>\n<pre>&lt;section&gt;\r\n    &lt;p&gt;\u5185\u5bb9...&lt;\/p&gt;\r\n    &lt;button&gt;\u66f4\u591a&lt;\/button&gt;\r\n&lt;\/section&gt;\r\n<\/pre>\n<p>\u5219\u53ef\u4ee5\u8fd9\u4e48\u8bbe\u7f6e\uff1a<\/p>\n<pre>section {\r\n    max-height: 120px;\r\n    overflow: hidden;\r\n}\r\nbutton {\r\n    display: none;\r\n}<\/pre>\n<p>\u6b64\u65f6\uff0c\u5185\u5bb9\u9ad8\u5ea6\u8d85\u8fc7120px\u7684\u65f6\u5019\uff0c\u5c31\u5c5e\u4e8e\u6eda\u52a8\u5185\u5bb9\u6ea2\u51fa\uff0c\u8fd9\u4e2a\u76ee\u524dCSS\u662f\u53ef\u4ee5\u68c0\u6d4b\u51fa\u6765\u7684\uff0c\u6b64\u65f6\u6211\u4eec\u5c31\u53ef\u4ee5\u8ba9\u201c\u66f4\u591a\u201d\u6309\u94ae\u663e\u793a\u51fa\u6765\u3002<\/p>\n<h4>2. \u68c0\u6d4b\u6eda\u52a8\u6ea2\u51fa<\/h4>\n<p>\u76f8\u5173CSS\u4ee3\u7801\u5982\u4e0b\uff0c\u57fa\u672c\u4e0a\u90fd\u662f\u56fa\u5b9a\u7684\uff0c\u53ef\u4ee5\u590d\u7528\u5728\u51e0\u4e4e\u5176\u4ed6\u4efb\u610f\u7c7b\u4f3c\u573a\u666f\u4e0b\u3002<\/p>\n<pre>section {\r\n    --flag: false;\r\n    animation: setFlag 1ms;\r\n    scroll-timeline: --detectScroll;\r\n    animation-timeline: --detectScroll;\r\n}\r\n@keyframes setFlag {\r\n    from, to { --flag: true; }    \r\n}\r\n@container style(--flag: true) {\r\n    \/* \u5bb9\u5668\u6ea2\u51fa *\/\r\n    button {\r\n        display: block;    \r\n    }\r\n}<\/pre>\n<p>\u7ed3\u675f\uff01<\/p>\n<p>\u4ee5\u4e0a\u8fd9\u6bb5CSS\u4ee3\u7801\u662f\u672c\u6587\u6700\u6709\u4ef7\u503c\u7684\u4e00\u6bb5\u4ee3\u7801\uff0c\u7b49\u4ee5\u540e\u6eda\u52a8\u52a8\u753b\u6ca1\u6709\u517c\u5bb9\u6027\u7684\u9650\u5236\u540e\uff0c\u5e94\u8be5\u4f1a\u6210\u4e3a\u524d\u7aef\u8fdb\u9636\u5fc5\u5b66\u6280\u672f\u4e4b\u4e00\u4e86\u3002<\/p>\n<p>\u5176\u4e2d\uff0c\u7528\u5230\u4e86CSS\u6eda\u52a8\u52a8\u753b\uff0cCSS\u4f20\u7edf\u52a8\u753b\u4ee5\u53caCSS\u5bb9\u5668\u67e5\u8be2\u7684<code>style()<\/code>\u8bed\u6cd5\uff08\u6837\u5f0f\u68c0\u6d4b\uff0c\u76ee\u524d\u4ec5\u652f\u6301CSS\u53d8\u91cf\uff09\uff0c\u5df2\u7ecf\u9010\u6e10\u8131\u79bb\u4e86\u65e9\u5e74\u7684CSS\u98ce\u683c\u3002<\/p>\n<p>\u524d\u7aef\u5c31\u662f\u8fd9\u6837\uff0c\u6280\u672f\u8fed\u4ee3\u5f88\u5feb\uff0c\u51e0\u5e74\u4e0d\u5b66\uff0c\u56de\u5934\u4e00\u770b\uff0c\u8fd9\u90fd\u5565\u8ddf\u5565\u554a\u3002<\/p>\n<p>\u6709demo\uff0c\u65b9\u4fbf\u5927\u5bb6\u5b66\u4e60\uff0c\u60a8\u53ef\u4ee5\u72e0\u72e0\u5730\u70b9\u51fb\u8fd9\u91cc\uff1a<a href=\"https:\/\/www.zhangxinxu.com\/study\/202408\/css-scroll-timeline-show-more-demo.php\">CSS\u81ea\u52a8\u8bc6\u522b\u6eda\u52a8\u6ea2\u51fa\u663e\u793a\u5c55\u5f00\u6309\u94aedemo<\/a><\/p>\n<p>\u6548\u679c\u5982\u4e0b\u56fe\u6240\u793a\uff0c\u4e0a\u9762\u7684\u6587\u5b57\u5185\u5bb9\u5c11\uff0c\u6ca1\u6709\u5c55\u5f00\u6309\u94ae\uff0c\u4e0b\u9762\u8fd9\u4e2adiv\u6587\u5b57\u5185\u5bb9\u591a\uff0c\u5c55\u5f00\u6309\u94ae\u5c31\u81ea\u52a8\u663e\u793a\u4e86\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/image.zhangxinxu.com\/image\/blog\/202408\/2024-08-23_215025.png\" width=\"375\" height=\"280\" alt=\"\u5c55\u5f00\u548c\u6536\u8d77\u6309\u94ae\u81ea\u52a8\u663e\u793ademo\" class=\"alignnone size-medium\" \/><\/p>\n<p>\u62c9\u52a8\u53f3\u4e0b\u89d2\u7684\u62d6\u62fd\u5c0f\u6309\u94ae\uff0c\u6539\u53d8\u5bb9\u5668\u5c3a\u5bf8\uff0c\u53ef\u4ee5\u770b\u5230\u5f53\u5c0f\u5230\u4e00\u5b9a\u7a0b\u5ea6\u7684\u65f6\u5019\uff0c\u4e0a\u9762\u7684\u5185\u5bb9\u6846\u7684\u5c55\u5f00\u6309\u94ae\u4e5f\u663e\u793a\u4e86\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/image.zhangxinxu.com\/image\/blog\/202408\/2024-08-23_215303.png\" width=\"269\" height=\"300\" alt=\"\u4e24\u4e2a\u6846\u5c55\u5f00\u6309\u94ae\u5747\u663e\u793a\" class=\"alignnone size-medium\" \/><\/p>\n<h4>\u5b9e\u73b0\u539f\u7406<\/h4>\n<p>\u5982\u679c\u5bb9\u5668\u53ef\u6eda\u52a8\uff0c\u4f1a\u5e94\u7528\u540d\u4e3asetFlag\u7684\u52a8\u753b\uff0c\u800csetFlag\u52a8\u753b\u505a\u7684\u552f\u4e00\u4e8b\u60c5\u5c31\u662f\u91cd\u7f6e\u6807\u5fd7CSS\u53d8\u91cf&#8211;flag\uff0c\u800c&#8211;flag\u4e00\u65e6\u53d8\u5316\uff0c\u5c31\u4f1a\u88ab\u5bb9\u5668\u67e5\u8be2\u68c0\u6d4b\u5230\uff0c\u4e8e\u662f\uff0c\u5bb9\u5668\u7684\u5b50\u5143\u7d20\u6837\u5f0f\u5c31\u53ef\u4ee5\u968f\u610f\u8bbe\u7f6e\u4e86\u3002<\/p>\n<p>\u770b\u8d77\u6765\u50cf\u662f\u4e2a\u4e09\u7ea7\u8054\u52a8\u7684\u4e1c\u897f\u3002<\/p>\n<p>\u975e\u5e38\u5de7\u5999\u7684\u5b9e\u73b0\u3002<\/p>\n<h3>\u4e94\u3001\u5176\u5b9e\u8fd8\u6709\u5f88\u591a\u77e5\u8bc6<\/h3>\n<p><strong>\u66f4\u65b0\u4e8e2025\u5e741\u67086\u65e5<\/strong><\/p>\n<p>\u5b9e\u8df5\u8868\u660e\uff0c\u6eda\u52a8\u52a8\u753b\u8981\u60f3\u6309\u7167\u9884\u671f\u6267\u884c\uff0c\u7956\u5148\u5143\u7d20\u662f\u4e0d\u80fd\u8bbe\u7f6e<code>overflow:hidden<\/code>\u7684\u3002<\/p>\n<p><strong>\u66f4\u65b0\u4e8e2025\u5e741\u670815\u65e5<\/strong><\/p>\n<p><code>animation-timeline<\/code>\u6eda\u52a8\u52a8\u753b\u662f\u6709Polyfill\u7684\uff0c\u5730\u5740\uff1a<a href=\"https:\/\/github.com\/flackr\/scroll-timeline\" rel=\"noopener\" target=\"_blank\">https:\/\/github.com\/flackr\/scroll-timeline<\/a><\/p>\n<p>\u76f4\u63a5\u5f15\u5165\u5bf9\u5e94\u7684JS\u6587\u4ef6\u5c31\u6709\u6548\u679c\u4e86\u3002<\/p>\n<p>\u9700\u8981\u6ce8\u610f\u7684\u662f\uff1a<\/p>\n<ul>\n<li>\u6eda\u52a8\u52a8\u753b\u76f8\u5173\u4ee3\u7801\u8981\u5199\u5728\u4e00\u4e2aCSS\u89c4\u5219\u4e2d\uff0c\u5426\u5219\u4f1a\u65e0\u6548\uff0c\u56e0\u4e3a\u52a8\u753b\u8bc6\u522b\u662f\u901a\u8fc7\u89e3\u6784CSS\u8bed\u53e5\u5206\u6790\u5b9e\u73b0\u7684\uff1b<\/li>\n<li>\u4e0d\u652f\u6301vh\u7b49\u5c3a\u5bf8\u5355\u4f4d\uff0c\u4f7f\u7528px\u4ee3\u66ff\uff1b<\/li>\n<li>CSS\u4ee3\u7801\u6700\u597d<code>&lt;style&gt;<\/code>\u5185\u8054\uff0c\u5982\u679c\u5728CSS\u6587\u4ef6\u4e2d\uff0c\u9700\u8981\u4fdd\u8bc1\u8be5CSS\u6587\u4ef6\u5730\u5740\u4e0d\u80fd\u8de8\u57df\uff0c\u5426\u5219\u65e0\u6cd5\u89e3\u6790CSS\u4ee3\u7801\uff0c\u4e5f\u5c31\u65e0\u6cd5\u8bc6\u522b\u54ea\u4e9b\u5143\u7d20\u5e94\u7528\u4e86\u6eda\u52a8\u52a8\u753b\u3002<\/li>\n<\/ul>\n<p><span class=\"s\">&#8212;&#8212;end&#8212;&#8212;<\/span><\/p>\n<p>\u5176\u5b9e\u6eda\u52a8\u52a8\u753b\u8fd8\u6709\u975e\u5e38\u591a\u7684\u77e5\u8bc6\uff0c\u8fd8\u662f\u65e5\u540e\u518d\u8bf4\u5427\u3002<\/p>\n<p>\u4f8b\u5982\uff0c\u4e0a\u9762\u7684\u6eda\u52a8\u68c0\u6d4b\u4e5f\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528<code>animation-timeline:scroll()<\/code>\uff0c\u53ef\u4ee5\u7701\u6389\u4e00\u4e2a<code>scroll-timeline<\/code>\u5c5e\u6027\uff0c\u4f46\u662f\u53ea\u80fd\u8bbe\u7f6e\u5728\u5bb9\u5668\u7684\u5b50\u5143\u7d20\u4e0a\u624d\u6709\u6548\uff0c\u6240\u4ee5\uff0c\u8fd8\u9700\u8981\u518d\u5d4c\u5957\u4e00\u5c42HTML\u6807\u7b7e\u7528\u6765\u5305\u88f9\u5185\u5bb9\u3002<\/p>\n<p>\u4ee3\u7801\u5927\u540c\u5c0f\u5f02\uff1a<\/p>\n<pre>&lt;section&gt;\r\n    &lt;div class=\"wrap\"&gt;\r\n        &lt;p&gt;\u6bb5\u843d\u6587\u5b57...&lt;\/p&gt;\r\n        &lt;button&gt;\u66f4\u591a&lt;\/button&gt;\r\n    &lt;\/div&gt;\r\n&lt;\/section&gt;<\/pre>\n<pre>.wrap {\r\n  --flag: false;\r\n  animation: setFlag 1ms;\r\n  animation-timeline: scroll();\r\n}\r\n@keyframes setFlag {\r\n  from, to { --flag: true; }    \r\n}\r\n@container style(--flag: true) {\r\n  button { display: block; }\r\n}<\/pre>\n<p>\u4e5f\u5c31\u662f\u7701\u4e86\u4e2aCSS\u58f0\u660e\uff0c\u4f46\u662f\u9700\u8981\u591a\u4e00\u5c42HTML\uff0c\u4e0d\u89c1\u5f97\u5212\u7b97\uff0c\u9664\u975e\u539f\u672cHTML\u5c31\u6709\u4e00\u5c42\u5bb9\u5668\u5d4c\u5957\u3002<\/p>\n<p>\u9664\u4e86<code>scroll-timeline<\/code>\u5c5e\u6027\uff0c\u8fd8\u6709\u4e2a\u4e0e\u4e4b\u76f8\u5bf9\u7684<code>view-timeline<\/code>\u5c5e\u6027\uff0c\u524d\u8005\u76f8\u5bf9\u4e8e\u6574\u4e2a\u6eda\u52a8\u8303\u56f4\uff0c\u540e\u8005\u9488\u5bf9\u67d0\u4e2a\u5177\u4f53\u5143\u7d20\uff0c\u800c\u4e14\u5f80\u5f80\u9700\u8981\u914d\u5408<code>animation-range<\/code>\u4f7f\u7528\uff08\u4ec0\u4e48\u65f6\u5019\u52a8\u753b\u624d\u6267\u884c\uff09\u3002<\/p>\n<p>\u603b\u800c\u8a00\u4e4b\uff0c\u6eda\u52a8\u52a8\u753b\u6240\u6d89\u53ca\u5230\u7684\u77e5\u8bc6\u8981\u8fdc\u6bd4\u672c\u6587\u4ecb\u7ecd\u7684\u8981\u591a\u3002<\/p>\n<p>\u4e0d\u8fc7\uff0c\u7531\u4e8e\u517c\u5bb9\u6027\u7684\u9650\u5236\uff0c\u76ee\u524d\uff0c\u4e86\u89e3\u672c\u6587\u8fd9\u51e0\u4e2a\u7ecf\u5178\u6848\u4f8b\u5c31\u8db3\u591f\u4e86\u3002<\/p>\n<p>\u597d\uff0c\u5c31\u8bf4\u8fd9\u4e48\u591a\u5427\u3002<\/p>\n<p>\u65ad\u65ad\u7eed\u7eed\u5199\u4e86\u4e00\u5468\u624d\u5b8c\u6210\uff0c\u5982\u679c\u4f60\u89c9\u5f97\u6709\u6240\u6536\u83b7\uff0c\u6b22\u8fce\u8f6c\u53d1\uff0c\u6b22\u8fce<button onclick=\"shareWeixin.click()\" class=\"entry-share-btn\">\u5206\u4eab<\/button>\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/image.zhangxinxu.com\/image\/blog\/202408\/2024-08-23_220808.jpg\" width=\"90\" height=\"82\" alt=\"\u5360\u4f4d\u5e95\u56fe\" 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=11318\">https:\/\/www.zhangxinxu.com\/wordpress\/?p=11318<\/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\/202408\/scroll-animation-cover.jpg\" width=\"160\" height=\"104\" alt=\"\u5c01\u9762\u5360\u4f4d\u56fe \u6eda\u52a8\u52a8\u753b\" class=\"alignright size-medium\" loading=\"lazy\" \/><\/p>\n<p>CSS\u6eda\u52a8\u52a8\u753b\u6bd4\u6211\u9884\u60f3\u7684\u8981\u5f3a\u591a\u4e86\uff0c\u9879\u76ee\u4e2d\u53c8\u6709\u5f88\u591aJavaScript\u4ee3\u7801\u53ef\u4ee5\u6dd8\u6c70\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":[3],"tags":[531,1995,1992,1523,1535,1994,1996,1993,204,124],"_links":{"self":[{"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/posts\/11318"}],"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=11318"}],"version-history":[{"count":4,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/posts\/11318\/revisions"}],"predecessor-version":[{"id":11558,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/posts\/11318\/revisions\/11558"}],"wp:attachment":[{"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/media?parent=11318"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/categories?post=11318"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/tags?post=11318"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}