{"id":31134,"date":"2024-11-25T11:37:16","date_gmt":"2024-11-25T03:37:16","guid":{"rendered":"https:\/\/fwq.ai\/blog\/31134\/"},"modified":"2024-11-25T11:37:16","modified_gmt":"2024-11-25T03:37:16","slug":"%e5%be%ae%e4%bf%a1%e5%b0%8f%e7%a8%8b%e5%ba%8f%e7%bb%84%e4%bb%b6-marquee%e5%ae%9e%e4%be%8b%e5%88%86%e4%ba%ab","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/31134\/","title":{"rendered":"\u5fae\u4fe1\u5c0f\u7a0b\u5e8f\u7ec4\u4ef6 marquee\u5b9e\u4f8b\u5206\u4eab"},"content":{"rendered":"<p>\u672c\u6587\u4e3b\u8981\u4ecb\u7ecd\u4e86\u5fae\u4fe1\u5c0f\u7a0b\u5e8f\u7ec4\u4ef6 marquee\u5b9e\u4f8b\u8be6\u89e3\u7684\u76f8\u5173\u8d44\u6599,\u9700\u8981\u7684\u670b\u53cb\u53ef\u4ee5\u53c2\u8003\u4e0b\uff0c\u5e0c\u671b\u80fd\u5e2e\u52a9\u5230\u5927\u5bb6\u3002<\/p>\n<p><strong>\u5fae\u4fe1\u5c0f\u7a0b\u5e8f\u7ec4\u4ef6 marquee\u5b9e\u4f8b\u8be6\u89e3<\/strong><\/p>\n<p><strong>1. marquee\u6807\u7b7e<\/strong><\/p>\n<p>html\u662f\u6709marquee\u6807\u7b7e\u7684\uff0c\u53ef\u4ee5\u5b9e\u73b0\u8dd1\u9a6c\u706f\u6548\u679c\uff0c\u4f46\u5c0f\u7a0b\u5e8f\u6ca1\u6709\uff0c\u6240\u4ee5\u8981\u5b9e\u73b0\u3002\u8fd9\u91cc\u8003\u8651\u4f7f\u7528css3\u7684animation\u5b9e\u73b0\u3002<\/p>\n<p>html\u7684marquee\u662f\u8fd9\u6837\u4f7f\u7528\u7684\u3002<\/p>\n<p><\/p>\n<pre>&lt;marquee&gt;\r\n&nbsp;&nbsp;&nbsp;hello&nbsp;world\r\n&lt;\/marquee&gt;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>2. wxml<\/strong><\/p>\n<p><\/p>\n<pre>&lt;view&gt;\r\n&nbsp;&nbsp;&lt;view&gt;{{marquee.text}}&lt;\/view&gt;&lt;\/view&gt;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4f20\u5165wxml\u7684\u662f\u4e2ajson\u5bf9\u8c61<\/p>\n<p><\/p>\n<pre>marquee:{\r\n&nbsp;&nbsp;width:12,\r\n&nbsp;&nbsp;text:'hello&nbsp;world'\r\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u800c\u90a3\u4e2a\u5947\u602a\u7684&#8211;marqueeWidth\u662f\u7ed9@keyframes\u4f20\u7684\u53d8\u91cf\u3002\u5185\u8054\u8bbe\u7f6e\u53d8\u91cf\uff0ccss\u6587\u4ef6\u4e2d\u4e5f\u53ef\u4ee5\u83b7\u53d6\u5230\u8be5\u53d8\u91cf\u3002<\/p>\n<p><strong>3. wxss<\/strong><\/p>\n<p><\/p>\n<pre>@keyframes&nbsp;around&nbsp;{\r\n&nbsp;&nbsp;from&nbsp;{\r\n&nbsp;&nbsp;&nbsp;margin-left:&nbsp;100%;\r\n&nbsp;&nbsp;}\r\n&nbsp;&nbsp;to&nbsp;{\r\n&nbsp;&nbsp;&nbsp;margin-left:&nbsp;var(--marqueeWidth--);\/\/&nbsp;var\u63a5\u53d7\u4f20\u5165\u7684\u53d8\u91cf\r\n&nbsp;&nbsp;}\r\n&nbsp;}\r\n\r\n.marquee_container{\r\n&nbsp;background-color:&nbsp;#0099FF;\r\n&nbsp;height:&nbsp;1.2em;\r\n&nbsp;position:&nbsp;relative;\r\n&nbsp;width:&nbsp;100%;\r\n}\r\n.marquee_container:hover{\r\n&nbsp;animation-play-state:&nbsp;paused;&nbsp;\/\/&nbsp;\u4e0d\u8d77\u4f5c\u7528\r\n}\r\n.marquee_text{\r\n&nbsp;display:&nbsp;inline-block;\r\n&nbsp;white-space:&nbsp;nowrap;\r\n&nbsp;animation-name:&nbsp;around;\r\n&nbsp;animation-duration:&nbsp;5s;\r\n&nbsp;animation-iteration-count:&nbsp;infinite;\r\n&nbsp;animation-timing-function:linear;\r\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>4. js<\/strong><\/p>\n<p><\/p>\n<pre>export&nbsp;default&nbsp;{\r\n&nbsp;getWidth:(str)=&amp;gt;{\r\n&nbsp;&nbsp;return&nbsp;[].reduce.call(str,&nbsp;(pre,&nbsp;cur,&nbsp;index,&nbsp;arr)&nbsp;=&amp;gt;&nbsp;{\r\n&nbsp;&nbsp;&nbsp;if&nbsp;(str.charCodeAt(index)&nbsp;&amp;gt;&nbsp;255)&nbsp;{\/\/&nbsp;charCode\u5927\u4e8e255\u662f\u6c49\u5b57\r\n&nbsp;&nbsp;&nbsp;&nbsp;pre++;\r\n&nbsp;&nbsp;&nbsp;}&nbsp;else&nbsp;{\r\n&nbsp;&nbsp;&nbsp;&nbsp;pre&nbsp;+=&nbsp;0.5;\r\n&nbsp;&nbsp;&nbsp;}\r\n&nbsp;&nbsp;&nbsp;return&nbsp;pre;\r\n&nbsp;&nbsp;},&nbsp;0);\r\n&nbsp;},\r\n&nbsp;getDuration:(str)=&amp;gt;{\/\/&nbsp;\u4fdd\u7559\uff0c\u6839\u636e\u6587\u5b57\u957f\u5ea6\u8bbe\u7f6e\u65f6\u95f4\r\n&nbsp;&nbsp;return&nbsp;this.getWidth()\/10;\r\n&nbsp;}\r\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4ee5\u4e0a\u662f\u7ec4\u4ef6\u7684\u5c01\u88c5\u3002<\/p>\n<p><strong>5. \u4f7f\u7528<\/strong><\/p>\n<p><\/p>\n<pre>\/\/&nbsp;wxml\r\n&lt;include&gt;&lt;\/include&gt;\r\n\/\/&nbsp;wxss\r\n@import&nbsp;\"..\/component\/marquee\/marquee.wxss\";\r\n\/\/&nbsp;js\r\nimport&nbsp;marquee&nbsp;from&nbsp;'..\/component\/marquee\/marquee.js';\r\n\r\nvar&nbsp;options&nbsp;=&nbsp;Object.assign(marquee,&nbsp;{\r\n&nbsp;data:&nbsp;{\r\n&nbsp;&nbsp;motto:&nbsp;'Hello&nbsp;World',\r\n&nbsp;&nbsp;userInfo:&nbsp;{},\r\n&nbsp;&nbsp;marquee:&nbsp;{&nbsp;text:&nbsp;'\u4f60\u597d\uff0c\u4e2d\u56fd\uff01hello,world!'&nbsp;}\r\n&nbsp;},\r\n&nbsp;onLoad:&nbsp;function&nbsp;()&nbsp;{\r\n&nbsp;&nbsp;\/\/&nbsp;...\r\n\r\n&nbsp;&nbsp;const&nbsp;str&nbsp;=&nbsp;this.data.marquee.text;\r\n&nbsp;&nbsp;const&nbsp;width&nbsp;=&nbsp;this.getWidth(str);\r\n&nbsp;&nbsp;console.log('width',width);\r\n&nbsp;&nbsp;this.setData({&nbsp;[`${'marquee'}.width`]:&nbsp;width&nbsp;});\r\n&nbsp;}\r\n});\r\nPage(options);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u76f8\u5173\u63a8\u8350\uff1a<\/p>\n<\/p>\n<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u5fae\u4fe1\u5c0f\u7a0b\u5e8f\u7ec4\u4ef6 marquee\u5b9e\u4f8b\u5206\u4eab\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>\u672c\u6587\u4e3b\u8981\u4ecb\u7ecd\u4e86\u5fae\u4fe1\u5c0f\u7a0b\u5e8f\u7ec4\u4ef6 marquee\u5b9e\u4f8b\u8be6\u89e3\u7684\u76f8\u5173\u8d44\u6599,\u9700\u8981\u7684\u670b\u53cb\u53ef\u4ee5\u53c2\u8003\u4e0b\uff0c\u5e0c\u671b\u80fd\u5e2e\u52a9\u5230\u5927\u5bb6\u3002 \u5fae\u4fe1\u5c0f\u7a0b\u5e8f\u7ec4\u4ef6 marquee\u5b9e\u4f8b\u8be6\u89e3 1. marquee\u6807\u7b7e html\u662f\u6709marquee\u6807\u7b7e\u7684\uff0c\u53ef\u4ee5\u5b9e\u73b0\u8dd1\u9a6c\u706f\u6548\u679c\uff0c\u4f46\u5c0f\u7a0b\u5e8f\u6ca1\u6709\uff0c\u6240\u4ee5\u8981\u5b9e\u73b0\u3002\u8fd9\u91cc\u8003\u8651\u4f7f\u7528css3\u7684animation\u5b9e\u73b0\u3002 html\u7684marquee\u662f\u8fd9\u6837\u4f7f\u7528\u7684\u3002 &lt;marquee&gt; &nbsp;&nbsp;&nbsp;hello&nbsp;world &lt;\/marquee&gt; \u767b\u5f55\u540e\u590d\u5236 2. wxml &lt;view&gt; &nbsp;&nbsp;&lt;view&gt;{{marquee.text}}&lt;\/view&gt;&lt;\/view&gt; \u767b\u5f55\u540e\u590d\u5236 \u4f20\u5165wxml\u7684\u662f\u4e2ajson\u5bf9\u8c61 marquee:{ &nbsp;&nbsp;width:12, &nbsp;&nbsp;text:&#8217;hello&nbsp;world&#8217; } \u767b\u5f55\u540e\u590d\u5236 \u800c\u90a3\u4e2a\u5947\u602a\u7684&#8211;marqueeWidth\u662f\u7ed9@keyframes\u4f20\u7684\u53d8\u91cf\u3002\u5185\u8054\u8bbe\u7f6e\u53d8\u91cf\uff0ccss\u6587\u4ef6\u4e2d\u4e5f\u53ef\u4ee5\u83b7\u53d6\u5230\u8be5\u53d8\u91cf\u3002 3. wxss @keyframes&nbsp;around&nbsp;{ &nbsp;&nbsp;from&nbsp;{ &nbsp;&nbsp;&nbsp;margin-left:&nbsp;100%; &nbsp;&nbsp;} &nbsp;&nbsp;to&nbsp;{ &nbsp;&nbsp;&nbsp;margin-left:&nbsp;var(&#8211;marqueeWidth&#8211;);\/\/&nbsp;var\u63a5\u53d7\u4f20\u5165\u7684\u53d8\u91cf &nbsp;&nbsp;} &nbsp;} .marquee_container{ &nbsp;background-color:&nbsp;#0099FF; &nbsp;height:&nbsp;1.2em; &nbsp;position:&nbsp;relative; &nbsp;width:&nbsp;100%; } .marquee_container:hover{ &nbsp;animation-play-state:&nbsp;paused;&nbsp;\/\/&nbsp;\u4e0d\u8d77\u4f5c\u7528 } .marquee_text{ &nbsp;display:&nbsp;inline-block; &nbsp;white-space:&nbsp;nowrap; &nbsp;animation-name:&nbsp;around; &nbsp;animation-duration:&nbsp;5s; &nbsp;animation-iteration-count:&nbsp;infinite; &nbsp;animation-timing-function:linear; } \u767b\u5f55\u540e\u590d\u5236 4. js export&nbsp;default&nbsp;{ [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[19],"tags":[],"class_list":["post-31134","post","type-post","status-publish","format-standard","hentry","category-19"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/31134","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=31134"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/31134\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=31134"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=31134"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=31134"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}