CSS 3.0中的混合模式的妙用

2020-11-26 11:21:59 浏览数 (1)

给大家分享一个用CSS 3.0的混合模式实现的特效,不用给文字设置多种颜色,滚动页面时,能够让文字能够根据背景颜色自动发生改变,效果如下:

以下是代码实现,欢迎大家复制粘贴和收藏。

代码语言:javascript复制
<!DOCTYPE html>
<html lang="en">

    <head>
        <meta charset="UTF-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <title>CSS 3.0中的混合模式的妙用</title>
        <style>
            * {
                margin: 0;
                padding:0;
            }

            .content,
            .wrapper:before {
                box-sizing: border-box;
                display: block;
                font-size: 4em;
                padding: 1em;
                width: 6em;
                height: 3em;
                position: fixed;
                top: 50%;
                left: 50%;
                transform: translate3d(-50%, -50%, 0);
            }

            .content {
                font-weight: bold;
                text-align: center;
                z-index: 1000;
                background: #fff;
                color: #000;
                mix-blend-mode: lighten;
                padding: 0.6rem;
            }

            section {
                background-repeat: no-repeat;
                background-size: cover;
                isolation: isolate;
                position: relative;
                height: 100vh;
                padding: 1em;
            }

            .wrapper {
                clip: rect(auto auto auto auto);
                position: absolute;
                top: 0;
                right: 0;
                bottom: 0;
                left: 0;
            }

            .wrapper:before {
                content: " ";
                padding: 2em 3.5em;
                mix-blend-mode: darken;
            }

            .one {
                background-image: url(https://source.unsplash.com/1920x1080?city);
            }

            .one .wrapper:before {
                background: red;
            }

            .two {
                background-image: url(https://source.unsplash.com/1920x1080?landscape);
            }

            .two .wrapper:before {
                background: #faaa54;
                mix-blend-mode: difference;
            }

            .three {
                background-image: url(https://source.unsplash.com/1920x1080?portrait);
            }

            .three .wrapper:before {
                background: #6c320a;
                mix-blend-mode: screen;
            }

            .four {
                background-image: url(https://source.unsplash.com/1920x1080?stars);
            }

            .four .wrapper:before {
                background: #e4135d;
            }
        </style>
    </head>

    <body>
        <div class="content">文字颜色自适应背景</div>
        <section class="one">
            <div class="wrapper"></div>
        </section>
        <section class="two">
            <div class="wrapper"></div>
        </section>
        <section class="three">
            <div class="wrapper"></div>
        </section>
        <section class="four">
            <div class="wrapper"></div>
        </section>
    </body>

</html>

0 人点赞