{"action":"created","issue":{"url":"https://api.github.com/repos/chokcoco/iCSS/issues/16","repository_url":"https://api.github.com/repos/chokcoco/iCSS","labels_url":"https://api.github.com/repos/chokcoco/iCSS/issues/16/labels{/name}","comments_url":"https://api.github.com/repos/chokcoco/iCSS/issues/16/comments","events_url":"https://api.github.com/repos/chokcoco/iCSS/issues/16/events","html_url":"https://github.com/chokcoco/iCSS/issues/16","id":226958474,"node_id":"MDU6SXNzdWUyMjY5NTg0NzQ=","number":16,"title":"谈谈一些有趣的CSS题目（28）-- 不可思议的颜色混合模式 mix-blend-mode","user":{"login":"chokcoco","id":8554143,"node_id":"MDQ6VXNlcjg1NTQxNDM=","avatar_url":"https://avatars1.githubusercontent.com/u/8554143?v=4","gravatar_id":"","url":"https://api.github.com/users/chokcoco","html_url":"https://github.com/chokcoco","followers_url":"https://api.github.com/users/chokcoco/followers","following_url":"https://api.github.com/users/chokcoco/following{/other_user}","gists_url":"https://api.github.com/users/chokcoco/gists{/gist_id}","starred_url":"https://api.github.com/users/chokcoco/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/chokcoco/subscriptions","organizations_url":"https://api.github.com/users/chokcoco/orgs","repos_url":"https://api.github.com/users/chokcoco/repos","events_url":"https://api.github.com/users/chokcoco/events{/privacy}","received_events_url":"https://api.github.com/users/chokcoco/received_events","type":"User","site_admin":false},"labels":[{"id":703082722,"node_id":"MDU6TGFiZWw3MDMwODI3MjI=","url":"https://api.github.com/repos/chokcoco/iCSS/labels/%E5%8A%A8%E6%95%88","name":"动效","color":"d4c5f9","default":false},{"id":703083259,"node_id":"MDU6TGFiZWw3MDMwODMyNTk=","url":"https://api.github.com/repos/chokcoco/iCSS/labels/%E5%8E%9F%E7%90%86","name":"原理","color":"c5def5","default":false}],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":0,"created_at":"2017-05-08T07:14:44Z","updated_at":"2018-11-27T03:54:13Z","closed_at":null,"author_association":"OWNER","body":"CSS3 新增了一个很有意思的属性 -- `mix-blend-mode` ，其中 mix 和 blend 的中文意译均为混合，那么这个属性的作用直译过来就是混合混合模式，当然，我们我们通常称之为**混合模式**。\r\n\r\n混合模式最常见于 photoshop 中，是 PS 中十分强大的功能之一。当然，瞎用乱用混合模式谁都会，利用混合模式将多个图层混合得到一个新的效果，只是要用到恰到好处，或者说在 CSS 中利用混合模式制作出一些效果则需要对混合模式很深的理解及不断的尝试。\r\n\r\n我个人对混合模式的理解也十分浅显，本文只是带领大家走进 CSS 混合模式的世界，初浅的了解混合模式及尝试使用它制作一些效果。\r\n\r\n## `mix-blend-mode` 概述\r\n\r\n上文也说了，`mix-blend-mode` 描述了元素的内容应该与元素的直系父元素的内容和元素的背景如何混合。我们将 PS 中图层的概念替换为 HTML 中的元素。\r\n\r\n看看可取的值有哪些：\r\n```CSS\r\n{\r\n  mix-blend-mode: normal;         // 正常\r\n  mix-blend-mode: multiply;       // 正片叠底\r\n  mix-blend-mode: screen;         // 滤色\r\n  mix-blend-mode: overlay;        // 叠加\r\n  mix-blend-mode: darken;         // 变暗\r\n  mix-blend-mode: lighten;        // 变亮\r\n  mix-blend-mode: color-dodge;    // 颜色减淡\r\n  mix-blend-mode: color-burn;     // 颜色加深\r\n  mix-blend-mode: hard-light;     // 强光\r\n  mix-blend-mode: soft-light;     // 柔光\r\n  mix-blend-mode: difference;     // 差值\r\n  mix-blend-mode: exclusion;      // 排除\r\n  mix-blend-mode: hue;            // 色相\r\n  mix-blend-mode: saturation;     // 饱和度\r\n  mix-blend-mode: color;          // 颜色\r\n  mix-blend-mode: luminosity;     // 亮度\r\n  \r\n  mix-blend-mode: initial;\r\n  mix-blend-mode: inherit;\r\n  mix-blend-mode: unset;\r\n}\r\n```\r\n除去 `initial` 默认、`inherit` 继承 和 `unset` 还原这 3 个所有 CSS 属性都可以取的值外，还有另外的 16 个具体的取值，对应不同的混合效果。\r\n\r\n如果不是专业的 PSer 天天和混合模式打交道，想要记住这么多效果，还是挺困难的。不过有前人帮我们总结了一番，看看如何比较好的理解或者说记忆这些效果，摘自[Photoshop中高级进阶系列之一——图层混合模式原理](http://www.zcool.com.cn/article/ZMzcyNzY=.html)：\r\n\r\n![image](https://cloud.githubusercontent.com/assets/8554143/25795589/9c58a612-3408-11e7-8b91-eb08c831be8b.png)\r\n\r\n当然，上图是 PS 中的混合模式，数量比 CSS 中的多出几个，但是分类还是通用的。\r\n\r\n## `mix-blend-mode` 实例\r\n\r\n眼见为实，要会使用 `mix-blend-mode` ，关键还是要迈出使用这一步。这里我写了一个简单的 Demo，包括了所有的混合模式，可以大概试一下各个模式的效果：\r\n\r\n[CodePen Demo](https://codepen.io/Chokcoco/pen/LyOozE)\r\n\r\n当然，仅仅是这样是感受不到混合模式的魅力的，下面就列举几个利用了混合模式制作的 CSS 动画。\r\n\r\n### 使用 `mix-blend-mode: screen` 滤色模式制作 loading 效果\r\n\r\n为了照顾某些访问 codepen 慢同学，特意制作了该效果的 Gif，看看效果：\r\n\r\n![mixmode-loading](https://cloud.githubusercontent.com/assets/8554143/25797192/cb0219fc-340e-11e7-80f9-340f4fa70bc4.gif)\r\n\r\n[CodePen Demo](http://codepen.io/Chokcoco/pen/zwPyWj)\r\n\r\n这里使用了 `mix-blend-mode: screen` 滤色模式，这是一种提亮图像形混合模式。滤色的英文是 screen，也就是两个颜色同时投影到一个屏幕上的合成颜色。具体做法是把两个颜色都反相，相乘，然后再反相。简单记忆为\"让白更白，而黑不变\"。（不一定十分准确，如有错误还请指正）\r\n\r\n我们将三个 div 按照不同延时(`animation-delay`)小幅度旋转起来，来达到一种很显眼很魔性的效果，适合做 loading 图。\r\n\r\n### 使用 `mix-blend-mode: difference` 差值模式\r\n\r\n再举个例子， `mix-blend-mode: difference` 差值模式。查看每个通道中的颜色信息，比较底色和绘图色，用较亮的像素点的像素值减去较暗的像素点的像素值。与白色混合将使底色反相；与黑色混合则不产生变化。\r\n\r\n通俗一点就是上方图层的亮区将下方图层的颜色进行反相，暗区则将颜色正常显示出来，效果与原图像是完全相反的颜色。\r\n\r\n看看利用了这个混合模式，运用在一些多图层效果里，可以产生十分绚烂的混合效果：\r\n\r\n![mixmode-different](https://cloud.githubusercontent.com/assets/8554143/25800098/7dccd4fe-341a-11e7-832c-f321e77ddac7.gif)\r\n\r\n[CodePen Demo](http://codepen.io/Chokcoco/pen/VbMrKX)\r\n\r\n上图看似复杂，其实了解原理之后非常的简单，6 个旋转的 div ，通过 `mix-blend-mode: difference` 混合在一起。\r\n\r\n### 使用多混合模式制作文字故障效果\r\n\r\n最后，想到我之前制作的一个文字故障效果，也可以很好的融合混合模式，制作出下列效果：\r\n\r\n![mixmode-word-break](https://cloud.githubusercontent.com/assets/8554143/25801329/a5d9db90-341f-11e7-9c25-f98afdb9d662.gif)\r\n\r\n[CodePen Demo](https://codepen.io/Chokcoco/pen/RVxbWW)\r\n\r\n不用怀疑你的眼睛，上图的效果是纯 CSS 实现的效果，运用了多种颜色混合模式实现颜色叠加，变亮等效果。\r\n\r\n本文涉及的专业理论知识很少，没有用很大的篇幅去描述每一个混合模式的效果及作用。我对混合模式的理解也比较粗浅，本文旨在通过一些 Demo 让读者学会开始去使用这些混合模式效果，俗话说修行在个人，如果真的感兴趣的可以自行深入研究。\r\n\r\n最后，看一眼兼容性吧，这种奇妙的属性兼容性通常都不怎么好，我之前几篇文章也提到过了，面向未来编程，所以本文的 CodePen Demo 都要求在 `-webkit-` 内核浏览器下观看：\r\n\r\n![image](https://cloud.githubusercontent.com/assets/8554143/25801631/0e28dfec-3421-11e7-8199-0aef5eab46b4.png)\r\n\r\n## 最后\r\n\r\n本文有下半篇：[不可思议的混合模式 background-blend-mode](https://github.com/chokcoco/iCSS/issues/31)，可以配合阅读，效果更好。"},"comment":{"url":"https://api.github.com/repos/chokcoco/iCSS/issues/comments/441894682","html_url":"https://github.com/chokcoco/iCSS/issues/16#issuecomment-441894682","issue_url":"https://api.github.com/repos/chokcoco/iCSS/issues/16","id":441894682,"node_id":"MDEyOklzc3VlQ29tbWVudDQ0MTg5NDY4Mg==","user":{"login":"Lost-Kun","id":25836355,"node_id":"MDQ6VXNlcjI1ODM2MzU1","avatar_url":"https://avatars1.githubusercontent.com/u/25836355?v=4","gravatar_id":"","url":"https://api.github.com/users/Lost-Kun","html_url":"https://github.com/Lost-Kun","followers_url":"https://api.github.com/users/Lost-Kun/followers","following_url":"https://api.github.com/users/Lost-Kun/following{/other_user}","gists_url":"https://api.github.com/users/Lost-Kun/gists{/gist_id}","starred_url":"https://api.github.com/users/Lost-Kun/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Lost-Kun/subscriptions","organizations_url":"https://api.github.com/users/Lost-Kun/orgs","repos_url":"https://api.github.com/users/Lost-Kun/repos","events_url":"https://api.github.com/users/Lost-Kun/events{/privacy}","received_events_url":"https://api.github.com/users/Lost-Kun/received_events","type":"User","site_admin":false},"created_at":"2018-11-27T03:54:13Z","updated_at":"2018-11-27T03:54:13Z","author_association":"NONE","body":" 很有意思👍"}}
{"actor":{"display_login":"Lost-Kun"}}