移动设备中CSS悬停下划线消失

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

CSS hover underline disappears in mobile

问题

I have a button that gets underlined on hover. It works fine on desktop, but the underline is never visible when on mobile. What can I do to fix it?

Codepen of button working correctly

Gif of how underline disappears on mobile

Interestingly if you view the codepen in mobile mode, the underline also disappears.

html

  1. <button id="google" class="button" onclick="window.location.href='https://www.google.com/;'>
  2. <span id="github" class="hover-underline-animation">Google</span>
  3. <svg viewBox="0 0 46 16" xmlns="http://www.w3.org/2000/svg" id="arrow-horizontal">
  4. <path fill="currentColor" transform="translate(30)" d="M8,0,6.545,1.455l5.506,5.506H-30V9.039H12.052L6.545,14.545,8,16l8-8Z" data-name="Path 10" id="Path_10"></path>
  5. </svg>
  6. </button>

css

  1. .button {
  2. border: none;
  3. background: none;
  4. margin-top: 9.5vh;
  5. cursor: pointer;
  6. }
  7. /* Button text */
  8. .button span {
  9. letter-spacing: 0.2em;
  10. font-size: 1.1vw;
  11. font-weight: 500;
  12. /* Padding at end of button text */
  13. padding-right: 0.8vw;
  14. text-transform: uppercase;
  15. }
  16. /* Button arrow */
  17. .button svg {
  18. height: 0;
  19. width: 0;
  20. /* transform: translateX(-0.6em);
  21. transition: all 0.3s ease; */
  22. }
  23. .button:hover svg {
  24. transform: translateX(0);
  25. }
  26. .button:active svg {
  27. transform: scale(0.9);
  28. }
  29. /* Underline animation on hover */
  30. .hover-underline-animation {
  31. position: relative;
  32. /* Padding from button text */
  33. padding-bottom: 0.5em;
  34. }
  35. .hover-underline-animation:after {
  36. content: '';
  37. position: absolute;
  38. width: 85%;
  39. transform: scaleX(0);
  40. /* Thickness of underline */
  41. height: 0.15em;
  42. bottom: 0;
  43. left: 0;
  44. transform-origin: bottom right;
  45. transition: transform 0.25s ease-out;
  46. background-color: black;
  47. }
  48. .button:hover .hover-underline-animation:after {
  49. transform: scaleX(1);
  50. transform-origin: bottom left;
  51. }

I tried playing around with the CSS. If I change scaleX from 0 to 1 in .hover-underline-animation:after, it becomes visible. So I know the underline does exist. But the hover just doesn't play in mobile.

英文:

I have a button that gets underlined on hover. It works fine on desktop, but the underline is never visible when on mobile. What can I do to fix it?

Codepen of button working correctly

Gif of how underline disappears on mobile

Interestingly if you view the codepen in mobile mode, the underline also disappears.

html

  1. &lt;button id=&quot;google&quot; class=&quot;button&quot; onclick=&quot;window.location.href=&#39;https://www.google.com/;&quot;&gt;
  2. &lt;span id=&quot;github&quot; class=&quot;hover-underline-animation&quot;&gt;Google&lt;/span&gt;
  3. &lt;svg viewBox=&quot;0 0 46 16&quot; xmlns=&quot;http://www.w3.org/2000/svg&quot; id=&quot;arrow-horizontal&quot;&gt;
  4. &lt;path fill=&quot;currentColor&quot; transform=&quot;translate(30)&quot; d=&quot;M8,0,6.545,1.455l5.506,5.506H-30V9.039H12.052L6.545,14.545,8,16l8-8Z&quot; data-name=&quot;Path 10&quot; id=&quot;Path_10&quot;&gt;&lt;/path&gt;
  5. &lt;/svg&gt;
  6. &lt;/button&gt;

css

  1. .button {
  2. border: none;
  3. background: none;
  4. margin-top: 9.5vh;
  5. cursor: pointer;
  6. }
  7. /* Button text */
  8. .button span {
  9. letter-spacing: 0.2em;
  10. font-size: 1.1vw;
  11. font-weight: 500;
  12. /* Padding at end of button text */
  13. padding-right: 0.8vw;
  14. text-transform: uppercase;
  15. }
  16. /* Button arrow */
  17. .button svg {
  18. height: 0;
  19. width: 0;
  20. /* transform: translateX(-0.6em);
  21. transition: all 0.3s ease; */
  22. }
  23. .button:hover svg {
  24. transform: translateX(0);
  25. }
  26. .button:active svg {
  27. transform: scale(0.9);
  28. }
  29. /* Underline animation on hover */
  30. .hover-underline-animation {
  31. position: relative;
  32. /* Padding from button text */
  33. padding-bottom: 0.5em;
  34. }
  35. .hover-underline-animation:after {
  36. content: &#39;&#39;;
  37. position: absolute;
  38. width: 85%;
  39. transform: scaleX(0);
  40. /* Thickness of underline */
  41. height: 0.15em;
  42. bottom: 0;
  43. left: 0;
  44. transform-origin: bottom right;
  45. transition: transform 0.25s ease-out;
  46. background-color: black;
  47. }
  48. .button:hover .hover-underline-animation:after {
  49. transform: scaleX(1);
  50. transform-origin: bottom left;
  51. }

I tried playing around with the CSS. If I change scaleX from 0 to 1 in .hover-underline-animation:after, it becomes visible. So I know the underline does exist. But the hover just doesn't play in mobile.

答案1

得分: 2

"hover"在移动设备上不存在。从历史上看,数字化设备无法区分人的手指触碰设备屏幕和悬停在其上。 – Heretic Monkey

英文:

> There is no "hover" in mobile. Historically, digitizers could not
> differentiate between a person's finger touching the device's screen
> and hovering over it. – Heretic Monkey

huangapple
  • 本文由 发表于 2023年6月8日 05:26:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/76427215.html
匿名

发表评论

匿名网友

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

确定