{"id":31376,"date":"2024-11-25T08:03:54","date_gmt":"2024-11-25T00:03:54","guid":{"rendered":"https:\/\/fwq.ai\/blog\/31376\/"},"modified":"2024-11-25T08:03:54","modified_gmt":"2024-11-25T00:03:54","slug":"js%e8%bf%90%e5%8a%a8%e7%bc%93%e5%86%b2%e6%95%88%e6%9e%9c%e7%9a%84%e5%b0%81%e8%a3%85%e5%87%bd%e6%95%b0%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/31376\/","title":{"rendered":"JS\u8fd0\u52a8\u7f13\u51b2\u6548\u679c\u7684\u5c01\u88c5\u51fd\u6570\u5982\u4f55\u4f7f\u7528"},"content":{"rendered":"<p style=\"text-align: left\">\u8fd9\u6b21\u7ed9\u5927\u5bb6\u5e26\u6765JS\u8fd0\u52a8\u7f13\u51b2\u6548\u679c\u7684\u5c01\u88c5\u51fd\u6570\u5982\u4f55\u4f7f\u7528\uff0c\u4f7f\u7528JS\u8fd0\u52a8\u7f13\u51b2\u6548\u679c\u7684\u5c01\u88c5\u51fd\u6570\u7684\u6709\u54ea\u4e9b\uff0c\u4e0b\u9762\u5c31\u662f\u5b9e\u6218\u6848\u4f8b\uff0c\u4e00\u8d77\u6765\u770b\u4e00\u4e0b\u3002<\/p>\n<p style=\"text-align: left\">\u4e4b\u524d\u7ecf\u5e38\u5199\u8fd0\u52a8\u51fd\u6570\uff0c\u8981\u5199\u597d\u591a\u597d\u591a\uff0c\u540e\u6765\u60f3\u529e\u6cd5\u5c01\u88c5\u8d77\u6765\u3002\uff08\u8fd0\u52a8\u7f13\u51b2\uff09\u3002<\/p>\n<pre>\/*\r\n  \u7269\u4f53\u591a\u5c5e\u6027\u540c\u65f6\u8fd0\u52a8\u7684\u51fd\u6570\r\n  obj\uff1a\u8fd0\u52a8\u7684\u7269\u4f53\r\n  oTarget\uff1a\u5bf9\u8c61\uff0c\u5c5e\u6027\u540d\u4e3a\u8fd0\u52a8\u7684\u6837\u5f0f\u540d\uff0c\u5c5e\u6027\u503c\u4e3a\u6837\u5f0f\u8fd0\u52a8\u7684\u7ec8\u70b9\u503c\r\n  ratio\uff1a\u901f\u5ea6\u7684\u7cfb\u6570\r\n*\/\r\nfunction bufferMove(obj, oTarget, fn,ratio = 8) {\r\n  clearInterval(obj.iTimer);\r\n  obj.iTimer = setInterval(function () {\r\n    \/\/ \u6b64\u5904\u8bbe\u5b9a\u5f00\u5173bBtn\uff0c\u5047\u8bbe\u6240\u6709\u7684\u5c5e\u6027\u5747\u5df2\u8fd0\u52a8\u5b8c\u6bd5true\r\n    var bBtn = true;\r\n    for(var sAttr in oTarget){\r\n      \/\/ \u83b7\u53d6\u5f53\u524d\u503c\uff0c\u6b64\u5904\u517c\u5bb9\u900f\u660e\u5ea6\u7684\u53d8\u5316\r\n      if(sAttr === 'opacity') {\r\n        var iCur = parseFloat(getStyle(obj, sAttr) * 100);\r\n      } else {\r\n        var iCur = parseInt(getStyle(obj, sAttr));\r\n      }\r\n      \/\/ \u8ba1\u7b97\u901f\u5ea6\uff0c\u6b64\u5904\u53ef\u5224\u5b9a\u65b9\u5411\uff0c\u5982\u5411\u5de6\u6216\u5411\u53f3\uff0c\u5148\u900f\u660e\u540e\u51fa\u73b0\u6216\u5148\u51fa\u73b0\u540e\u900f\u660e\u7b49\r\n      var iSpeed = (oTarget[sAttr] - iCur) \/ ratio;\r\n      iSpeed = iSpeed &gt; 0 ? Math.ceil(iSpeed) : Math.floor(iSpeed);\r\n      \/\/ \u8ba1\u7b97\u4e0b\u4e00\u6b21\u7684\u503c\r\n      var iNext = iCur + iSpeed;\r\n      \/\/ \u8d4b\u503c\u7ed9\u5bf9\u5e94\u6837\u5f0f\r\n      if(sAttr ==='opacity') {\r\n        obj.style.opacity = iNext \/ 100;\r\n        obj.style.filter = 'alpha(opacity=' + iNext +')';\r\n      } else {\r\n        obj.style[sAttr] = iNext + 'px';\r\n      }\r\n      \/\/ \u6e05\u9664\u5b9a\u65f6\u5668\uff0c\u5f53\u524d\u7684\u4f4d\u7f6e\u548c\u7ec8\u70b9\u503c\u662f\u5426\u76f8\u7b49\uff0c\u76f8\u7b49\u5219\u8bf4\u660e\u7ed3\u675f\r\n      if(iNext !== oTarget[sAttr]) {\r\n        bBtn = false;\r\n      }\r\n    }\r\n    \/\/ \u5982\u679cbBtn\u7684\u503c\u4e3atrue\uff0c\u5219\u8bf4\u660e\u6240\u6709\u7684\u5c5e\u6027\u5747\u5df2\u8fd0\u52a8\u5b8c\u6bd5\uff0c\u56de\u8c03\u51fd\u6570fn()\r\n    if(bBtn) {\r\n      clearInterval(obj.iTimer);\r\n      if(fn){\r\n        fn();\r\n      }\r\n    }\r\n  }, 50);\r\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p style=\"text-align: left\">\u4ee5\u4e0a\u5c01\u88c5\u51fd\u6570\u4e5f\u53ef\u4ee5\u7528\u4e8e\u5355\u4e2a\u5c5e\u6027\uff0c\u591a\u6837\u5f0f\u8fd0\u52a8\uff0c\u6bd4\u5982\uff1a<\/p>\n<pre>bufferMove(obj,{\"left\":200,\"width\":400,\"opacity\":80},fn,8);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p style=\"text-align: left\">\u5c31\u8fd9\u6837\u3002<\/p>\n<p>\u76f8\u4fe1\u770b\u4e86\u672c\u6587\u6848\u4f8b\u4f60\u5df2\u7ecf\u638c\u63e1\u4e86\u65b9\u6cd5\uff0c\u66f4\u591a\u7cbe\u5f69\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n<p>\u63a8\u8350\u9605\u8bfb\uff1a<\/p>\n<p><\/p>\n<p><\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fJS\u8fd0\u52a8\u7f13\u51b2\u6548\u679c\u7684\u5c01\u88c5\u51fd\u6570\u5982\u4f55\u4f7f\u7528\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\u6b21\u7ed9\u5927\u5bb6\u5e26\u6765JS\u8fd0\u52a8\u7f13\u51b2\u6548\u679c\u7684\u5c01\u88c5\u51fd\u6570\u5982\u4f55\u4f7f\u7528\uff0c\u4f7f\u7528JS\u8fd0\u52a8\u7f13\u51b2\u6548\u679c\u7684\u5c01\u88c5\u51fd\u6570\u7684\u6709\u54ea\u4e9b\uff0c\u4e0b\u9762\u5c31\u662f\u5b9e\u6218\u6848\u4f8b\uff0c\u4e00\u8d77\u6765\u770b\u4e00\u4e0b\u3002 \u4e4b\u524d\u7ecf\u5e38\u5199\u8fd0\u52a8\u51fd\u6570\uff0c\u8981\u5199\u597d\u591a\u597d\u591a\uff0c\u540e\u6765\u60f3\u529e\u6cd5\u5c01\u88c5\u8d77\u6765\u3002\uff08\u8fd0\u52a8\u7f13\u51b2\uff09\u3002 \/* \u7269\u4f53\u591a\u5c5e\u6027\u540c\u65f6\u8fd0\u52a8\u7684\u51fd\u6570 obj\uff1a\u8fd0\u52a8\u7684\u7269\u4f53 oTarget\uff1a\u5bf9\u8c61\uff0c\u5c5e\u6027\u540d\u4e3a\u8fd0\u52a8\u7684\u6837\u5f0f\u540d\uff0c\u5c5e\u6027\u503c\u4e3a\u6837\u5f0f\u8fd0\u52a8\u7684\u7ec8\u70b9\u503c ratio\uff1a\u901f\u5ea6\u7684\u7cfb\u6570 *\/ function bufferMove(obj, oTarget, fn,ratio = 8) { clearInterval(obj.iTimer); obj.iTimer = setInterval(function () { \/\/ \u6b64\u5904\u8bbe\u5b9a\u5f00\u5173bBtn\uff0c\u5047\u8bbe\u6240\u6709\u7684\u5c5e\u6027\u5747\u5df2\u8fd0\u52a8\u5b8c\u6bd5true var bBtn = true; for(var sAttr in oTarget){ \/\/ \u83b7\u53d6\u5f53\u524d\u503c\uff0c\u6b64\u5904\u517c\u5bb9\u900f\u660e\u5ea6\u7684\u53d8\u5316 if(sAttr === &#8216;opacity&#8217;) { var iCur = parseFloat(getStyle(obj, sAttr) * 100); } else { var iCur = parseInt(getStyle(obj, sAttr)); } \/\/ \u8ba1\u7b97\u901f\u5ea6\uff0c\u6b64\u5904\u53ef\u5224\u5b9a\u65b9\u5411\uff0c\u5982\u5411\u5de6\u6216\u5411\u53f3\uff0c\u5148\u900f\u660e\u540e\u51fa\u73b0\u6216\u5148\u51fa\u73b0\u540e\u900f\u660e\u7b49 [&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-31376","post","type-post","status-publish","format-standard","hentry","category-19"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/31376","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=31376"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/31376\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=31376"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=31376"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=31376"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}