|
| 1 | +> 点击勘误[issues](https://github.com/webVueBlog/awesome-css/issues),感谢大家的阅读 |
| 2 | +
|
| 3 | +首先,我们需要一个用于放置窗帘的容器,我们将给它.curtain,我们有一个.invert子元素,它将作为我们的“粘性”框。最后,我们在这个盒子里有了内容。 |
| 4 | + |
| 5 | +```js |
| 6 | +<div class="curtain"> |
| 7 | + <div class="invert"> |
| 8 | + <h2>Section title</h2> |
| 9 | + </div> |
| 10 | +</div> |
| 11 | +``` |
| 12 | + |
| 13 | +## 让我们设置一些 CSS 变量 |
| 14 | + |
| 15 | +需要三个值。让我们用它们制作 CSS 变量: |
| 16 | + |
| 17 | +- `--minh` 容器高度 |
| 18 | +- `--color1` 浅颜色 |
| 19 | +- `--color2` 深颜色 |
| 20 | + |
| 21 | +```js |
| 22 | +:root { |
| 23 | + --minh: 98vh; |
| 24 | + --color1: wheat; |
| 25 | + --color2: midnightblue; |
| 26 | +} |
| 27 | +``` |
| 28 | + |
| 29 | +## 是时候拉开帷幕了 |
| 30 | + |
| 31 | +- `linear-gradient` 代表“分裂”背景 |
| 32 | +- `min-height` 容器底部的额外空间 |
| 33 | + |
| 34 | +我们使用`::after`伪元素将额外的空间添加到底部。这样,我们的“粘性”内容实际上会在滚动经过`::after`元素时粘在容器上。 |
| 35 | + |
| 36 | +```js |
| 37 | +.curtain { |
| 38 | + /** create the "split" background **/ |
| 39 | + background-image: linear-gradient(to bottom, var(--color2) 50%, var(--color1) 50%); |
| 40 | +} |
| 41 | + |
| 42 | +/** add extra space to the bottom (need this for the "sticky" effect) **/ |
| 43 | +.curtain::after { |
| 44 | + content: ""; |
| 45 | + display: block; |
| 46 | + min-height: var(--minh); |
| 47 | +} |
| 48 | +``` |
| 49 | + |
| 50 | +## 制作粘性内容 |
| 51 | + |
| 52 | +接下来,我们需要使我们的内容“粘性”,因为它完全位于容器内,因为背景和文本交换颜色值。事实上,我们已经为.curtain的子元素提供了一个.invert可以用作粘性容器的类。 |
| 53 | + |
| 54 | + |
| 55 | +- position: sticky并top定义粘性。 |
| 56 | +- mix-blend-mode: difference 将元素内部内容的颜色混合到.curtain的背景渐变中。 |
| 57 | +- display: flex将演示内容居中。 |
| 58 | +- min-height定义容器的高度并允许底部的额外空间。 |
| 59 | +- color设置标题的颜色。 |
| 60 | + |
| 61 | +```js |
| 62 | +.invert { |
| 63 | + /** make the content sticky **/ |
| 64 | + position: sticky; |
| 65 | + top: 20px; |
| 66 | + |
| 67 | + /** blend the content with the contrast effect **/ |
| 68 | + mix-blend-mode: difference; |
| 69 | + |
| 70 | + /** center the content **/ |
| 71 | + display: flex; |
| 72 | + align-items: center; |
| 73 | + justify-content: center; |
| 74 | + |
| 75 | + /** set the minimum height of the section **/ |
| 76 | + min-height: var(--minh); |
| 77 | +} |
| 78 | + |
| 79 | +h2 { |
| 80 | + /** set the color of the text **/ |
| 81 | + color: var(--color1); |
| 82 | +} |
| 83 | +``` |
| 84 | + |
| 85 | +.curtain::after内容的高度是使用 CSS 变量设置的,其值与伪元素的高度值相同。 |
| 86 | + |
| 87 | +声明将mix-blend-mode: difference我们的内容与背景融合在一起。该difference值很复杂,但您可能会将其可视化为背景的反转文本颜色。 |
| 88 | + |
| 89 | +<iframe height="300" style="width: 100%;" scrolling="no" title="mix-blend-mode" src="https://codepen.io/webvueblog/embed/RwQpYdw?default-tab=css%2Cresult" frameborder="no" loading="lazy" allowtransparency="true" allowfullscreen="true"> |
| 90 | + See the Pen <a href="https://codepen.io/webvueblog/pen/RwQpYdw"> |
| 91 | + mix-blend-mode</a> by 我是哪吒(达达) (<a href="https://codepen.io/webvueblog">@webvueblog</a>) |
| 92 | + on <a href="https://codepen.io">CodePen</a>. |
| 93 | +</iframe> |
| 94 | + |
| 95 | +## “拉开窗帘”演示 |
| 96 | + |
| 97 | +<iframe height="300" style="width: 100%;" scrolling="no" title="pullCurtain" src="https://codepen.io/webvueblog/embed/vYdxzwY?default-tab=css%2Cresult" frameborder="no" loading="lazy" allowtransparency="true" allowfullscreen="true"> |
| 98 | + See the Pen <a href="https://codepen.io/webvueblog/pen/vYdxzwY"> |
| 99 | + pullCurtain</a> by 我是哪吒(达达) (<a href="https://codepen.io/webvueblog">@webvueblog</a>) |
| 100 | + on <a href="https://codepen.io">CodePen</a>. |
| 101 | +</iframe> |
| 102 | + |
0 commit comments