{"id":171753,"date":"2024-05-08T17:36:19","date_gmt":"2024-05-08T09:36:19","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/171753.html"},"modified":"2024-05-08T17:36:28","modified_gmt":"2024-05-08T09:36:28","slug":"css%e6%88%96javascript%e6%80%8e%e6%a0%b7%e5%ae%9e%e7%8e%b0%e5%9b%be%e7%89%87%e9%80%8f%e6%98%8e%e5%ba%a6%e6%b8%90%e5%8f%98","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/171753.html","title":{"rendered":"CSS\u6216JavaScript\u600e\u6837\u5b9e\u73b0\u56fe\u7247\u900f\u660e\u5ea6\u6e10\u53d8"},"content":{"rendered":"<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/27043027\/6a788d28-abe3-4769-a24c-ca74002685cf.webp\" alt=\"CSS\u6216JavaScript\u600e\u6837\u5b9e\u73b0\u56fe\u7247\u900f\u660e\u5ea6\u6e10\u53d8\" \/><\/p>\n<p><p>\u56fe\u7247\u900f\u660e\u5ea6\u6e10\u53d8\u53ef\u4ee5\u901a\u8fc7CSS\u7684transition\u5c5e\u6027\u6216\u8005JavaScript\u7684DOM\u64cd\u4f5c\u5b9e\u73b0\uff0c\u901a\u8fc7\u8c03\u6574\u5143\u7d20\u7684opacity\u5c5e\u6027\uff0c\u4ece\u800c\u6e10\u53d8\u56fe\u7247\u7684\u900f\u660e\u5ea6\u3002<strong>CSS\u5b9e\u73b0\u8f83\u4e3a\u7b80\u5355\u3001\u4fbf\u6377\u3001\u4e14\u6027\u80fd\u826f\u597d<\/strong>\uff0c\u53ea\u9700\u8bbe\u7f6e\u900f\u660e\u5ea6\u521d\u59cb\u503c\u548c\u53d8\u5316\u8fc7\u7a0b\u6240\u9700\u7684\u65f6\u95f4\u3002\u800c\u4f7f\u7528<strong>JavaScript\u5b9e\u73b0\u5219\u53ef\u4ee5\u63d0\u4f9b\u66f4\u9ad8\u7684\u7075\u6d3b\u6027<\/strong>\uff0c\u4f8b\u5982\u6839\u636e\u7528\u6237\u884c\u4e3a\u6765\u63a7\u5236\u6e10\u53d8\u8fc7\u7a0b\uff0c\u6216\u8005\u7ed3\u5408\u66f4\u590d\u6742\u7684\u903b\u8f91\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001CSS\u5b9e\u73b0\u56fe\u7247\u900f\u660e\u5ea6\u6e10\u53d8<\/h3>\n<\/p>\n<p><p>\u5728\u4f7f\u7528CSS\u6765\u5b9e\u73b0\u56fe\u7247\u900f\u660e\u5ea6\u6e10\u53d8\u65f6\uff0c\u6211\u4eec\u4e3b\u8981\u4f7f\u7528\u5230\u7684\u662f<code>opacity<\/code>\u5c5e\u6027\u548c<code>transition<\/code>\u5c5e\u6027\u3002<code>opacity<\/code>\u5c5e\u6027\u5b9a\u4e49\u4e86\u5143\u7d20\u7684\u4e0d\u900f\u660e\u5ea6\u7b49\u7ea7\uff0c\u503c\u4e3a0\u8868\u793a\u5b8c\u5168\u900f\u660e\uff0c\u503c\u4e3a1\u8868\u793a\u5b8c\u5168\u4e0d\u900f\u660e\u3002<code>transition<\/code>\u5c5e\u6027\u5219\u662f\u7528\u6765\u6307\u5b9a<code>opacity<\/code>\u53d8\u5316\u7684\u6301\u7eed\u65f6\u95f4\u4ee5\u53ca\u8fc7\u6e21\u6548\u679c\u3002<\/p>\n<\/p>\n<p><h4>1. \u8bbe\u5b9a\u521d\u59cb\u6837\u5f0f<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u4e3a\u56fe\u7247\u8bbe\u5b9a\u4e00\u4e2a\u521d\u59cb\u7684\u900f\u660e\u5ea6\u503c\uff0c\u8fd9\u901a\u5e38\u5728CSS\u4e2d\u5b9a\u4e49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-css\">.img-transparent {<\/p>\n<p>    opacity: 1; \/* \u8bbe\u7f6e\u5143\u7d20\u4e3a\u5b8c\u5168\u4e0d\u900f\u660e *\/<\/p>\n<p>    transition: opacity 2s ease-in-out; \/* \u5b9a\u4e49\u900f\u660e\u5ea6\u53d8\u5316\u6301\u7eed2\u79d2\uff0c\u5e76\u4e14\u8fc7\u6e21\u6548\u679c\u4e3a\u5148\u6162\u540e\u5feb\u518d\u6162 *\/<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u5b9a\u4e49\u4ea4\u4e92\u6837\u5f0f<\/h4>\n<\/p>\n<p><p>\u63a5\u7740\uff0c\u6211\u4eec\u53ef\u4ee5\u5b9a\u4e49\u4e00\u4e2a\u65b0\u7684\u72b6\u6001\uff0c\u4f8b\u5982\u5f53\u9f20\u6807\u60ac\u505c\u5728\u56fe\u7247\u4e0a\u65f6\uff0c\u900f\u660e\u5ea6\u53d8\u5316\u5230\u4e00\u4e2a\u65b0\u7684\u503c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-css\">.img-transparent:hover {<\/p>\n<p>    opacity: 0.5; \/* \u9f20\u6807\u60ac\u505c\u65f6\u900f\u660e\u5ea6\u53d8\u4e3a0.5 *\/<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001JavaScript \u5b9e\u73b0\u56fe\u7247\u900f\u660e\u5ea6\u6e10\u53d8<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7JavaScript\uff0c\u6211\u4eec\u53ef\u4ee5\u66f4\u7cbe\u786e\u5730\u63a7\u5236\u56fe\u7247\u900f\u660e\u5ea6\u7684\u6e10\u53d8\uff0c\u8fd9\u5305\u62ec\u5728\u7279\u5b9a\u4e8b\u4ef6\u89e6\u53d1\u65f6\u8fdb\u884c\u53d8\u5316\uff0c\u6216\u8005\u521b\u5efa\u590d\u6742\u7684\u52a8\u753b\u6548\u679c\u3002<\/p>\n<\/p>\n<p><h4>1. \u57fa\u7840\u8bbe\u7f6e<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u5728JavaScript\u4e2d\u9009\u53d6\u5bf9\u5e94\u7684\u56fe\u7247\u5143\u7d20\uff0c\u5e76\u8bbe\u7f6e\u5176\u521d\u59cb\u900f\u660e\u5ea6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-javascript\">var image = document.querySelector(&#039;.img-transparent&#039;);<\/p>\n<p>image.style.opacity = 1; \/\/ \u8bbe\u7f6e\u5143\u7d20\u4e3a\u5b8c\u5168\u4e0d\u900f\u660e<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u5b9a\u4e49\u900f\u660e\u5ea6\u53d8\u5316\u51fd\u6570<\/h4>\n<\/p>\n<p><p>\u7136\u540e\uff0c\u6211\u4eec\u53ef\u4ee5\u5b9a\u4e49\u4e00\u4e2a\u51fd\u6570\u6765\u6e10\u53d8\u900f\u660e\u5ea6\uff0c\u8fd9\u5c06\u5141\u8bb8\u6211\u4eec\u5728\u9700\u8981\u7684\u65f6\u5019\u8c03\u7528\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-javascript\">function fadeTo(element, opacity, duration) {<\/p>\n<p>    element.style.transition = `opacity ${duration}s ease-in-out`;<\/p>\n<p>    element.style.opacity = opacity;<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3. \u4e8b\u4ef6\u89e6\u53d1\u900f\u660e\u5ea6\u6e10\u53d8<\/h4>\n<\/p>\n<p><p>\u6700\u540e\uff0c\u6211\u4eec\u901a\u8fc7\u4e8b\u4ef6\u76d1\u542c\u6765\u89e6\u53d1\u900f\u660e\u5ea6\u7684\u6e10\u53d8\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-javascript\">var image = document.querySelector(&#039;.img-transparent&#039;);<\/p>\n<p>image.addEventListener(&#039;mouseenter&#039;, function() { fadeTo(image, 0.5, 2); });<\/p>\n<p>image.addEventListener(&#039;mouseleave&#039;, function() { fadeTo(image, 1, 2); });<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4ee5\u4e0a\u4ecb\u7ecd\u5404\u81ea\u65b9\u6cd5\u7684\u57fa\u7840\u5b9e\u73b0\u65b9\u5f0f\uff0c\u63a5\u4e0b\u6765\u6211\u4eec\u5c06\u8be6\u7ec6\u63a2\u8ba8\u6df1\u5165\u7684\u6280\u5de7\u548c\u6ce8\u610f\u4e8b\u9879\u6765\u5b8c\u5584\u8fd9\u4e9b\u57fa\u672c\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001CSS \u8fdb\u9636\u4f7f\u7528<\/h3>\n<\/p>\n<p><p>\u5f53\u6211\u4eec\u5728HTML\u548cCSS\u4e2d\u6e10\u53d8\u56fe\u7247\u900f\u660e\u5ea6\u65f6\uff0c\u9664\u4e86\u4e0a\u8ff0\u7684\u57fa\u672c\u7528\u6cd5\uff0c\u6211\u4eec\u8fd8\u53ef\u4ee5\u52a0\u5165\u66f4\u591a\u7684\u8bbe\u8ba1\u5143\u7d20\u548c\u6280\u5de7\u3002<\/p>\n<\/p>\n<p><h4>1. \u4f7f\u7528CSS\u53d8\u91cf<\/h4>\n<\/p>\n<p><p>CSS\u53d8\u91cf\u53ef\u4ee5\u589e\u52a0\u6837\u5f0f\u5b9a\u4e49\u7684\u7075\u6d3b\u6027\u3002\u6211\u4eec\u53ef\u4ee5\u5b9a\u4e49\u4e00\u4e2a\u53d8\u91cf\u6765\u63a7\u5236\u900f\u660e\u5ea6\u7684\u503c\uff0c\u8fd9\u6837\u5c31\u53ef\u4ee5\u5728\u591a\u5904\u4f7f\u7528\uff0c\u5e76\u4e14\u65b9\u4fbf\u7ba1\u7406\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-css\">:root {<\/p>\n<p>    --image-opacity: 0.5;<\/p>\n<p>}<\/p>\n<p>.img-transparent:hover {<\/p>\n<p>    opacity: var(--image-opacity);<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u7ed3\u5408\u5176\u4ed6\u52a8\u753b<\/h4>\n<\/p>\n<p><p>\u6211\u4eec\u8fd8\u53ef\u4ee5\u7ed3\u5408\u5176\u4ed6\u52a8\u753b\u5c5e\u6027\uff0c\u5982<code>transform<\/code>\uff0c\u4e0e\u900f\u660e\u5ea6\u6e10\u53d8\u4e00\u8d77\u4f7f\u7528\uff0c\u521b\u9020\u66f4\u4e30\u5bcc\u7684\u89c6\u89c9\u6548\u679c\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p><strong>1. \u5982\u4f55\u4f7f\u7528CSS\u5b9e\u73b0\u56fe\u7247\u900f\u660e\u5ea6\u6e10\u53d8\uff1f<\/strong><\/p>\n<p>\u8981\u5b9e\u73b0\u56fe\u7247\u7684\u900f\u660e\u5ea6\u6e10\u53d8\u6548\u679c\uff0c\u53ef\u4ee5\u4f7f\u7528CSS\u7684transition\u5c5e\u6027\u548copacity\u5c5e\u6027\u3002\u9996\u5148\uff0c\u5c06\u56fe\u7247\u7684\u521d\u59cb\u900f\u660e\u5ea6\u8bbe\u7f6e\u4e3a0\uff0c\u7136\u540e\u4f7f\u7528:hover\u4f2a\u7c7b\u9009\u62e9\u5668\u6765\u63a7\u5236\u9f20\u6807\u60ac\u505c\u65f6\u7684\u900f\u660e\u5ea6\u3002\u901a\u8fc7\u8bbe\u7f6etransition\u5c5e\u6027\uff0c\u53ef\u4ee5\u8ba9\u8fc7\u6e21\u6548\u679c\u5e73\u6ed1\u800c\u4e0d\u7a81\u5140\u3002\u540c\u65f6\uff0c\u8fd8\u53ef\u4ee5\u8c03\u6574\u8fc7\u6e21\u6548\u679c\u7684\u65f6\u95f4\u548c\u901f\u5ea6\uff0c\u4ee5\u8fbe\u5230\u7406\u60f3\u7684\u6548\u679c\u3002<\/p>\n<p><strong>2. \u600e\u6837\u4f7f\u7528JavaScript\u6765\u5b9e\u73b0\u56fe\u7247\u900f\u660e\u5ea6\u6e10\u53d8\uff1f<\/strong><\/p>\n<p>\u4f7f\u7528JavaScript\u6765\u5b9e\u73b0\u56fe\u7247\u900f\u660e\u5ea6\u6e10\u53d8\u4e5f\u662f\u4e00\u79cd\u5e38\u89c1\u7684\u65b9\u6cd5\u3002\u4f60\u53ef\u4ee5\u901a\u8fc7\u83b7\u53d6\u56fe\u7247\u7684DOM\u5143\u7d20\uff0c\u7136\u540e\u4f7f\u7528setInterval\u51fd\u6570\u548c\u900f\u660e\u5ea6\u5c5e\u6027\u6765\u5b9e\u73b0\u9010\u6b65\u6539\u53d8\u56fe\u7247\u900f\u660e\u5ea6\u7684\u6548\u679c\u3002\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6e\u65f6\u95f4\u95f4\u9694\u548c\u6539\u53d8\u900f\u660e\u5ea6\u7684\u6b65\u957f\u6765\u8c03\u6574\u6e10\u53d8\u6548\u679c\u7684\u901f\u5ea6\u548c\u6d41\u7545\u5ea6\u3002<\/p>\n<p><strong>3. \u6709\u6ca1\u6709\u5176\u4ed6\u65b9\u6cd5\u53ef\u4ee5\u5b9e\u73b0\u56fe\u7247\u900f\u660e\u5ea6\u6e10\u53d8\uff1f<\/strong><\/p>\n<p>\u9664\u4e86\u4f7f\u7528CSS\u548cJavaScript\uff0c\u8fd8\u6709\u5176\u4ed6\u65b9\u6cd5\u53ef\u4ee5\u5b9e\u73b0\u56fe\u7247\u900f\u660e\u5ea6\u6e10\u53d8\u3002\u6bd4\u5982\uff0c\u4f60\u53ef\u4ee5\u501f\u52a9\u4e00\u4e9b\u73b0\u6210\u7684CSS\u52a8\u753b\u5e93\u6216\u6846\u67b6\uff0c\u5982Animate.css\u3001Velocity.js\u7b49\u3002\u8fd9\u4e9b\u5e93\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u8fc7\u6e21\u6548\u679c\u548c\u52a8\u753b\u9009\u9879\uff0c\u53ef\u4ee5\u66f4\u65b9\u4fbf\u5730\u5b9e\u73b0\u56fe\u7247\u900f\u660e\u5ea6\u7684\u6e10\u53d8\u6548\u679c\u3002\u53e6\u5916\uff0c\u4e5f\u53ef\u4ee5\u4f7f\u7528\u4e00\u4e9b\u524d\u7aef\u5f00\u53d1\u5de5\u5177\u548c\u7f16\u8f91\u5668\uff0c\u5982Adobe Photoshop\u3001Sketch\u7b49\uff0c\u901a\u8fc7\u8bbe\u7f6e\u56fe\u5c42\u4e0d\u540c\u7684\u900f\u660e\u5ea6\u6765\u5b9e\u73b0\u56fe\u7247\u7684\u6e10\u53d8\u6548\u679c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u56fe\u7247\u900f\u660e\u5ea6\u6e10\u53d8\u53ef\u4ee5\u901a\u8fc7CSS\u7684transition\u5c5e\u6027\u6216\u8005JavaScript\u7684DOM\u64cd\u4f5c\u5b9e\u73b0\uff0c\u901a\u8fc7\u8c03\u6574\u5143\u7d20\u7684 [&hellip;]","protected":false},"author":3,"featured_media":171770,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/171753"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=171753"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/171753\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/171770"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=171753"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=171753"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=171753"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}