CSS box-shadow实现按钮悬浮效果

た 入场券 2022-11-15 11:19 448阅读 0赞

html代码

  1. <div class="buttons">
  2. <button class="fill">填写</button>
  3. <button class="pulse">脉冲</button>
  4. <button class="close">关闭</button>
  5. <button class="raise">升高</button>
  6. <button class="up">填充</button>
  7. <button class="slide">幻灯片</button>
  8. <button class="offset">偏移</button>
  9. </div>

css样式

  1. .fill:hover, .fill:focus {
  2. box-shadow: inset 0 0 0 2em var(--hover);
  3. }
  4. .pulse:hover, .pulse:focus {
  5. -webkit-animation: pulse 1s;
  6. animation: pulse 1s;
  7. box-shadow: 0 0 0 2em rgba(255, 255, 255, 0);
  8. }
  9. @-webkit-keyframes pulse {
  10. 0% {
  11. box-shadow: 0 0 0 0 var(--hover);
  12. }
  13. }
  14. @keyframes pulse {
  15. 0% {
  16. box-shadow: 0 0 0 0 var(--hover);
  17. }
  18. }
  19. .close:hover, .close:focus {
  20. box-shadow: inset -3.5em 0 0 0 var(--hover), inset 3.5em 0 0 0 var(--hover);
  21. }
  22. .raise:hover, .raise:focus {
  23. box-shadow: 0 0.5em 0.5em -0.4em var(--hover);
  24. -webkit-transform: translateY(-0.25em);
  25. transform: translateY(-0.25em);
  26. }
  27. .up:hover, .up:focus {
  28. box-shadow: inset 0 -3.25em 0 0 var(--hover);
  29. }
  30. .slide:hover, .slide:focus {
  31. box-shadow: inset 6.5em 0 0 0 var(--hover);
  32. }
  33. .offset {
  34. box-shadow: 0.3em 0.3em 0 0 var(--color), inset 0.3em 0.3em 0 0 var(--color);
  35. }
  36. .offset:hover, .offset:focus {
  37. box-shadow: 0 0 0 0 var(--hover), inset 6em 3.5em 0 0 var(--hover);
  38. }
  39. .fill {
  40. --color: #a972cb;
  41. --hover: #cb72aa;
  42. }
  43. .pulse {
  44. --color: #ef6eae;
  45. --hover: #ef8f6e;
  46. }
  47. .close {
  48. --color: #ff7f82;
  49. --hover: #ffdc7f;
  50. }
  51. .raise {
  52. --color: #ffa260;
  53. --hover: #e5ff60;
  54. }
  55. .up {
  56. --color: #e4cb58;
  57. --hover: #94e458;
  58. }
  59. .slide {
  60. --color: #8fc866;
  61. --hover: #66c887;
  62. }
  63. .offset {
  64. --color: #19bc8b;
  65. --hover: #1973bc;
  66. }
  67. button {
  68. color: var(--color);
  69. -webkit-transition: 0.25s;
  70. transition: 0.25s;
  71. }
  72. button:hover, button:focus {
  73. border-color: var(--hover);
  74. color: #fff;
  75. }
  76. body {
  77. color: #fff;
  78. background: #17181c;
  79. font: 300 1em 'Fira Sans', sans-serif;
  80. -webkit-box-pack: center;
  81. -ms-flex-pack: center;
  82. justify-content: center;
  83. -ms-flex-line-pack: center;
  84. align-content: center;
  85. -webkit-box-align: center;
  86. -ms-flex-align: center;
  87. align-items: center;
  88. text-align: center;
  89. min-height: 100vh;
  90. display: -webkit-box;
  91. display: -ms-flexbox;
  92. display: flex;
  93. }
  94. button {
  95. background: none;
  96. border: 2px solid;
  97. font: inherit;
  98. line-height: 1;
  99. margin: 0.5em;
  100. padding: 1em 2em;
  101. }
  102. h1 {
  103. font-weight: 400;
  104. }
  105. code {
  106. color: #e4cb58;
  107. font: inherit;
  108. }

效果图

在这里插入图片描述

发表评论

表情:
评论列表 (有 0 条评论,448人围观)

还没有评论,来说两句吧...

相关阅读