{"id":2495,"date":"2024-11-10T12:16:26","date_gmt":"2024-11-10T04:16:26","guid":{"rendered":"https:\/\/fwq.ai\/blog\/2495\/"},"modified":"2024-11-10T12:16:26","modified_gmt":"2024-11-10T04:16:26","slug":"%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8-javascript-%e6%a8%a1%e6%8b%9f-css-sticky-%e6%95%88%e6%9e%9c%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/2495\/","title":{"rendered":"\u5982\u4f55\u4f7f\u7528 JavaScript \u6a21\u62df CSS sticky \u6548\u679c\uff1f"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/001\/246\/273\/173077730582372.jpg\" class=\"aligncenter\" title=\"\u5982\u4f55\u4f7f\u7528 JavaScript \u6a21\u62df CSS sticky \u6548\u679c\uff1f\u63d2\u56fe\" alt=\"\u5982\u4f55\u4f7f\u7528 JavaScript \u6a21\u62df CSS sticky \u6548\u679c\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>\u6a21\u62df  \u7684 sticky \u6548\u679c<\/strong><\/p>\n<p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u5e0c\u671b\u5728\u9875\u9762\u4e0a\u5b9e\u73b0\u7c7b\u4f3c css sticky \u7684\u6548\u679c\uff0c\u5728\u9875\u9762\u5411\u4e0b\u6eda\u52a8\u65f6\uff0c\u67d0\u4e9b\u5143\u7d20\u53ef\u4ee5\u56fa\u5b9a\u5728\u9875\u9762\u9876\u90e8\u6216\u5e95\u90e8\u3002\u867d\u7136 css \u4e2d\u7684 sticky \u5c5e\u6027\u53ef\u4ee5\u5b9e\u73b0\u6b64\u6548\u679c\uff0c\u4f46\u5b83\u5e76\u4e0d\u9002\u7528\u4e8e\u6240\u6709\u6d4f\u89c8\u5668\u3002\u53ef\u4ee5\u901a\u8fc7 javascript \u6765\u6a21\u62df\u8fd9\u79cd sticky \u6548\u679c\u3002<\/p>\n<p><strong>\u73b0\u6709\u5e93<\/strong><\/p>\n<p>\u6709\u4e00\u79cd\u73b0\u6210\u7684\u5e93\u79f0\u4e3a stickyfill\uff0c\u53ef\u4ee5\u7528\u6765\u5b9e\u73b0 sticky \u6548\u679c\u3002\u8be5\u5e93\u5f00\u6e90\uff0c\u53ef\u4ee5\u67e5\u770b\u5176\u6e90\u7801\u4e86\u89e3\u5b9e\u73b0\u7ec6\u8282\u3002<\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<p><strong>\u6a21\u62df\u6b65\u9aa4<\/strong><\/p>\n<p>\u4f7f\u7528 stickyfill \u5e93\u6a21\u62df css sticky \u6548\u679c\u7684\u6b65\u9aa4\u5982\u4e0b\uff1a<\/p>\n<ol>\n<li> <strong>\u6ce8\u91ca\u6389 css sticky \u58f0\u660e\uff1a<\/strong>\u5728 css \u6837\u5f0f\u4e2d\uff0c\u6ce8\u91ca\u6389 .r \u5143\u7d20\u7684 position: sticky \u58f0\u660e\u3002<\/li>\n<li> <strong>\u5f15\u5165 stickyfill \u5e93\uff1a<\/strong>\u5728 html \u9875\u9762\u4e2d\uff0c\u5f15\u5165 stickyfill \u5e93\u3002<\/li>\n<li> <strong>\u5f3a\u5236\u5f00\u542f polyfill\uff1a<\/strong>\u4f7f\u7528 stickyfill.forcesticky() \u65b9\u6cd5\u5f3a\u5236\u5f00\u542f polyfill\u3002\uff08\u5728\u652f\u6301 css sticky \u7684\u6d4f\u89c8\u5668\u4e2d\uff0c\u5982\u679c\u4e0d\u5f00\u542f polyfill\uff0c\u6a21\u62df\u6548\u679c\u5c06\u4e0d\u4f1a\u751f\u6548\u3002\uff09<\/li>\n<li> <strong>\u6307\u5b9a\u6a21\u62df\u5143\u7d20\uff1a<\/strong>\u4f7f\u7528 stickyfill.addone() \u65b9\u6cd5\u6307\u5b9a\u9700\u8981\u6a21\u62df sticky \u6548\u679c\u7684\u5143\u7d20\uff08.r \u5143\u7d20\uff09\u3002<\/li>\n<\/ol>\n<p><strong>\u793a\u4f8b<\/strong><\/p>\n<p>\u4ee5\u4e0b\u4ee3\u7801\u5c55\u793a\u4e86\u5982\u4f55\u4f7f\u7528 stickyfill \u6765\u6a21\u62df css sticky \u6548\u679c\uff1a<\/p>\n<pre>&lt;style&gt;\n  \/\/ \u524d\u9762\u7565\n\n  .r {\n    background-color: burlywood;\n    \/\/ position: sticky; \/\/ \u6ce8\u91ca\u6389\n    top: 0;\n  }\n&lt;\/style&gt;\n&lt;script src=\"https:\/\/unpkg.com\/stickyfilljs@2.1.0\/dist\/stickyfill.js\"&gt;&lt;\/script&gt;\n&lt;div class=\"container\"&gt;\n  &lt;div class=\"l\"&gt;\u7565&lt;\/div&gt;\n  &lt;div&gt;\n    &lt;div class=\"r\"&gt;\u7565&lt;\/div&gt;\n  &lt;\/div&gt;\n&lt;\/div&gt;\n&lt;script&gt;\n  Stickyfill.forceSticky(); \/\/ \u6ce8\u610f\u9700\u8981\u5f3a\u5236\u5f00\u542f polyfill\n  Stickyfill.addOne(document.querySelector('.r'));\n&lt;\/script&gt;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u5982\u4f55\u4f7f\u7528 JavaScript \u6a21\u62df CSS sticky \u6548\u679c\uff1f\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6a21\u62df \u7684 sticky \u6548\u679c \u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u5e0c\u671b\u5728\u9875\u9762\u4e0a\u5b9e\u73b0\u7c7b\u4f3c css sticky \u7684\u6548\u679c\uff0c\u5728\u9875\u9762\u5411\u4e0b\u6eda\u52a8\u65f6\uff0c\u67d0\u4e9b\u5143\u7d20\u53ef\u4ee5\u56fa\u5b9a\u5728\u9875\u9762\u9876\u90e8\u6216\u5e95\u90e8\u3002\u867d\u7136 css \u4e2d\u7684 sticky \u5c5e\u6027\u53ef\u4ee5\u5b9e\u73b0\u6b64\u6548\u679c\uff0c\u4f46\u5b83\u5e76\u4e0d\u9002\u7528\u4e8e\u6240\u6709\u6d4f\u89c8\u5668\u3002\u53ef\u4ee5\u901a\u8fc7 javascript \u6765\u6a21\u62df\u8fd9\u79cd sticky \u6548\u679c\u3002 \u73b0\u6709\u5e93 \u6709\u4e00\u79cd\u73b0\u6210\u7684\u5e93\u79f0\u4e3a stickyfill\uff0c\u53ef\u4ee5\u7528\u6765\u5b9e\u73b0 sticky \u6548\u679c\u3002\u8be5\u5e93\u5f00\u6e90\uff0c\u53ef\u4ee5\u67e5\u770b\u5176\u6e90\u7801\u4e86\u89e3\u5b9e\u73b0\u7ec6\u8282\u3002 \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b \u6a21\u62df\u6b65\u9aa4 \u4f7f\u7528 stickyfill \u5e93\u6a21\u62df css sticky \u6548\u679c\u7684\u6b65\u9aa4\u5982\u4e0b\uff1a \u6ce8\u91ca\u6389 css sticky \u58f0\u660e\uff1a\u5728 css \u6837\u5f0f\u4e2d\uff0c\u6ce8\u91ca\u6389 .r \u5143\u7d20\u7684 position: sticky \u58f0\u660e\u3002 \u5f15\u5165 stickyfill \u5e93\uff1a\u5728 html \u9875\u9762\u4e2d\uff0c\u5f15\u5165 stickyfill \u5e93\u3002 \u5f3a\u5236\u5f00\u542f polyfill\uff1a\u4f7f\u7528 stickyfill.forcesticky() \u65b9\u6cd5\u5f3a\u5236\u5f00\u542f polyfill\u3002\uff08\u5728\u652f\u6301 css sticky \u7684\u6d4f\u89c8\u5668\u4e2d\uff0c\u5982\u679c\u4e0d\u5f00\u542f [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-2495","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/2495","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=2495"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/2495\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=2495"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=2495"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=2495"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}