{"id":1937,"date":"2023-03-25T14:26:38","date_gmt":"2023-03-25T06:26:38","guid":{"rendered":""},"modified":"2023-03-25T14:26:38","modified_gmt":"2023-03-25T06:26:38","slug":"ES6 \u52a8\u753b","status":"publish","type":"post","link":"https:\/\/bianchenghao6.com\/1937.html","title":{"rendered":"ES6 \u52a8\u753b"},"content":{"rendered":"


\n <\/head>
\n <\/p>\n

\n

ES6 \u52a8\u753b<\/h1>\n<\/p><\/div>\n

ES6 \u52a8\u753b<\/h2>\n
\n JavaScript \u4e2d\u7684\u52a8\u753b\u53ef\u4ee5\u5904\u7406 CSS \u65e0\u6cd5\u5904\u7406\u7684\u4e8b\u60c5\u3002 JavaScript \u4e2d\u7684\u51e0\u4e2a\u5143\u7d20\u6709\u52a9\u4e8e\u521b\u5efa\u590d\u6742\u7684\u52a8\u753b\uff0c\u4f8b\u5982\u6de1\u5165\u6de1\u51fa\u6548\u679c\u3001\u70df\u82b1\u3001\u6eda\u5165\u6216\u6edaout, \u7b49\u3002\u901a\u8fc7\u4f7f\u7528 JavaScript\uff0c\u6211\u4eec\u53ef\u4ee5\u79fb\u52a8 DOM \u5143\u7d20\uff0c\u4f8b\u5982 <\/img>\u3001<\/div> \u6216\u4efb\u4f55\u5176\u4ed6 HTML \u5143\u7d20\u3002\n <\/div>\n
\n \u90a3\u91ccJavaScript\u4e2d\u6267\u884c\u52a8\u753b\u7684\u4e24\u79cd\u65b9\u5f0f\uff0c\u5206\u522b\u5982\u4e0b:\n <\/div>\n

\u4f7f\u7528 setInterval() \u65b9\u6cd5: \u5b83\u63a5\u53d7\u4e24\u4e2a\u53c2\u6570\uff0c\u4e00\u4e2a\u51fd\u6570\u548c\u4e00\u4e2a\u6574\u6570\u3002\u6b64\u65b9\u6cd5\u4f7f\u7528 clearInterval() \u65b9\u6cd5\u7ec8\u6b62\u3002<\/span>
\n \u4f7f\u7528 requestAnimationFrame() \u65b9\u6cd5: \u5f53\u5c4f\u5e55\u51c6\u5907\u597d\u63a5\u53d7\u4e0b\u4e00\u6b21\u91cd\u7ed8\u65f6\uff0c\u6b64\u65b9\u6cd5\u8fd0\u884c\u8be5\u51fd\u6570\u3002\u5b83\u9700\u8981\u4e00\u4e2a\u53c2\u6570\u51fd\u6570\u3002\u5f53\u6211\u4eec\u9012\u5f52\u8c03\u7528\u8fd9\u4e2a\u65b9\u6cd5\u65f6\uff0c\u52a8\u753b\u6548\u679c\u5c31\u4f1a\u53d1\u751f\u3002\u9884\u671f\u7684\u52a8\u753b\u662f\u9010\u5e27\u521b\u5efa\u7684\uff0c\u5e76\u4e14\u5728\u6d4f\u89c8\u5668\u51c6\u5907\u597d\u65f6\u8c03\u7528\u6bcf\u4e00\u5e27\u3002<\/span> <\/p>\n

\n \u8ba9\u6211\u4eec\u8bd5\u7740\u8be6\u7ec6\u8bf4\u660e\u4e00\u4e0b\u8fd9\u4e9b\u65b9\u6cd5\u3002\n <\/div>\n

setInterval() \u65b9\u6cd5<\/h2>\n
\n \u5b83\u662fJavaScript\u5236\u4f5c\u52a8\u753b\u6548\u679c\u7684\u4f20\u7edf\u65b9\u6cd5.\u5b83\u4f9d\u8d56\u4e8e\u4ee3\u7801\u7684\u91cd\u590d\u6267\u884c\u6765\u9010\u5e27\u66f4\u6539\u5143\u7d20\u3002\n <\/div>\n
\n
anime = setInterval(show, t);\n\/\/It calls the function show after every t milliseconds<\/span>\nclearInterval(anime);\n\/\/It terminates above process<\/span><\/pre>\n

<\/code>\n <\/div>\n

\n \u5982\u679c\u51fd\u6570show\u4ea7\u751f\u5143\u7d20\u6837\u5f0f\u7684\u53d8\u5316\uff0c\u90a3\u4e48setInterval(show, t)\u65b9\u6cd5\u53ef\u7528\u4e8e\u4ea7\u751f\u5143\u7d20\u6837\u5f0f\u7684\u6e10\u53d8\u5728\u6bcf\u4e2a\u65f6\u95f4\u95f4\u9694\u4e4b\u540e\u3002\u5f53\u65f6\u95f4\u95f4\u9694\u8f83\u5c0f\u65f6\uff0c\u52a8\u753b\u770b\u8d77\u6765\u662f\u8fde\u7eed\u7684\u3002\n <\/div>\n

requestAnimationFrame() \u65b9\u6cd5<\/h2>\n
\n \u6b64\u65b9\u6cd5\u8bbe\u7f6e\u5bb9\u6613\uff0c\u53d6\u6d88\u96be\u3002\u5b83\u7ecf\u8fc7\u4f18\u5316\u4ee5\u6267\u884c\u6d41\u7545\u7684\u52a8\u753b\u3002\u5176\u4e2d\u7684\u5faa\u73af\u9700\u8981\u624b\u52a8\u521b\u5efa\uff0c\u8ba1\u65f6\u4e5f\u9700\u8981\u624b\u52a8\u8bbe\u7f6e\u3002\u6b64\u65b9\u6cd5\u4e0d\u662f\u4e3a\u4e86\u5728\u7279\u5b9a\u65f6\u95f4\u95f4\u9694\u4f7f\u7528\u3002\n <\/div>\n
\n \u6b64\u65b9\u6cd5\u65e8\u5728\u4ee5 60fps(\u6bcf\u79d2\u5e27\u6570) \u5faa\u73af\u4ee5\u6267\u884c\u6d41\u7545\u7684\u52a8\u753b\u3002\u5b83\u4e0d\u4f1a\u5728\u540e\u53f0\u8fd0\u884c\uff0c\u800c\u4e14\u4e5f\u5f88\u8282\u80fd\u3002\n <\/div>\n
\n \u73b0\u5728\uff0c\u8ba9\u6211\u4eec\u770b\u770b JavaScript \u52a8\u753b\u7684\u4e00\u4e9b\u6f14\u793a\u3002\n <\/div>\n
\n Example-1\n <\/div>\n
\n \u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u901a\u8fc7\u4f7f\u7528 DOM \u5bf9\u8c61\u7684\u5c5e\u6027\u548c JavaScript \u7684\u51fd\u6570\u6765\u5b9e\u73b0\u4e00\u4e2a\u7b80\u5355\u7684\u52a8\u753b\u3002\u6211\u4eec\u4f7f\u7528 JavaScript \u51fd\u6570 getElementById() \u6765\u83b7\u53d6 DOM \u5bf9\u8c61\uff0c\u7136\u540e\u5c06\u8be5\u5bf9\u8c61\u5206\u914d\u7ed9\u4e00\u4e2a\u5168\u5c40\u53d8\u91cf\u3002\n <\/div>\n
\n \u8ba9\u6211\u4eec\u901a\u8fc7\u4ee5\u4e0b\u793a\u4f8b\u6765\u7406\u89e3\u7b80\u5355\u7684\u52a8\u753b\u3002\n <\/div>\n
\n \u793a\u4f8b\n <\/div>\n
\n
<html> \n   <head> \n      <script type = \"text\/javascript\"<\/span>> \n    \n            var <\/span>img = null<\/span>; \n            function init(){  \n               img = document.getElementById<\/span>('myimg'<\/span>);\n               img.style.position = 'relative'<\/span>;     \n               img.style.left = '50px'<\/span>;   \n            }     \n            function moveRight(){  \n               img.style.left = parseInt<\/span>(\n               img.style.left) + 100 + 'px'<\/span>;  \n            }  \n            window.onload = init;  \n   \n      <\/script> \n   <\/head> \n   \n   <body> \n      <form> \n         <img id = \"myimg\"<\/span> src = \"train1.png\"<\/span> \/> \n         <center>\n            <p>Click the below button to move the image right<\/p> \n         <input type = \"button\"<\/span> value = \"Click Me\"<\/span> onclick = \"moveRight();\"<\/span> \/>\n      <\/center> \n      <\/form>\n   <\/body>\n   \n<\/html><\/pre>\n

<\/code>\n <\/div>\n

\n Example-2\n <\/div>\n
\n \u8ba9\u6211\u4eec\u4e86\u89e3\u53e6\u4e00\u4e2a JavaScript \u52a8\u753b\u7684\u4f8b\u5b50\u3002\n <\/div>\n
\n \u5728\u8fd9\u4e2a\u52a8\u753b\u4e2d\uff0c\u6211\u4eec\u5c06\u4f7f\u7528 setTimeout() JavaScript \u7684\u529f\u80fd\u3002\u5f88\u660e\u663e\uff0c\u5982\u679c\u6211\u4eec\u4f7f\u7528setTimeout()\u51fd\u6570\uff0c\u90a3\u4e48\u8981\u6e05\u9664\u8ba1\u65f6\u5668\uff0c\u6211\u4eec\u5fc5\u987b\u624b\u52a8\u8bbe\u7f6eJavaScript\u7684clearTimeout()\u51fd\u6570\u3002\n <\/div>\n
\n \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u770b\u5230\u4e86\u56fe\u50cf\u5982\u4f55\u5728\u6bcf\u6b21\u70b9\u51fb\u65f6\u5411\u53f3\u79fb\u52a8\u3002\u8ba9\u6211\u4eec\u5c1d\u8bd5\u4f7f\u7528 JavaScript \u7684 setTimeout() \u51fd\u6570\u81ea\u52a8\u6267\u884c\u6b64\u8fc7\u7a0b\u3002\n <\/div>\n
\n
<html> \n   <head> \n      <title>JavaScript Animation<\/title> \n      <script type = \"text\/javascript\"<\/span>> \n            var <\/span>anime ; \n            function init(){  \n               img = document.getElementById<\/span>('myimg'<\/span>);     \n               img.style.position = 'relative'<\/span>;    \n               img.style.left = '0px'<\/span><\/span>; \n            }  \n            function towardRight(){  \n               img.style.left = parseInt<\/span>(img.style.left) + 10 + 'px'<\/span>;    \n               anime = setTimeout(towardRight,50); \n            }  \n            function stop() {     \n               clearTimeout(anime);    \n              img.style.left = '0px';   \n            }  \n            window.onload = init;  \n      <\/script> \n   <\/head> \n   <body> \n      <form> \n         <img id = \"myimg\"<\/span> src = \"train1.png\"<\/span> \/> \n         <center>\n         <h2 style=\"color:darkblue;\"<\/span>>Click the following buttons to handle animation<\/h2> \n         <input type=\"button\"<\/span><\/span> value=\"Start\"<\/span> onclick = \"towardRight();\"<\/span> \/> \n         <input type = \"button\" value=\"Stop\"<\/span> onclick = \"stop();\"<\/span> \/> \n         <center>\n      <\/form>    \n   <\/body> \n<\/html><\/pre>\n

<\/code>\n <\/div>\n

\u9f20\u6807\u4e8b\u4ef6\u7684\u56fe\u50cf\u7ffb\u8f6c<\/h2>\n
\n \u8ba9\u6211\u4eec\u4e86\u89e3\u53e6\u4e00\u4e2a\u52a8\u753b\u793a\u4f8b\uff0c\u5176\u4e2d\u9f20\u6807\u4e8b\u4ef6\u5bfc\u81f4\u56fe\u50cf\u7ffb\u8f6c\u3002\u5f53\u9f20\u6807\u79fb\u5230\u56fe\u50cf\u4e0a\u65f6\uff0cHTTP \u56fe\u50cf\u5c06\u4ece\u7b2c\u4e00\u4e2a\u56fe\u50cf\u53d8\u4e3a\u7b2c\u4e8c\u4e2a\u56fe\u50cf\u3002\u4f46\u662f\u5f53\u9f20\u6807\u79fb\u79bb\u56fe\u50cf\u65f6\uff0c\u5c06\u663e\u793a\u539f\u59cb\u56fe\u50cf\u3002\n <\/div>\n
\n \u793a\u4f8b\n <\/div>\n
\n onMouseOver\u5f53\u7528\u6237\u5c06\u9f20\u6807\u79fb\u5230\u94fe\u63a5\u4e0a\u65f6\u89e6\u53d1\u4e8b\u4ef6\u5904\u7406\u7a0b\u5e8f\uff0c\u5f53\u7528\u6237\u5c06\u9f20\u6807\u79fb\u79bb\u94fe\u63a5\u65f6\u89e6\u53d1 onMouseOut \u4e8b\u4ef6\u5904\u7406\u7a0b\u5e8f\u3002\n <\/div>\n
\n
<html>\n   \n   <head>\n         \n      <script type = \"text\/javascript\"<\/span>>\n            if(document.images) {\n               var <\/span>img1 = new <\/span>Image();     \n               img1.src = \"cat.jpg\"<\/span><\/span>;\n               var <\/span>img2 = new <\/span>Image();     \n               img2.src = \"tiger.jpg\"<\/span>;\n            }\n      <\/script>\n   <\/head>\n   \n   <body>\n   <center>\n      \n      <a href = \"#\"<\/span> onMouseOver = \"document.myImg.src = img2.src;\"<\/span><\/span>\n         onMouseOut = \"document.myImg.src = img1.src;\"<\/span>>\n         <img name = \"myImg\"<\/span> src = \"cat.jpg\" \/>\n      <\/a><br><br><br>\n      <h1>Move your mouse over the image to see the result<\/h1>\n   <\/center>\n   <\/body>\n<\/html><\/pre>\n

<\/code>\n <\/div>\n

<\/body>
\n<\/html><\/p>\n","protected":false},"excerpt":{"rendered":"ES6 \u52a8\u753bzh-cn","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[161],"tags":[],"class_list":["post-1937","post","type-post","status-publish","format-standard","hentry","category-es6jc"],"_links":{"self":[{"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/posts\/1937"}],"collection":[{"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/comments?post=1937"}],"version-history":[{"count":0,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/posts\/1937\/revisions"}],"wp:attachment":[{"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/media?parent=1937"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/categories?post=1937"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/tags?post=1937"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}