动态地使文本闪烁背景位置动画。

huangapple go评论89阅读模式
英文:

Animating text shimmer background position dynamically

问题

I am using CSS keyframes to add a shimmer effect to my text. The effect is working great but I'm having a hard time determining how to make it work with any length of text since I am currently using pixel values to determine how far to animate the background-position.

  1. h2 {
  2. color: #F5C21B;
  3. background: -webkit-gradient(linear, left top, left bottom, from(red), to(blue));
  4. -webkit-background-clip: text;
  5. -webkit-text-fill-color: transparent;
  6. font-family: 'Open Sans', sans-serif;
  7. font-weight:800;
  8. font-size:80px;
  9. text-transform:uppercase;
  10. position:relative;
  11. }
  12. h2:before {
  13. background-position: -180px;
  14. -webkit-animation: flare 5s infinite;
  15. -webkit-animation-timing-function: linear;
  16. background-image: linear-gradient(65deg, transparent 20%, rgba(255, 255, 255, 0.2) 20%, rgba(255, 255, 255, 0.3) 27%, transparent 27%, transparent 100%);
  17. -webkit-background-clip: text;
  18. -webkit-text-fill-color: transparent;
  19. content:attr(data-title);
  20. color: #FFF;
  21. display: block;
  22. padding-right: 140px;
  23. position: absolute;
  24. }
  25. /* Animations */
  26. @-webkit-keyframes flare {
  27. 0% { background-position: -180px; }
  28. 30% { background-position: 400px; }
  29. 100% { background-position: 400px; }
  30. }

I would like for the "flare" animation to start to the left of the text out of view and end to the right of the text out of view. I tried using percentages for the background-position animation, but then the animation wouldn't work. Is there any way to modify this so that the flare animation works for any length/font-size of text?

英文:

I am using CSS keyframes to add a shimmer effect to my text. The effect is working great but I'm having a hard time determining how to make it work with any length of text since I am currently using pixel values to determine how far to animate the background-position

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

  1. h2 {
  2. color: #F5C21B;
  3. background: -webkit-gradient(linear, left top, left bottom, from(red), to(blue));
  4. -webkit-background-clip: text;
  5. -webkit-text-fill-color: transparent;
  6. font-family: &#39;Open Sans&#39;, sans-serif;
  7. font-weight:800;
  8. font-size:80px;
  9. text-transform:uppercase;
  10. position:relative;
  11. }
  12. h2:before {
  13. background-position: -180px;
  14. -webkit-animation: flare 5s infinite;
  15. -webkit-animation-timing-function: linear;
  16. background-image: linear-gradient(65deg, transparent 20%, rgba(255, 255, 255, 0.2) 20%, rgba(255, 255, 255, 0.3) 27%, transparent 27%, transparent 100%);
  17. -webkit-background-clip: text;
  18. -webkit-text-fill-color: transparent;
  19. content:attr(data-title);
  20. color: #FFF;
  21. display: block;
  22. padding-right: 140px;
  23. position: absolute;
  24. }
  25. /* Animations */
  26. @-webkit-keyframes flare {
  27. 0% { background-position: -180px; }
  28. 30% { background-position: 400px; }
  29. 100% { background-position: 400px; }
  30. }

<!-- language: lang-html -->

  1. &lt;link rel=&quot;preconnect&quot; href=&quot;https://fonts.googleapis.com&quot;&gt;
  2. &lt;link rel=&quot;preconnect&quot; href=&quot;https://fonts.gstatic.com&quot; crossorigin&gt;
  3. &lt;link href=&quot;https://fonts.googleapis.com/css2?family=Open+Sans:wght@800&amp;display=swap&quot; rel=&quot;stylesheet&quot;&gt;
  4. &lt;h2 data-title=&quot;Lorem&quot;&gt;Lorem&lt;/h2&gt;
  5. &lt;h2 data-title=&quot;Ipsum&quot;&gt;Ipsum&lt;/h2&gt;
  6. &lt;h2 data-title=&quot;Lorem Ipsum&quot;&gt;Lorem Ipsum&lt;/h2&gt;
  7. &lt;h2 data-title=&quot;Lorem Ipsum Dolor&quot;&gt;Lorem Ipsum Dolor&lt;/h2&gt;

<!-- end snippet -->

I would like for the "flare" animation to start to the left of the text out of view and end to the right of the text out of view. I tried using percentages for the background-position animation, but then the animation wouldn't work. Is there any way to modify this so that the flare animation works for any length/font-size of text?

答案1

得分: 1

我首先会考虑一个文本层并更新代码如下。我有一个详细的答案,解释了background-sizebackground-position之间的关系。

  1. h2 {
  2. background:
  3. /* gradient for the shimmer */
  4. linear-gradient(65deg,
  5. #0000 calc(50% - 50px), #fff5 0,
  6. #fffb calc(50% + 50px), #0000 0),
  7. /* gradient for the text */
  8. linear-gradient(45deg, red, blue);
  9. /* the sise of the first gradient is bigger than 100% */
  10. background-size: calc(200% + 100px) 100%,100%;
  11. -webkit-background-clip: text;
  12. -webkit-text-fill-color: transparent;
  13. animation: flare 3s infinite linear;
  14. font-family: sans-serif;
  15. font-weight:800;
  16. font-size:80px;
  17. text-transform:uppercase;
  18. margin: 0;
  19. width: fit-content:
  20. }
  1. <h2>Lorem</h2>
  2. <h2>Ipsum</h2>
  3. <h2>Lorem Ipsum</h2>
  4. <h2>Lorem Ipsum Dolor</h2>
  1. <details>
  2. <summary>英文:</summary>
  3. I would first consider one text layer and update the code like below. I have [a detailed answer][1] that explain the relation between `background-size` and `background-position`
  4. &lt;!-- begin snippet: js hide: false console: true babel: false --&gt;
  5. &lt;!-- language: lang-css --&gt;
  6. h2 {
  7. background:
  8. /* gradient for the shimmer */
  9. linear-gradient(65deg,
  10. #0000 calc(50% - 50px), #fff5 0,
  11. #fffb calc(50% + 50px), #0000 0),
  12. /* gradient for the text */
  13. linear-gradient(45deg, red, blue);
  14. /* the sise of the first gradient is bigger than 100% */
  15. background-size: calc(200% + 100px) 100%,100%;
  16. -webkit-background-clip: text;
  17. -webkit-text-fill-color: transparent;
  18. animation: flare 3s infinite linear;
  19. font-family: sans-serif;
  20. font-weight:800;
  21. font-size:80px;
  22. text-transform:uppercase;
  23. margin: 0;
  24. width: fit-content:
  25. }
  26. /* you animate from right to left */
  27. @keyframes flare {
  28. 0% { background-position: right; }
  29. 30%,100% { background-position: left; }
  30. }
  31. &lt;!-- language: lang-html --&gt;
  32. &lt;h2&gt;Lorem&lt;/h2&gt;
  33. &lt;h2&gt;Ipsum&lt;/h2&gt;
  34. &lt;h2&gt;Lorem Ipsum&lt;/h2&gt;
  35. &lt;h2&gt;Lorem Ipsum Dolor&lt;/h2&gt;
  36. &lt;!-- end snippet --&gt;
  37. [1]: https://stackoverflow.com/q/51731106/8620333
  38. </details>

huangapple
  • 本文由 发表于 2023年5月15日 02:48:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/76249165.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定