|
| 1 | +## 利用 animation-delay 实现文字渐现效果 |
| 2 | + |
| 3 | +利用 animation-delay 实现文字渐现效果 |
| 4 | + |
| 5 | +### 关键点 |
| 6 | + |
| 7 | +1. hover 状态下和非 hover 状态下的 `transition-duration` 是不一样的,是因为取消 hover 过程中,动画消失过程的时间通常是要求更短的; |
| 8 | +2. 借助了 SASS 的循环 `@for $i from 1 to 21 {}` 递增给每个 `span` 和它的伪元素添加了递增的 `transition-delay`; |
| 9 | + |
| 10 | +### 线性渐变 |
| 11 | +HTML: |
| 12 | +```HTML |
| 13 | +<div class="button">Button</div> |
| 14 | +<div class="g-wrap"></div> |
| 15 | +<p><span data-text="Lorem">Lorem</span> <span data-text="ipsum">ipsum</span> <span data-text="dolor">dolor</span> <span data-text="sit">sit</span> <span data-text="amet">amet</span> <span data-text="consectetur">consectetur</span> <span data-text="adipisicing">adipisicing</span> <span data-text="elit.">elit.</span> <span data-text="Mollitia">Mollitia</span> <span data-text="nostrum">nostrum</span> <span data-text="placeat">placeat</span> <span data-text="consequatur">consequatur</span> <span data-text="deserunt">deserunt</span> <span data-text="velit">velit</span> <span data-text="ducimus">ducimus</span> <span data-text="possimus">possimus</span> <span data-text="commodi">commodi</span> <span data-text="temporibus">temporibus</span> <span data-text="debitis">debitis</span> <span data-text="quam.">quam.</span></p> |
| 16 | +``` |
| 17 | + |
| 18 | +CSS: |
| 19 | +```CSS |
| 20 | +html, |
| 21 | +body { |
| 22 | + width: 100%; |
| 23 | + height: 100%; |
| 24 | + display: flex; |
| 25 | + overflow: hidden; |
| 26 | +} |
| 27 | + |
| 28 | +.button { |
| 29 | + // position: fixed; |
| 30 | + top: 0; |
| 31 | + left: 0; |
| 32 | + width: 120px; |
| 33 | + height: 64px; |
| 34 | + line-height: 64px; |
| 35 | + text-align: center; |
| 36 | + border: 1px solid #eee; |
| 37 | + cursor: pointer; |
| 38 | + transition: .3s all; |
| 39 | + z-index: 1; |
| 40 | + |
| 41 | + &:hover { |
| 42 | + background: #eee; |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +.button:hover ~ .g-wrap { |
| 47 | + content: ""; |
| 48 | + position: fixed; |
| 49 | + top: 0; |
| 50 | + left: 0; |
| 51 | + right: 0; |
| 52 | + bottom: 0; |
| 53 | + transition: .3s background linear; |
| 54 | + background: rgba(0, 0, 0, .5); |
| 55 | + // z-index: -1; |
| 56 | +} |
| 57 | + |
| 58 | +p { |
| 59 | + position: relative; |
| 60 | + margin: 50px auto; |
| 61 | + width: 500px; |
| 62 | + height: 250px; |
| 63 | + font-size: 22px; |
| 64 | + line-height: 1.5; |
| 65 | + overflow: hidden; |
| 66 | + z-index: 2; |
| 67 | +} |
| 68 | + |
| 69 | +p span { |
| 70 | + position: relative; |
| 71 | + display: inline-block; |
| 72 | + opacity: 0; |
| 73 | + transform: translateY(15px) translateZ(0); |
| 74 | + transition-property: transform, opacity; |
| 75 | + transition-duration: .3s, .2s; |
| 76 | + transition-timing-function: cubic-bezier(.23,1,.32,1), linear; |
| 77 | + |
| 78 | +} |
| 79 | + |
| 80 | +.button:hover ~ p span { |
| 81 | + opacity: 1; |
| 82 | + color: #ddd; |
| 83 | + transform: translateY(0) translateZ(0); |
| 84 | + transition-property: transform, opacity; |
| 85 | + transition-duration: 1s, .2s; |
| 86 | + transition-timing-function: cubic-bezier(.23,1,.32,1), linear; |
| 87 | + |
| 88 | +} |
| 89 | + |
| 90 | +p span:after, |
| 91 | +p span:before { |
| 92 | + position: absolute; |
| 93 | + content: attr(data-text); |
| 94 | + top: 0; |
| 95 | + left: 0; |
| 96 | + z-index: 1; |
| 97 | + transform: translateZ(0); |
| 98 | + will-change: opacity; |
| 99 | +} |
| 100 | + |
| 101 | +// p span:before { |
| 102 | +// color: #fff; |
| 103 | +// transition: opacity .6s .2s; |
| 104 | +// } |
| 105 | + |
| 106 | +// .button:hover ~ p span:before { |
| 107 | +// opacity: 0; |
| 108 | +// transition: opacity .6s .3s; |
| 109 | +// } |
| 110 | + |
| 111 | +p span:after { |
| 112 | + color: #e62541; |
| 113 | + transition-property: opacity; |
| 114 | + transition-duration: .1s; |
| 115 | + transition-timing-function: cubic-bezier(.23,1,.32,1); |
| 116 | +} |
| 117 | + |
| 118 | +.button:hover ~ p span:after { |
| 119 | + opacity: 0; |
| 120 | + transition-property: opacity; |
| 121 | + transition-duration: .4s; |
| 122 | + transition-timing-function: cubic-bezier(.23,1,.32,1); |
| 123 | +} |
| 124 | + |
| 125 | +@for $i from 1 to 21 { |
| 126 | + p span:nth-child(#{$i}) { |
| 127 | + transition-delay: #{$i * 0.04}s; |
| 128 | + |
| 129 | + &::after { |
| 130 | + transition-delay: #{$i * 0.04 + 0.2}s; |
| 131 | + } |
| 132 | + } |
| 133 | +} |
| 134 | +``` |
| 135 | + |
| 136 | +效果如下(Hover 按钮触发效果): |
| 137 | + |
| 138 | +<iframe height="350" style="width: 100%;" scrolling="no" title="实现文字渐现效果" src="https://codepen.io/Chokcoco/embed/LYLLVGw?default-tab=result&editable=true&theme-id=light" frameborder="no" loading="lazy" allowtransparency="true" allowfullscreen="true"> |
| 139 | + See the Pen <a href="https://codepen.io/Chokcoco/pen/LYLLVGw"> |
| 140 | + 实现文字渐现效果</a> by Chokcoco (<a href="https://codepen.io/Chokcoco">@Chokcoco</a>) |
| 141 | + on <a href="https://codepen.io">CodePen</a>. |
| 142 | +</iframe> |
0 commit comments