具有静态文本的CSS旋转器

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

CSS-Spinner with static text inside

问题

我尝试创建一个使用CSS实现的非常简单的加载动画。

  1. <div class="spinner">abc</div>

如何让旋转动画内部的文本保持静态,而不需要在旋转动画外部添加另一个元素?我不想使用库来实现这个效果,但如果有助于解决问题,我可以使用jQuery。但它应该简单而易于实现。

英文:

I try to get a very simple loading animation that is implemented with CSS.

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

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

  1. .spinner
  2. {
  3. border: 5px solid red;
  4. border-top: 5px solid green;
  5. border-radius: 50%;
  6. width: 30px;
  7. height: 30px;
  8. animation: spin 3s linear infinite;
  9. }
  10. @keyframes spin
  11. {
  12. 0% { transform: rotate(0deg); }
  13. 100% { transform: rotate(360deg); }
  14. }

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

  1. 123
  2. &lt;div class=&quot;spinner&quot;&gt;abc&lt;/div&gt;
  3. 987

<!-- end snippet -->

Any way I get the text inside the spinner to be static and not turning without having another element outside the spinner?

I do not want to integrate libs for that. But I use jQuery if that helps. But it should be simple and easy.

答案1

得分: 4

你可以使用::before来在文本周围添加一个旋转的加载图标。以下是一个示例代码:

  1. .spinner {
  2. position: relative;
  3. display: flex;
  4. justify-content: center;
  5. align-items: center;
  6. width: 200px;
  7. height: 200px;
  8. background-color: aliceblue;
  9. border-radius: 50%;
  10. }
  11. .spinner::before {
  12. content: "";
  13. position: absolute;
  14. top: -10px;
  15. left: -10px;
  16. right: -10px;
  17. z-index: -100;
  18. bottom: -10px;
  19. background: rgb(82,210,56);
  20. background: linear-gradient(15deg, rgba(82,210,56,1) 0%, rgba(135,107,184,1) 11%, rgba(106,190,96,1) 43%, rgba(209,35,35,1) 79%, rgba(217,186,173,1) 100%);
  21. border-radius: 50%;
  22. transform-origin: center center;
  23. animation: spin 2s linear infinite;
  24. }
  25. @keyframes spin {
  26. 0% {
  27. transform: rotate(0);
  28. }
  29. 100% {
  30. transform: rotate(360deg);
  31. }
  32. }
  1. <div class="spinner">
  2. asdasd
  3. <br>
  4. asdasd
  5. </div>
英文:

You can use ::before to add a spinner around text.
here is a sample code

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

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

  1. .spinner
  2. {
  3. position: relative;
  4. display: flex;
  5. justify-content: center;
  6. align-items: center;
  7. width: 200px;
  8. height: 200px;
  9. background-color: aliceblue;
  10. border-radius: 50%;
  11. }
  12. .spinner::before {
  13. content: &quot;&quot;;
  14. position: absolute;
  15. top: -10px;
  16. left: -10px;
  17. right: -10px;
  18. z-index: -100;
  19. bottom: -10px;
  20. background: rgb(82,210,56);
  21. background: linear-gradient(15deg, rgba(82,210,56,1) 0%, rgba(135,107,184,1) 11%, rgba(106,190,96,1) 43%, rgba(209,35,35,1) 79%, rgba(217,186,173,1) 100%);
  22. border-radius: 50%;
  23. transform-origin: center center;
  24. animation: spin 2s linear infinite;
  25. }
  26. @keyframes spin {
  27. 0% {
  28. transform: rotate(0);
  29. }
  30. 100% {
  31. transform: rotate(360deg);
  32. }
  33. }

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

<div class="spinner">
asdasd
<br>
asdasd
</div>

<!-- end snippet -->

答案2

得分: 2

你可以使用一个包装元素来定位其中的元素。

  1. 123
  2. <div class="spinner-container">
  3. <div class="spinner absolute"></div>
  4. <p class="absolute">abc</p>
  5. </div>
  6. 987
英文:

You could use a wrapper element to position your elements inside it.

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

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

  1. .spinner {
  2. right: 0;
  3. border: 5px solid red;
  4. border-top: 5px solid green;
  5. border-radius: 50%;
  6. animation: spin 3s linear infinite;
  7. }
  8. @keyframes spin {
  9. 0% {
  10. transform: rotate(0deg);
  11. }
  12. 100% {
  13. transform: rotate(360deg);
  14. }
  15. }
  16. .spinner-container {
  17. position: relative;
  18. width: 35px;
  19. height: 35px;
  20. }
  21. .spinner-container p {
  22. display: flex;
  23. justify-content: center;
  24. align-items: center;
  25. }
  26. .absolute {
  27. position: absolute;
  28. top: 0;
  29. bottom: 0;
  30. left: 0;
  31. right: 0;
  32. }

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

  1. 123
  2. &lt;div class=&quot;spinner-container&quot;&gt;
  3. &lt;div class=&quot;spinner absolute&quot;&gt;&lt;/div&gt;
  4. &lt;p class=&quot;absolute&quot;&gt;abc&lt;/p&gt;
  5. &lt;/div&gt;
  6. 987

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年6月13日 18:09:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/76463815.html
匿名

发表评论

匿名网友

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

确定