html 跨度内部div悬停效果字母间距效果

fslejnso  于 2023-11-15  发布在  其他
关注(0)|答案(1)|浏览(137)

下面是一个div中的span代码:

  1. <div class="mission-button"><span class="mission">view our mission</span></div>

字符串
下面是CSS:

  1. .mission-button::before {
  2. content:'';
  3. position: absolute;
  4. top: 0;
  5. left: 0;
  6. width: 100%;
  7. height: 100%;
  8. transition: all 1s;
  9. border-left-width: 2px;
  10. border-right-width: 2px;
  11. border-left-style: solid;
  12. border-right-style: solid;
  13. background-color: transparent;
  14. border-color: var(--main-dark);
  15. animation: borderOnLoad 2s forwards;
  16. }
  17. @keyframes changeColor {
  18. 0% {
  19. letter-spacing: 0px;
  20. color: white;
  21. }
  22. 100% {
  23. letter-spacing: 2px;
  24. color: var(--main-dark);
  25. }
  26. }
  27. @keyframes borderOnLoad {
  28. 0% {
  29. width: 100%;
  30. border-left-width: 3px;
  31. border-right-width: 3px;
  32. height: 100%;
  33. top: 0;
  34. color: white;
  35. background-color: var(--main-dark);
  36. }
  37. 50% {
  38. width: 200%;
  39. left: -150px;
  40. border-left-width: 3px;
  41. border-right-width: 3px;
  42. height: 100%;
  43. top: 0;
  44. color: white;
  45. }
  46. 100% {
  47. width: 65%;
  48. left: -150px;
  49. border-left-width: 200px;
  50. border-right-width: 200px;
  51. height: 1px;
  52. top: 30px;
  53. color: var(--main-dark);
  54. background-color: transparent;
  55. }
  56. }
  57. .mission-button {
  58. padding: 20px;
  59. /* background-color: #083958;*/
  60. cursor: pointer;
  61. width: 50%;
  62. position: relative;
  63. margin-top: 20px;
  64. font-size: 20px;
  65. text-align: center;
  66. }
  67. .mission {
  68. letter-spacing: 0px;
  69. -webkit-transition: all 1s ease;
  70. -moz-transition: all 1s ease;
  71. -o-transition: all 1s ease;
  72. transition: all 1s ease;
  73. cursor: pointer;
  74. animation: changeColor 2s forwards;
  75. }
  76. .mission-button:hover .mission {
  77. letter-spacing: 5px;
  78. }
  79. .mission-button:hover::before {
  80. width: 32%;
  81. border-left-width: 200px;
  82. border-right-width: 200px;
  83. height: 1px;
  84. top: 30px;
  85. }


changeColor和borderOnLoad动画按预期工作,但当我悬停div时,我希望span通过以下方式更改其字母间距:

  1. .mission {
  2. letter-spacing: 0px;
  3. -webkit-transition: all 1s ease;
  4. -moz-transition: all 1s ease;
  5. -o-transition: all 1s ease;
  6. transition: all 1s ease;
  7. cursor: pointer;
  8. animation: changeColor 2s forwards;
  9. }
  10. .mission-button:hover .mission {
  11. letter-spacing: 5px;
  12. }


但它做任何事情。我想这是因为已经有一个动画的任务按钮,这是优先于其余的,但我不知道

ncecgwcz

ncecgwcz1#

这是因为您将animation-fill-mode设置为forwards,这将保持最后一个关键帧的样式,包括line-spacing:2px。将最后一个关键帧的原始2px行距移动到.mission可能更有意义:

  1. @keyframes changeColor {
  2. 0% {
  3. letter-spacing: 0px;
  4. color: white;
  5. }
  6. 100% {
  7. color: black;
  8. }
  9. }
  10. .mission {
  11. letter-spacing: 2px;
  12. -webkit-transition: all 1s ease;
  13. -moz-transition: all 1s ease;
  14. -o-transition: all 1s ease;
  15. transition: all 1s ease;
  16. cursor: pointer;
  17. animation: changeColor 2s forwards;
  18. }
  19. .mission-button:hover .mission{
  20. letter-spacing: 5px;
  21. }

字符串

  1. .mission-button::before {
  2. content:'';
  3. position: absolute;
  4. top: 0;
  5. left: 0;
  6. width: 100%;
  7. height: 100%;
  8. transition: all 1s;
  9. border-left-width: 2px;
  10. border-right-width: 2px;
  11. border-left-style: solid;
  12. border-right-style: solid;
  13. background-color: transparent;
  14. border-color: black;
  15. animation: borderOnLoad 2s forwards;
  16. }
  17. @keyframes changeColor {
  18. 0% {
  19. letter-spacing: 0px;
  20. color: white;
  21. }
  22. 100% {
  23. color: black;
  24. }
  25. }
  26. @keyframes borderOnLoad {
  27. 0% {
  28. width: 100%;
  29. border-left-width: 3px;
  30. border-right-width: 3px;
  31. height: 100%;
  32. top: 0;
  33. color: white;
  34. background-color: black;
  35. }
  36. 50% {
  37. width: 200%;
  38. left: -150px;
  39. border-left-width: 3px;
  40. border-right-width: 3px;
  41. height: 100%;
  42. top: 0;
  43. color: white;
  44. }
  45. 100% {
  46. width: 65%;
  47. left: -150px;
  48. border-left-width: 200px;
  49. border-right-width: 200px;
  50. height: 1px;
  51. top: 30px;
  52. color: black;
  53. background-color: transparent;
  54. }
  55. }
  56. .mission-button {
  57. padding: 20px;
  58. /* background-color: #083958;*/
  59. cursor: pointer;
  60. width: 50%;
  61. position: relative;
  62. margin-top: 20px;
  63. font-size: 20px;
  64. text-align: center;
  65. }
  66. .mission {
  67. letter-spacing: 2px;
  68. -webkit-transition: all 1s ease;
  69. -moz-transition: all 1s ease;
  70. -o-transition: all 1s ease;
  71. transition: all 1s ease;
  72. cursor: pointer;
  73. animation: changeColor 2s forwards;
  74. }
  75. .mission-button:hover .mission{
  76. letter-spacing: 5px;
  77. }
  78. .mission-button:hover::before {
  79. width: 32%;
  80. border-left-width: 200px;
  81. border-right-width: 200px;
  82. height: 1px;
  83. top: 30px;
  84. }
  1. <div class="mission-button"><span class="mission">view our mission</span></div>

如果无法更改原始间距,则可以使用!important来覆盖它:

  1. .mission-button:hover .mission{
  2. letter-spacing: 5px !important;
  3. }

  1. .mission-button::before {
  2. content: '';
  3. position: absolute;
  4. top: 0;
  5. left: 0;
  6. width: 100%;
  7. height: 100%;
  8. transition: all 1s;
  9. border-left-width: 2px;
  10. border-right-width: 2px;
  11. border-left-style: solid;
  12. border-right-style: solid;
  13. background-color: transparent;
  14. border-color: black;
  15. animation: borderOnLoad 2s forwards;
  16. }
  17. @keyframes changeColor {
  18. 0% {
  19. letter-spacing: 0px;
  20. color: white;
  21. }
  22. 100% {
  23. letter-spacing: 2px;
  24. color: black;
  25. }
  26. }
  27. @keyframes borderOnLoad {
  28. 0% {
  29. width: 100%;
  30. border-left-width: 3px;
  31. border-right-width: 3px;
  32. height: 100%;
  33. top: 0;
  34. color: white;
  35. background-color: black;
  36. }
  37. 50% {
  38. width: 200%;
  39. left: -150px;
  40. border-left-width: 3px;
  41. border-right-width: 3px;
  42. height: 100%;
  43. top: 0;
  44. color: white;
  45. }
  46. 100% {
  47. width: 65%;
  48. left: -150px;
  49. border-left-width: 200px;
  50. border-right-width: 200px;
  51. height: 1px;
  52. top: 30px;
  53. color: black;
  54. background-color: transparent;
  55. }
  56. }
  57. .mission-button {
  58. padding: 20px;
  59. /* background-color: #083958;*/
  60. cursor: pointer;
  61. width: 50%;
  62. position: relative;
  63. margin-top: 20px;
  64. font-size: 20px;
  65. text-align: center;
  66. }
  67. .mission {
  68. letter-spacing: 0px;
  69. -webkit-transition: all 1s ease;
  70. -moz-transition: all 1s ease;
  71. -o-transition: all 1s ease;
  72. transition: all 1s ease;
  73. cursor: pointer;
  74. animation: changeColor 2s forwards;
  75. }
  76. .mission-button:hover .mission {
  77. letter-spacing: 5px !important;
  78. }
  79. .mission-button:hover::before {
  80. width: 32%;
  81. border-left-width: 200px;
  82. border-right-width: 200px;
  83. height: 1px;
  84. top: 30px;
  85. }
  1. <div class="mission-button"><span class="mission">view our mission</span></div>

或者将文本 Package 在另一个容器中,并在该容器上指定样式:

  1. .mission-button:hover .text-spacing{
  2. letter-spacing: 5px;
  3. }
  4. <div class="mission-button">
  5. <span class="mission">
  6. <div class="text-spacing">view our mission</div>
  7. </span>
  8. </div>

  1. .mission-button::before {
  2. content:'';
  3. position: absolute;
  4. top: 0;
  5. left: 0;
  6. width: 100%;
  7. height: 100%;
  8. transition: all 1s;
  9. border-left-width: 2px;
  10. border-right-width: 2px;
  11. border-left-style: solid;
  12. border-right-style: solid;
  13. background-color: transparent;
  14. border-color: black;
  15. animation: borderOnLoad 2s forwards;
  16. }
  17. @keyframes changeColor {
  18. 0% {
  19. letter-spacing: 0px;
  20. color: white;
  21. }
  22. 100% {
  23. letter-spacing: 2px;
  24. color: black;
  25. }
  26. }
  27. @keyframes borderOnLoad {
  28. 0% {
  29. width: 100%;
  30. border-left-width: 3px;
  31. border-right-width: 3px;
  32. height: 100%;
  33. top: 0;
  34. color: white;
  35. background-color: black;
  36. }
  37. 50% {
  38. width: 200%;
  39. left: -150px;
  40. border-left-width: 3px;
  41. border-right-width: 3px;
  42. height: 100%;
  43. top: 0;
  44. color: white;
  45. }
  46. 100% {
  47. width: 65%;
  48. left: -150px;
  49. border-left-width: 200px;
  50. border-right-width: 200px;
  51. height: 1px;
  52. top: 30px;
  53. color: black;
  54. background-color: transparent;
  55. }
  56. }
  57. .mission-button {
  58. padding: 20px;
  59. /* background-color: #083958;*/
  60. cursor: pointer;
  61. width: 50%;
  62. position: relative;
  63. margin-top: 20px;
  64. font-size: 20px;
  65. text-align: center;
  66. }
  67. .mission {
  68. letter-spacing: 0px;
  69. -webkit-transition: all 1s ease;
  70. -moz-transition: all 1s ease;
  71. -o-transition: all 1s ease;
  72. transition: all 1s ease;
  73. cursor: pointer;
  74. animation: changeColor 2s forwards;
  75. }
  76. .mission-button:hover .text-spacing{
  77. letter-spacing: 5px;
  78. }
  79. .mission-button:hover::before {
  80. width: 32%;
  81. border-left-width: 200px;
  82. border-right-width: 200px;
  83. height: 1px;
  84. top: 30px;
  85. }
  1. <div class="mission-button">
  2. <span class="mission">
  3. <div class="text-spacing">view our mission</div>
  4. </span>
  5. </div>
展开查看全部

相关问题