CSS3 mix-blend-mode的使用

 CSS3 新增了一个很有意思的属性 -- mix-blend-mode ,其中 mix 和 blend 的中文意译均为混合,那么这个属性的作用直译过来就是混合混合模式,当然,我们我们通常称之为混合模式。 -->

 mix-blend-mode 用于多个不同标签间的混合模式 

 mix-blend-mode 描述了元素的内容应该与元素的直系父元素的内容和元素的背景如何混合。我们将 PS 中图层的概念替换为 HTML 中的元素。 

 CSS混合模式属性主要包括mix-blend-mode、isolation和background-blend-mode三个属性,

mix-blend-mode: normal; // 正常

mix-blend-mode: multiply; // 正片叠底

mix-blend-mode: screen; // 滤色

mix-blend-mode: overlay; // 叠加

mix-blend-mode: darken; // 变暗

mix-blend-mode: lighten; // 变亮

mix-blend-mode: color-dodge; // 颜色减淡

mix-blend-mode: color-burn; // 颜色加深

mix-blend-mode: hard-light; // 强光

mix-blend-mode: soft-light; // 柔光

mix-blend-mode: difference; // 差值

mix-blend-mode: exclusion; // 排除

mix-blend-mode: hue; // 色相

mix-blend-mode: saturation; // 饱和度

mix-blend-mode: color; // 颜色

mix-blend-mode: luminosity; // 亮度

mix-blend-mode: initial;

mix-blend-mode: inherit;

mix-blend-mode: unset;

 按效果来分可以分为这几类

基础混合模式  normal  利用图层透明度和不透明度来控制与下面的图层混合

降暗混合模式   darken,multiply,color-burn  减色模式,滤掉图像中高亮色,从而达到图像变暗

加亮混合模式  screen,lighten,color-dodge  加色模式,滤掉图像中暗色,从而达到图像变亮

融合混合模式  overlay,soft-light,hard-light   用于不同程度的对上、下两图层的融合

变异混合模式  difference,exclusion,hard-light 用于制作各种变异的图层混合

色彩叠加混合模式    hue,saturation,color,luminosity 根据图层的色相,饱和度等基本属性,完成图层融合 

下面就看个小案例

先看看效果图

通过改变mix-blend-mode的值来实现不同的效果

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>mix-blend-mode</title>
</head>
<style>
    body {
        width: 100%;
        background-color: #fff;
        margin: 0 auto;
    }

    .container {
        position: relative;
        width: 150px;
        height: 150px;
        margin: 100px auto;
    }

    .container>div {
        position: absolute;
        top: 0;
        left: 0;
        width: 150px;
        height: 150px;
        border-radius: 50%;
        mix-blend-mode: normal;
    }

    .title {
        color: #333;
        font-size: 24px;
        line-height: 32px;
        text-align: center;
    }

    .red {
        background-color: rgba(255, 0, 0, .8);
        transform: translateX(25%);
    }

    .blue {
        background-color: rgba(0, 255, 0, .8);
        transform: translateX(-25%);
    }

    .white {
        background-color: rgba(0, 0, 255, .8);
        transform: translateY(-25%);
    }

    .u-icon_box {
        /*容器必须有背景*/
        width: 100%;
        text-align: center;
    }

    .img {
        background: #09f;
        display: inline-block;
    }

    .img img {
        width: 100px;
        vertical-align: top;
    }

    img:hover {
        /*设置混合模式*/
        mix-blend-mode: lighten;
    }
</style>

<body>
    <h1 class="title">Mix Blend Mode:
        <select id="select">
            <option value="normal">normal -- 正常</option>
            <option value="multiply">multiply -- 正片叠底</option>
            <option value="screen">screen -- 滤色</option>
            <option value="overlay">overlay -- 叠加</option>
            <option value="darken">darken -- 变暗</option>
            <option value="lighten">lighten -- 变亮</option>
            <option value="color-dodge">color-dodge -- 颜色减淡</option>
            <option value="color-burn">color-burn -- 颜色加深</option>
            <option value="hard-light">hard-light -- 强光</option>
            <option value="soft-light">soft-light -- 柔光</option>
            <option value="difference">difference -- 差值</option>
            <option value="exclusion">exclusion -- 排除</option>
            <option value="hue">hue -- 色相</option>
            <option value="saturation">saturation -- 饱和度</option>
            <option value="color">color -- 颜色</option>
            <option value="luminosity">luminosity -- 亮度</option>
        </select>
    </h1>

    <div class="container">
        <div class="mode red"></div>
        <div class="mode blue"></div>
        <div class="mode white"></div>

    </div>
    <div class="u-icon_box">
        <div class="img">
            <img id="u-icon" src="https://user-gold-cdn.xitu.io/2018/11/18/16724f3cbf3be921?imageView2/0/w/1280/h/960/format/webp/ignore-error/1"
                class="u-icon" />
        </div>
    </div>

    <script>
        let select = document.getElementById('select');
        let mode = document.getElementsByClassName("mode");
        let uicon = document.getElementById('u-icon')
        let value1;
        select.addEventListener('click', changeEvent);
        function changeEvent() {
            value = select.value;
            for (let item of mode) {
                item.style.cssText = 'mix-blend-mode:' + value
            }
            uicon.style.cssText = 'mix-blend-mode:' + value
        };
           </script>
</body>

</html>

下面我们在来看一个例子,同过改变透明度、背景颜色、mix-blend-mode的值来改变色调,来实现不同的图片效果

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>mix-blend-mode</title>
</head>
<style>
    .selectBox {
        display: flex;
        justify-content: space-around;
        align-items: center;
        margin-bottom: 200px;
    }

    .title {
        color: #333;
        font-size: 24px;
        line-height: 32px;
        text-align: center;
    }

    .box {
        width: 600px;
        height: 600px;
        margin: 0 auto;
        background: green;
        text-align: center;

    }

    .box img {
        margin-top: 50px;
        width: 300px;
        height: 500px;
        /* mix-blend-mode: normal; */
        /* opacity: 1; */
    }
</style>

<body>
    <div class="selectBox">
        <h1 class="title">Opacity:
            <select id="select1">
                <option value="1">1</option>
                <option value="0">0</option>
                <option value="0.1">0.1</option>
                <option value="0.2">0.2</option>
                <option value="0.3">0.3</option>
                <option value="0.4">0.4</option>
                <option value="0.5">0.5</option>
                <option value="0.6">0.6</option>
                <option value="0.7">0.7</option>
                <option value="0.8">0.8</option>
                <option value="0.9">0.9</option>


            </select>
        </h1>
        <h1 class="title">background-Color:
            <select id="select2">
                <option value="green">green</option>
                <option value="blue">blue</option>
                <option value="yellow">yellow</option>
                <option value="gray">gray</option>
                <option value="greenyellow">greenyellow</option>
                <option value="aqua">aqua</option>
                <option value="pink">pink</option>
                <option value="orange">orange</option>
                <option value="blueviolet">blueviolet</option>
                <option value="wihte">wihte</option>
                <option value="black">black</option>
            </select>
        </h1>
        <h1 class="title">Mix Blend Mode:
            <select id="select3">
                <option value="normal">normal -- 正常</option>
                <option value="multiply">multiply -- 正片叠底</option>
                <option value="screen">screen -- 滤色</option>
                <option value="overlay">overlay -- 叠加</option>
                <option value="darken">darken -- 变暗</option>
                <option value="lighten">lighten -- 变亮</option>
                <option value="color-dodge">color-dodge -- 颜色减淡</option>
                <option value="color-burn">color-burn -- 颜色加深</option>
                <option value="hard-light">hard-light -- 强光</option>
                <option value="soft-light">soft-light -- 柔光</option>
                <option value="difference">difference -- 差值</option>
                <option value="exclusion">exclusion -- 排除</option>
                <option value="hue">hue -- 色相</option>
                <option value="saturation">saturation -- 饱和度</option>
                <option value="color">color -- 颜色</option>
                <option value="luminosity">luminosity -- 亮度</option>
            </select>
        </h1>
    </div>

    <div class="box" id="box">
        <img id='img' src="https://ss0.bdstatic.com/94oJfD_bAAcT8t7mm9GUKT-xh_/timg?image&quality=100&size=b4000_4000&sec=1543226590&di=04749dc122564efa336f4ffb1558b1a5&src=http://p9.pstatp.com/large/pgc-image/15383778782624c35df4332"
            alt="">
    </div>
    <script>
        let select1 = document.getElementById('select1');
        let select2 = document.getElementById('select2');
        let img = document.getElementById("img");
        let box = document.getElementById("box");
        let value1, value2, value3;
        select1.addEventListener('click', changeEvent);
        function changeEvent() {
            value1 = select1.value;
            img.style.cssText = 'opacity:' + value1;
        }
        select2.addEventListener('click', changeEvent2);
        function changeEvent2() {
            value2 = select2.value;
            box.style.cssText = 'background-Color:' + value2;
        }
        select3.addEventListener('click', changeEvent3);
        function changeEvent3() {
            value3 = select3.value;
            img.style.cssText = 'mix-blend-mode:' + value3;
        }

    </script>
</body>

</html>

注意:这个属性的兼容性不是很好,所以使用的时候要注意点

猜你喜欢

转载自blog.csdn.net/dwb123456123456/article/details/84560763