{"id":7451,"date":"2018-03-25T21:19:38","date_gmt":"2018-03-25T13:19:38","guid":{"rendered":"http:\/\/www.zhangxinxu.com\/wordpress\/?p=7451"},"modified":"2019-02-28T17:45:14","modified_gmt":"2019-02-28T09:45:14","slug":"background-gradient-transtion","status":"publish","type":"post","link":"https:\/\/www.zhangxinxu.com\/wordpress\/2018\/03\/background-gradient-transtion\/","title":{"rendered":"\u51e0\u79cdCSS\u6e10\u53d8\u80cc\u666f\u56fe\u7247transtion\u52a8\u753b\u65b9\u6cd5"},"content":{"rendered":"<p>by <a href=\"http:\/\/www.zhangxinxu.com\/\">zhangxinxu<\/a> from <a href=\"http:\/\/www.zhangxinxu.com\/wordpress\/?p=7451\">http:\/\/www.zhangxinxu.com\/wordpress\/?p=7451<\/a><br \/>\n\u672c\u6587\u53ef\u5168\u6587\u8f6c\u8f7d\uff0c\u4f46\u9700\u8981\u4fdd\u7559\u539f\u4f5c\u8005\u548c\u51fa\u5904\uff0c\u6458\u8981\u5f15\u6d41\u5219\u968f\u610f\u3002<\/p>\n<h3>\u4e00\u3001background-image\u4e0d\u652f\u6301CSS3 transition<\/h3>\n<p><code>background-image<\/code>\u4e0d\u652f\u6301CSS3 <code>transition<\/code>\uff0c\u800cCSS3 gradient\u6e10\u53d8\u4f5c\u4e3a\u80cc\u666f\u56fe\u7247\u5b58\u5728\u7684\u65f6\u5019\uff0c\u4e0b\u9762\u7684CSS\u8bbe\u7f6e\u662f\u4e0d\u4f1a\u6709\u8fc7\u6e21\u6548\u679c\u7684\u3002<\/p>\n<pre>.gradient {\r\n  background-image: linear-gradient(to right, olive, green);\r\n  transition: background-image 0.5s linear;\r\n}\r\n.gradient:hover {\r\n  background-image: linear-gradient(to right, green, purple);\r\n}<\/pre>\n<p>\u9f20\u6807hover\u4f1a\u53d1\u73b0\u6e10\u53d8\u7684\u53d8\u5316\u662f\u5f88\u5510\u7a81\u7684\uff0c\u4e00\u70b9\u8fc7\u6e21\u6548\u679c\u4e5f\u6ca1\u6709\u3002<\/p>\n<p>\u4e0b\u9762\u95ee\u9898\u6765\u4e86\uff0c\u5982\u679c\u6211\u4eec\u5e0c\u671b\u5b9e\u73b0\u6e10\u53d8hover\u65f6\u5019\u6709\u8fc7\u6e21\u53d8\u5316\u7684\u6548\u679c\uff0c\u8be5\u5982\u4f55\u5b9e\u73b0\u5462\uff1f\u6211\u8fd9\u91cc\u7f57\u5217\u7684\u51e0\u79cd\u53ef\u884c\u7684\u65b9\u6cd5\u3002<\/p>\n<h3>\u4e8c\u3001\u501f\u52a9background-position\u5b9e\u73b0\u6e10\u53d8\u8fc7\u6e21<\/h3>\n<p><code>background-image<\/code>\u867d\u7136\u4e0d\u652f\u6301CSS3 <code>transition<\/code>\u8fc7\u6e21\uff0c\u4f46\u662f<code>background-position<\/code>\u652f\u6301\u554a\uff0c\u4e8e\u662f\uff0c\u901a\u8fc7\u63a7\u5236\u80cc\u666f\u4f4d\u7f6e\uff0c\u6211\u4eec\u662f\u53ef\u4ee5\u5b9e\u73b0\u6e10\u53d8\u8fc7\u6e21\u6548\u679c\u7684\u3002<\/p>\n<p>\u60a8\u53ef\u4ee5\u72e0\u72e0\u5730\u70b9\u51fb\u8fd9\u91cc\uff1a<a href=\"http:\/\/www.zhangxinxu.com\/study\/201803\/gradient-transition-by-background-position.html\">\u501f\u52a9background-position\u5b9e\u73b0\u6e10\u53d8\u8fc7\u6e21demo<\/a><\/p>\n<p>\u5b9e\u73b0\u6548\u679c\u5982\u4e0b\uff08\u9f20\u6807hover\uff09\uff1a<\/p>\n<style>.gradient{    max-width: 400px;    height: 200px;    background: linear-gradient(to right, olive, green, purple);    background-size: 200%;    transition: background-position .5s;    }.gradient:hover {    background-position: 100% 0;    }<\/style>\n<div class=\"gradient\"><\/div>\n<p>\u76f8\u5173\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre>&lt;div class=\"box\"&gt;&lt;\/div&gt;<\/pre>\n<pre>.box {\r\n    max-width: 400px;\r\n    height: 200px;\r\n    background: linear-gradient(to right, olive, green, purple);\r\n    background-size: 200%;\r\n    transition: background-position .5s;    \r\n}\r\n.box:hover {\r\n    background-position: 100% 0;    \r\n}<\/pre>\n<h3>\u4e09\u3001\u501f\u52a9background-color\u5b9e\u73b0\u6e10\u53d8\u8fc7\u6e21<\/h3>\n<p><code>background-image<\/code>\u867d\u7136\u4e0d\u652f\u6301CSS3 <code>transition<\/code>\u8fc7\u6e21\uff0c\u4f46\u662f<code>background-color<\/code>\u652f\u6301\u554a\uff0c\u4e8e\u662f\uff0c\u901a\u8fc7\u63a7\u5236\u80cc\u666f\u989c\u8272\uff0c\u548c\u4e00\u4e2a\u989c\u8272\u5448\u73b0\u6280\u5de7\uff0c\u6211\u4eec\u4e5f\u662f\u53ef\u4ee5\u5b9e\u73b0\u6e10\u53d8\u8fc7\u6e21\u6548\u679c\u7684\u3002<\/p>\n<p>\u60a8\u53ef\u4ee5\u72e0\u72e0\u5730\u70b9\u51fb\u8fd9\u91cc\uff1a<a href=\"http:\/\/www.zhangxinxu.com\/study\/201803\/gradient-transition-by-background-color.html\" rel=\"noopener\" target=\"_blank\">background-color\u5b9e\u73b0\u6e10\u53d8hover\u8fc7\u6e21\u6548\u679cdemo<\/a><\/p>\n<p>\u9f20\u6807hover\u524d\u540e\u6548\u679c\u5bf9\u6bd4\uff1a<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/image.zhangxinxu.com\/image\/blog\/201803\/2018-03-25_210007.png\" width=\"405\" height=\"207\" alt=\"\u9f20\u6807hover\u524d\u6548\u679c\" class=\"alignnone\" \/> <img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/image.zhangxinxu.com\/image\/blog\/201803\/2018-03-25_210201.png\" width=\"406\" height=\"203\" alt=\"\u9f20\u6807hover\u65f6\u5019\u6548\u679c\" class=\"alignnone\" \/><\/p>\n<p>\u76f8\u5173\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre>&lt;div class=\"box\"&gt;&lt;\/div&gt;<\/pre>\n<pre>.box {\r\n    max-width: 400px;\r\n    height: 200px;\r\n    background: olive linear-gradient(to right, rgba(0,255,0,0), rgba(0,255,0,.5));\r\n    transition: background-color .5s;    \r\n}\r\n.box:hover {\r\n    background-color: purple;    \r\n}<\/pre>\n<h3>\u56db\u3001\u501f\u52a9\u4f2a\u5143\u7d20\u548copacity\u5b9e\u73b0\u6e10\u53d8\u8fc7\u6e21<\/h3>\n<p>\u501f\u52a9\u4f2a\u5143\u7d20\u521b\u5efa\u53d8\u6362\u540e\u7684\u6e10\u53d8\u6548\u679c\uff0c\u901a\u8fc7\u6539\u53d8\u8986\u76d6\u7684\u6e10\u53d8\u7684opacity\u900f\u660e\u5ea6\u53d8\u5316\u5b9e\u73b0\u6e10\u53d8\u8fc7\u6e21\u6548\u679c\u3002<\/p>\n<p>\u60a8\u53ef\u4ee5\u72e0\u72e0\u5730\u70b9\u51fb\u8fd9\u91cc\uff1a<a href=\"http:\/\/www.zhangxinxu.com\/study\/201803\/gradient-transition-by-opacity.html\" rel=\"noopener\" target=\"_blank\">CSS3 opacity\u5b9e\u73b0\u6e10\u53d8hover\u8fc7\u6e21\u6548\u679cdemo<\/a><\/p>\n<p>\u4e0b\u56fe\u4e3ahover\u4e4b\u540e\u7684\u6548\u679c\uff1a<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/image.zhangxinxu.com\/image\/blog\/201803\/2018-03-25_211354.png\" width=\"411\" height=\"211\" alt=\"hover\u4e4b\u540e\u900f\u660e\u5ea6\u53d8\u5316\u622a\u56fe\" class=\"alignnone\" \/><\/p>\n<p>\u76f8\u5173\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre>&lt;div class=\"box\"&gt;&lt;\/div&gt;<\/pre>\n<pre>.box {\r\n    max-width: 400px; height: 200px;\r\n    background: linear-gradient(to right, olive, green);\r\n    position: relative;\r\n    z-index: 0;    \r\n}\r\n.box::before {\r\n    content: '';\r\n    position: absolute;\r\n    left: 0; top: 0; right: 0; bottom: 0;\r\n    background: linear-gradient(to right, green, purple);\r\n    opacity: 0;    \r\n    transition: opacity .5s;\r\n    z-index: -1;\r\n}\r\n.box:hover::before {\r\n    opacity: 1;    \r\n}<\/pre>\n<h3>\u4e94\u3001\u65e5\u540e\u8865\u5145\u65b0\u65b9\u6cd5\uff0c\u7ed3\u675f\u8bed<\/h3>\n<p><strong>\u8865\u5145\u4e8e2018-11-25<\/strong><\/p>\n<p>\u8fd8\u53ef\u4ee5\u501f\u52a9CSS houdini\u4e2d\u7684Properties &amp; Values API\u5b9e\u73b0\u53d8\u5316\u6548\u679c\u3002<\/p>\n<p>CSS Properties &amp; Values API\u53ef\u4ee5\u81ea\u5b9a\u4e49CSS\u5c5e\u6027\uff0c\u4f8b\u5982\uff0c\u6211\u4eec\u628a\u6e10\u53d8\u8d77\u6b62\u989c\u8272\u5b9a\u4e49\u4e3a\u4e00\u4e2a<code>&lt;color&gt;<\/code>\u7c7b\u578b\u7684CSS\u5c5e\u6027\uff0c\u8fd9\u6837\uff0c\u6e10\u53d8\u4e5f\u80fdtransition\u4e86\uff0c\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre>&lt;div class=\"box\"&gt;&lt;\/div&gt;<\/pre>\n<pre>.box {\r\n    --start-stop: olive;\r\n    --end-stop: green;\r\n    background: linear-gradient(to right, var(--start-stop), var(--end-stop));\r\n    transition: --start-stop .5s, --end-stop .5s;\r\n}\r\n.box:hover {\r\n    --start-stop: green;\r\n    --end-stop: purple;\r\n}<\/pre>\n<p>\u7136\u540e\uff0c\u6700\u6700\u5173\u952e\u7684\u662f\u5c31\u662f\u501f\u52a9CSS.registerProperty API\u628a<code>--start-stop<\/code>\u548c<code>--end-stop<\/code>\u6ce8\u518c\u4e3a\u5408\u6cd5\u7684CSS\u5c5e\u6027\uff0c\u5982\u4e0b\uff1a<\/p>\n<pre>if (window.CSS) {\r\n    CSS.registerProperty({\r\n        name: '--start-stop',\r\n        syntax: '&lt;color&gt;',\r\n        inherits: false,\r\n        initialValue: 'transparent'\r\n    });\r\n    CSS.registerProperty({\r\n        name: '--end-stop',\r\n        syntax: '&lt;color&gt;',\r\n        inherits: false,\r\n        initialValue: 'transparent'\r\n    });\r\n}<\/pre>\n<p>\u7136\u540e\u6e10\u53d8hover transition\u8fc7\u6e21\u6548\u679c\u5c31\u6709\u4e86\u54c8\uff01<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"\/\/image.zhangxinxu.com\/image\/blog\/201811\/2018-11-25_214202.jpg\" width=\"448\" height=\"270\" alt=\"hover\u65f6\u5019\u7684\u6e10\u53d8\u8fc7\u6e21\u6548\u679c\u622a\u56fe\" class=\"alignnone size-medium\" \/><\/p>\n<p>\u773c\u89c1\u4e3a\u5b9e\uff0c\u60a8\u53ef\u4ee5\u72e0\u72e0\u5730\u70b9\u51fb\u8fd9\u91cc\uff1a<a href=\"https:\/\/www.zhangxinxu.com\/study\/201811\/css-registerproperty-background-transition.php\" rel=\"noopener\" target=\"_blank\">CSS registerProperty\u6e10\u53d8\u80cc\u666f\u7684transition\u8fc7\u6e21demo<\/a><\/p>\n<p>\u53ef\u5c5e\u6027hover\u6e10\u53d8\u611f\u53d7\u8fc7\u6e21\u52a8\u753b\u3002<\/p>\n<hr>\n<p>\u4ee5\u4e0a\u5c31\u662f\u6211\u6240\u77e5\u9053\u7684\u51e0\u4e2a\u65b9\u6cd5\uff0c\u5f53\u7136\uff0c\u80af\u5b9a\u8fd8\u6709\u5176\u4ed6\u66f4\u597d\u7684\u5b9e\u73b0\uff0c\u6b22\u8fce\u8865\u5145\u3002<\/p>\n<p>\u611f\u8c22\u9605\u8bfb\uff01<\/p>\n<p><img decoding=\"async\" src=\"http:\/\/image.zhangxinxu.com\/image\/blog\/201611\/14.png\" align=\"absmiddle\"><\/p>\n<p>\u672c\u6587\u4e3a\u539f\u521b\u6587\u7ae0\uff0c\u4f1a\u7ecf\u5e38\u66f4\u65b0\u77e5\u8bc6\u70b9\u4ee5\u53ca\u4fee\u6b63\u4e00\u4e9b\u9519\u8bef\uff0c\u56e0\u6b64\u8f6c\u8f7d\u8bf7\u4fdd\u7559\u539f\u51fa\u5904\uff0c\u65b9\u4fbf\u6eaf\u6e90\uff0c\u907f\u514d\u9648\u65e7\u9519\u8bef\u77e5\u8bc6\u7684\u8bef\u5bfc\uff0c\u540c\u65f6\u6709\u66f4\u597d\u7684\u9605\u8bfb\u4f53\u9a8c\u3002<br \/>\n\u672c\u6587\u5730\u5740\uff1a<a href=\"http:\/\/www.zhangxinxu.com\/wordpress\/?p=7451\">http:\/\/www.zhangxinxu.com\/wordpress\/?p=7451<\/a><\/p>\n<p>\uff08\u672c\u7bc7\u5b8c\uff09<\/p>\n","protected":false},"excerpt":{"rendered":"<p>background-image\u4e0d\u652f\u6301CSS3 transition\uff0c\u56e0\u6b64\uff0c\u5f53CSS3 gradient\u6e10\u53d8\u4f5c\u4e3a\u80cc\u666f\u56fe\u7247\u5b58\u5728\u7684\u65f6\u5019\uff0c\u76f4\u63a5\u8bbe\u7f6etransition\u662f\u4e0d\u4f1a\u6709\u8fc7\u6e21\u6548\u679c\u7684\uff0c\u90a3\u8be5\u600e\u4e48\u529e\u5462\uff1f<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[851,1231,177,1271,364,1180,398,1368,288,189],"_links":{"self":[{"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/posts\/7451"}],"collection":[{"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/comments?post=7451"}],"version-history":[{"count":0,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/posts\/7451\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/media?parent=7451"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/categories?post=7451"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/tags?post=7451"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}