{"id":2265,"date":"2024-11-10T13:28:53","date_gmt":"2024-11-10T05:28:53","guid":{"rendered":"https:\/\/fwq.ai\/blog\/2265\/"},"modified":"2024-11-10T13:28:53","modified_gmt":"2024-11-10T05:28:53","slug":"%e4%bd%bf%e7%94%a8-alpinejs-%e6%9e%84%e5%bb%ba%e5%b8%a6%e6%9c%89%e5%8f%af%e7%82%b9%e5%87%bb%e6%8e%a7%e4%bb%b6%e7%9a%84%e7%ae%80%e5%8d%95%e8%87%aa%e5%8a%a8%e6%92%ad%e6%94%be%e8%bd%ae%e6%92%ad","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/2265\/","title":{"rendered":"\u4f7f\u7528 Alpinejs \u6784\u5efa\u5e26\u6709\u53ef\u70b9\u51fb\u63a7\u4ef6\u7684\u7b80\u5355\u81ea\u52a8\u64ad\u653e\u8f6e\u64ad"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/001\/246\/273\/173070277597394.jpg\" class=\"aligncenter\" title=\"\u4f7f\u7528 Alpinejs \u6784\u5efa\u5e26\u6709\u53ef\u70b9\u51fb\u63a7\u4ef6\u7684\u7b80\u5355\u81ea\u52a8\u64ad\u653e\u8f6e\u64ad\u63d2\u56fe\" alt=\"\u4f7f\u7528 Alpinejs \u6784\u5efa\u5e26\u6709\u53ef\u70b9\u51fb\u63a7\u4ef6\u7684\u7b80\u5355\u81ea\u52a8\u64ad\u653e\u8f6e\u64ad\u63d2\u56fe\" \/><\/p>\n<p>\u8fd9\u662f\u4f7f\u7528 alpine.js \u521b\u5efa\u7b80\u5355\u8f6e\u64ad\u7684\u5206\u6b65\u793a\u4f8b\u3002 alpine.js \u662f\u4e00\u4e2a\u8f7b\u91cf\u7ea7 javascript \u6846\u67b6\uff0c\u63d0\u4f9b\u53cd\u5e94\u6027\uff0c\u53ef\u7528\u4e8e\u6784\u5efa\u4ea4\u4e92\u5f0f\u7ec4\u4ef6\uff0c\u800c\u65e0\u9700\u5927\u91cf javascript\u3002<\/p>\n<p>\u5728\u6b64\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u5c06\u521b\u5efa\u4e00\u4e2a\u57fa\u672c\u7684\u8f6e\u64ad\uff0c\u4e00\u6b21\u663e\u793a\u4e00\u5f20\u56fe\u50cf\uff0c\u5e76\u4f7f\u7528\u201c\u4e0a\u4e00\u9875\u201d\u548c\u201c\u4e0b\u4e00\u9875\u201d\u6309\u94ae\u6765\u6d4f\u89c8\u5b83\u4eec\u3002\u8ba9\u6211\u4eec\u5f00\u59cb\u5427\uff01<\/p>\n<h3> \u7b2c 1 \u6b65\uff1a\u8bbe\u7f6e html \u5e76\u5305\u542b alpine.js <\/h3>\n<p>\u9996\u5148\uff0c\u6211\u4eec\u5c06\u5728 html \u6587\u4ef6\u7684\u5934\u90e8\u5305\u542b alpine.js\u3002\u60a8\u53ef\u4ee5\u901a\u8fc7\u6dfb\u52a0\u4ee5\u4e0b\u811a\u672c\u6807\u7b7e\u6765\u505a\u5230\u8fd9\u4e00\u70b9\uff1a<\/p>\n<pre>&lt;!doctype html&gt;\n&lt;html lang=\"en\"&gt;\n&lt;head&gt;\n  &lt;meta charset=\"utf-8\"&gt;\n  &lt;meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\"&gt;\n  &lt;title&gt;alpine.js carousel&lt;\/title&gt;\n  &lt;script src=\"https:\/\/cdn.jsdelivr.net\/npm\/alpinejs@3.x.x\/dist\/cdn.min.js\" defer&gt;&lt;\/script&gt;\n  &lt;style&gt;\n    .carousel {\n      display: flex;\n      justify-content: center;\n      align-items: center;\n      width: 100%;\n      height: 300px;\n      overflow: hidden;\n      position: relative;\n    }\n    .carousel img {\n      width: 100%;\n      transition: opacity 0.5s ease-in-out;\n      opacity: 0;\n    }\n    .carousel img.active {\n      opacity: 1;\n    }\n  &lt;\/style&gt;\n&lt;\/head&gt;\n&lt;body&gt;\n<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<h3> \u7b2c2\u6b65\uff1a\u8f6e\u64ad\u7ec4\u4ef6\u7ed3\u6784 <\/h3>\n<p>\u5728body\u5185\u90e8\uff0c\u4e3acarousel\u7ec4\u4ef6\u521b\u5efa\u4e00\u4e2adiv\uff0c\u5e76\u4f7f\u7528x-data\u5bf9\u5176\u8fdb\u884c\u521d\u59cb\u5316\uff0c\u4ee5\u5b9a\u4e49alpine.js\u5c5e\u6027\u548c\u65b9\u6cd5\u3002<\/p>\n<pre>&lt;div x-data=\"carousel()\" class=\"carousel\"&gt;\n  &lt;!-- previous button --&gt;\n  &lt;button @click=\"prev\" class=\"absolute left-0 bg-gray-700 text-white px-3 py-2 rounded\"&gt;previous&lt;\/button&gt;\n\n  &lt;!-- carousel images --&gt;\n  &lt;template x-for=\"(image, index) in images\" :key=\"index\"&gt;\n    @@##@@\n  &lt;\/template&gt;\n\n  &lt;!-- next button --&gt;\n  &lt;button @click=\"next\" class=\"absolute right-0 bg-gray-700 text-white px-3 py-2 rounded\"&gt;next&lt;\/button&gt;\n&lt;\/div&gt;\n<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<h3> \u7b2c 3 \u6b65\uff1a\u5b9a\u4e49 alpine.js \u6570\u636e\u548c\u65b9\u6cd5 <\/h3>\n<p>\u73b0\u5728\u6211\u4eec\u5c06\u5728 alpine \u7ec4\u4ef6\u4e2d\u5b9a\u4e49\u8f6e\u64ad\u529f\u80fd\uff0c\u8bbe\u7f6e\u7528\u4e8e\u5bfc\u822a\u56fe\u50cf\u7684\u521d\u59cb\u6570\u636e\u548c\u65b9\u6cd5\u3002<\/p>\n<pre>&lt;script&gt;\n  function carousel() {\n    return {\n      currentIndex: 0, \/\/ Track the index of the current image\n      images: [\n        'https:\/\/via.placeholder.com\/600x300?text=Slide+1',\n        'https:\/\/via.placeholder.com\/600x300?text=Slide+2',\n        'https:\/\/via.placeholder.com\/600x300?text=Slide+3',\n      ],\n      interval: null,\n      startAutoPlay() {\n          this.interval = setInterval(() =&gt; {\n              this.next();\n          }, 3000); \/\/ Change every 3 seconds\n      },\n      stopAutoPlay() {\n          clearInterval(this.interval);\n      },\n      \/\/ Method to go to the next image\n      next() {\n        this.currentIndex = (this.currentIndex + 1) % this.images.length;\n      },\n      \/\/ Method to go to the previous image\n      prev() {\n        this.currentIndex = (this.currentIndex - 1 + this.images.length) % this.images.length;\n      },\n      init() {\n          this.startAutoPlay();\n      }\n    };\n  }\n&lt;\/script&gt;\n<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<h3> \u7ec4\u4ef6\u8bf4\u660e <\/h3>\n<ol>\n<li>\n<p><strong>\u8f6e\u64ad html \u7ed3\u6784<\/strong>:<\/p>\n<ul>\n<li> <strong>\u4e0a\u4e00\u4e2a\u6309\u94ae<\/strong>\uff1a\u5355\u51fb\u65f6\uff0c\u5b83\u4f1a\u8c03\u7528 prev \u65b9\u6cd5\u5bfc\u822a\u5230\u4e0a\u4e00\u4e2a\u56fe\u50cf\u3002<\/li>\n<li> <strong>images<\/strong>\uff1a\u6211\u4eec\u4f7f\u7528 x-for \u5faa\u73af\u904d\u5386\u56fe\u50cf\u5e76\u7ed1\u5b9a src \u5c5e\u6027\u3002 :class \u7ed1\u5b9a\u5c06\u6d3b\u52a8\u7c7b\u5e94\u7528\u4e8e\u5f53\u524d\u56fe\u50cf\uff0c\u4f7f\u5176\u53ef\u89c1\u3002<\/li>\n<li> <strong>\u4e0b\u4e00\u4e2a\u6309\u94ae<\/strong>\uff1a\u5355\u51fb\u65f6\uff0c\u5b83\u4f1a\u8c03\u7528\u4e0b\u4e00\u4e2a\u65b9\u6cd5\u6765\u5bfc\u822a\u5230\u4e0b\u4e00\u4e2a\u56fe\u50cf\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>alpine.js \u6570\u636e\u548c\u65b9\u6cd5<\/strong>:<\/p>\n<ul>\n<li> <strong>currentindex<\/strong>\uff1a\u8ddf\u8e2a\u5f53\u524d\u663e\u793a\u7684\u56fe\u50cf\u3002<\/li>\n<li> <strong>images<\/strong>\uff1a\u5305\u542b\u8f6e\u64ad\u56fe\u50cf url \u7684\u6570\u7ec4\u3002<\/li>\n<li> <strong>startautoplay() \u548c stopautoplay()<\/strong>\uff1a\u4ee5 3 \u79d2\u7684\u95f4\u9694\u5f00\u59cb\u548c\u505c\u6b62\u81ea\u52a8\u64ad\u653e\u3002<\/li>\n<li> <strong>next()<\/strong>\uff1a\u589e\u52a0 currentindex\u3002\u5982\u679c\u8d85\u8fc7\u56fe\u50cf\u6570\u91cf\uff0c\u5219\u4f1a\u91cd\u7f6e\u5230\u5f00\u5934\u3002<\/li>\n<li> <strong>prev()<\/strong>\uff1a\u9012\u51cf currentindex\u3002\u5982\u679c\u5b83\u4f4e\u4e8e\u96f6\uff0c\u5b83\u5c06\u8fd4\u56de\u5230\u6700\u540e\u4e00\u5f20\u56fe\u50cf\u3002<\/li>\n<li> <strong>init()<\/strong>\uff1a\u8f6e\u64ad\u521d\u59cb\u5316\u65f6\u5f00\u59cb\u81ea\u52a8\u64ad\u653e\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<h3> \u7b2c 4 \u6b65\uff1a\u8bbe\u7f6e\u8f6e\u64ad\u6837\u5f0f <\/h3>\n<p>\u6211\u4eec\u4e3a\u8f6e\u64ad\u6dfb\u52a0\u4e86\u57fa\u672c\u7684  \u6837\u5f0f\u4ee5\u53ca\u7528\u4e8e\u5b9a\u4f4d\u548c\u53ef\u89c1\u6027\u7684\u6309\u94ae\u3002 css \u8fc7\u6e21\u4e3a\u56fe\u50cf\u63d0\u4f9b\u6de1\u5165\u6548\u679c\u3002<\/p>\n<h3> \u7b2c 5 \u6b65\uff1a\u81ea\u52a8\u64ad\u653e\u548c\u53ef\u70b9\u51fb\u63a7\u4ef6 <\/h3>\n<ul>\n<li> <strong>\u81ea\u52a8\u64ad\u653e<\/strong>\uff1a\u4f7f\u7528 init() \u4e2d\u7684 startautoplay() \u81ea\u52a8\u64ad\u653e\u3002<\/li>\n<li> <strong>\u5355\u51fb\u63a7\u4ef6<\/strong>\uff1a\u6309\u94ae\u89e6\u53d1 prev() \u548c next() \u51fd\u6570\u6765\u5bfc\u822a\u5e7b\u706f\u7247\u3002<\/li>\n<\/ul>\n<h3> \u6982\u62ec <\/h3>\n<ul>\n<li> <strong>alpine.js<\/strong> \u7528\u4e8e\u4ea4\u4e92\uff0c\u4f7f\u8f6e\u64ad\u53d8\u5f97\u8f7b\u91cf\u7ea7\u4e14\u54cd\u5e94\u7075\u654f\u3002<\/li>\n<li> <strong>css \u8fc7\u6e21<\/strong> \u968f\u7740\u56fe\u50cf\u53d8\u5316\u521b\u5efa\u6de1\u5165\u6de1\u51fa\u6548\u679c\u3002<\/li>\n<li> <strong>\u6309\u94ae\u70b9\u51fb<\/strong>\u89e6\u53d1alpine\u65b9\u6cd5\u4ee5\u65b9\u4fbf\u5bfc\u822a\u3002<\/li>\n<\/ul>\n<p>\u6b64\u793a\u4f8b\u63d0\u4f9b\u81ea\u52a8\u64ad\u653e\u529f\u80fd\u548c\u53ef\u70b9\u51fb\u63a7\u4ef6\uff0c\u4f7f\u8f6e\u64ad\u5177\u6709\u4ea4\u4e92\u6027\u3002\u5982\u679c\u60a8\u9700\u8981\u8fdb\u4e00\u6b65\u5b9a\u5236\u6216\u9644\u52a0\u529f\u80fd\uff0c\u8bf7\u544a\u8bc9\u6211\uff01<\/p>\n<p>\u4e0e\u6211\u8054\u7cfb\uff1a@ linkedin \u5e76\u67e5\u770b\u6211\u7684\u4f5c\u54c1\u96c6\u3002<\/p>\n<p>\u8bf7\u7ed9\u6211\u7684 hub \u9879\u76ee\u4e00\u9897\u661f \u2b50\ufe0f<\/p>\n<p>\u6e90\u4ee3\u7801<\/p>\n<p><img> <\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u4f7f\u7528 Alpinejs \u6784\u5efa\u5e26\u6709\u53ef\u70b9\u51fb\u63a7\u4ef6\u7684\u7b80\u5355\u81ea\u52a8\u64ad\u653e\u8f6e\u64ad\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>\u8fd9\u662f\u4f7f\u7528 alpine.js \u521b\u5efa\u7b80\u5355\u8f6e\u64ad\u7684\u5206\u6b65\u793a\u4f8b\u3002 alpine.js \u662f\u4e00\u4e2a\u8f7b\u91cf\u7ea7 javascript \u6846\u67b6\uff0c\u63d0\u4f9b\u53cd\u5e94\u6027\uff0c\u53ef\u7528\u4e8e\u6784\u5efa\u4ea4\u4e92\u5f0f\u7ec4\u4ef6\uff0c\u800c\u65e0\u9700\u5927\u91cf javascript\u3002 \u5728\u6b64\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u5c06\u521b\u5efa\u4e00\u4e2a\u57fa\u672c\u7684\u8f6e\u64ad\uff0c\u4e00\u6b21\u663e\u793a\u4e00\u5f20\u56fe\u50cf\uff0c\u5e76\u4f7f\u7528\u201c\u4e0a\u4e00\u9875\u201d\u548c\u201c\u4e0b\u4e00\u9875\u201d\u6309\u94ae\u6765\u6d4f\u89c8\u5b83\u4eec\u3002\u8ba9\u6211\u4eec\u5f00\u59cb\u5427\uff01 \u7b2c 1 \u6b65\uff1a\u8bbe\u7f6e html \u5e76\u5305\u542b alpine.js \u9996\u5148\uff0c\u6211\u4eec\u5c06\u5728 html \u6587\u4ef6\u7684\u5934\u90e8\u5305\u542b alpine.js\u3002\u60a8\u53ef\u4ee5\u901a\u8fc7\u6dfb\u52a0\u4ee5\u4e0b\u811a\u672c\u6807\u7b7e\u6765\u505a\u5230\u8fd9\u4e00\u70b9\uff1a &lt;!doctype html&gt; &lt;html lang=&#8221;en&#8221;&gt; &lt;head&gt; &lt;meta charset=&#8221;utf-8&#8243;&gt; &lt;meta name=&#8221;viewport&#8221; content=&#8221;width=device-width, initial-scale=1.0&#8243;&gt; &lt;title&gt;alpine.js carousel&lt;\/title&gt; &lt;script src=&#8221;https:\/\/cdn.jsdelivr.net\/npm\/alpinejs@3.x.x\/dist\/cdn.min.js&#8221; defer&gt;&lt;\/script&gt; &lt;style&gt; .carousel { display: flex; justify-content: center; align-items: center; width: 100%; height: 300px; overflow: hidden; position: relative; } .carousel img [&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-2265","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/2265","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=2265"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/2265\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=2265"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=2265"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=2265"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}