前端快捷菜单图标按钮交互特效 (附加代码)

准备工作需要引用外部字体库,实际开发中最好就是下载到本地
  1. <!-- 引入font-awesome字体图标库 -->
  2. <link rel="stylesheet" href="https://cdn.staticfile.org/font-awesome/4.7.0/css/font-awesome.css">

HTML代码用于总体布局:
  1. <div class="menu-box">
  2. <!-- 图标按钮 -->
  3. <div class="menu-button">
  4. <div class="line-box">
  5. <div class="line"></div>
  6. <div class="line"></div>
  7. <div class="line"></div>
  8. </div>
  9. </div>
  10. <!-- 菜单列表 -->
  11. <ul class="menu-list">
  12. <li><i class="fa fa-sliders"></i><span>设置</span></li>
  13. <li><i class="fa fa-clone"></i><span>复制</span></li>
  14. <li><i class="fa fa-share-square-o"></i><span>分享</span></li>
  15. <li><i class="fa fa-trash-o"></i><span>删除</span></li>
  16. </ul>
  17. </div>
JS代码用于事件的处理:
  1. // 要操作的元素
  2. const menu_box=document.querySelector('.menu-box');
  3. const menu_button=document.querySelector('.menu-button');
  4. // 为菜单按钮绑定点击事件
  5. menu_button.addEventListener('click',function(){
  6. menu_box.classList.toggle('active');
  7. })
CSS代码:
  1. *{
  2. /* 初始化 */
  3. margin: 0;
  4. padding: 0;
  5. }
  6. body{
  7. height: 100vh;
  8. /* 弹性布局 水平+垂直居中 */
  9. display: flex;
  10. justify-content: center;
  11. align-items: center;
  12. /* 渐变背景 */
  13. background: linear-gradient(to top left,#84a0f4,#c2e9fb);
  14. }
  15. body::before{
  16. content: "点击右下角";
  17. color: #fff;
  18. font-size: 32px;
  19. text-shadow: 0 3px 3px #4c6ed3;
  20. }
  21. .menu-box{
  22. /* 固定定位 右下角 */
  23. position: fixed;
  24. bottom: 40px;
  25. right: 40px;
  26. }
  27. /* 菜单按钮 */
  28. .menu-button{
  29. width: 50px;
  30. height: 50px;
  31. background-color: #5c67ff;
  32. border-radius: 50%;
  33. /* 投影 */
  34. box-shadow: 0 0 0 4px rgba(92,103,255,0.3);
  35. color: #fff;
  36. display: flex;
  37. justify-content: center;
  38. align-items: center;
  39. position: relative;
  40. z-index: 1;
  41. cursor: pointer;
  42. /* 过渡 */
  43. transition: 0.2s ease-in;
  44. }
  45. .menu-button:hover{
  46. background-color: #4854ff;
  47. box-shadow: 0 0 0 8px rgba(92,103,255,0.3);
  48. }
  49. .menu-button .line-box{
  50. width: 20px;
  51. height: 20px;
  52. display: flex;
  53. flex-direction: column;
  54. justify-content: space-between;
  55. cursor: pointer;
  56. transition: transform 0.3s ease-out;
  57. }
  58. /* 菜单按钮图标(三条杠) */
  59. .menu-button .line{
  60. background-color: #fff;
  61. width: 100%;
  62. height: 2px;
  63. border-radius: 2px;
  64. }
  65. /* 前后两条短,中间的长 */
  66. .menu-button .line:first-child{
  67. width: 50%;
  68. /* 设置变换的基点 */
  69. transform-origin: right;
  70. /* 过渡效果 */
  71. transition: transform 0.3s ease-in-out;
  72. }
  73. .menu-button .line:last-child{
  74. width: 50%;
  75. align-self: flex-end;
  76. transform-origin: left;
  77. transition: transform 0.3s ease-in-out;
  78. }
  79. /* 菜单列表 */
  80. .menu-list{
  81. width: 140px;
  82. height: 160px;
  83. background-color: #fff;
  84. border-radius: 8px;
  85. list-style: none;
  86. padding: 6px;
  87. box-shadow: 0 0 4px 4px rgba(92,103,255,0.15);
  88. position: absolute;
  89. right: 15px;
  90. bottom: 15px;
  91. /* 默认隐藏 */
  92. opacity: 0;
  93. transform: scale(0);
  94. /* 设置变换的基点 */
  95. transform-origin: bottom right;
  96. /* 过渡效果 */
  97. transition: 0.3s ease;
  98. /* 过渡延迟 */
  99. transition-delay: 0.1s;
  100. }
  101. /* 菜单项 */
  102. .menu-list li{
  103. display: flex;
  104. align-items: center;
  105. padding: 10px;
  106. color: #1c3991;
  107. cursor: pointer;
  108. position: relative;
  109. /* 默认隐藏 */
  110. opacity: 0;
  111. transform: translateX(-10px);
  112. transition: 0.2s ease-in;
  113. }
  114. .menu-list li:hover{
  115. color: #5c67ff;
  116. }
  117. /* 菜单项下边框 */
  118. .menu-list li::before{
  119. content: "";
  120. width: calc(100% - 24px);
  121. height: 1px;
  122. background-color: rgba(92,103,255,0.1);
  123. position: absolute;
  124. bottom: 0;
  125. left: 12px;
  126. }
  127. /* 最后一项不用下边框 */
  128. .menu-list li:last-child::before{
  129. display: none;
  130. }
  131. /* 菜单项图标 */
  132. .menu-list .fa{
  133. font-size: 18px;
  134. width: 18px;
  135. height: 18px;
  136. display: flex;
  137. justify-content: center;
  138. align-items: center;
  139. }
  140. /* 菜单项文本 */
  141. .menu-list span{
  142. font-size: 14px;
  143. margin-left: 8px;
  144. }
  145. /* 活动态下的部分样式变化 */
  146. /* 三条杠的变化 */
  147. .active .line-box{
  148. transform: rotate(-45deg);
  149. }
  150. .active .line-box .line:first-child{
  151. transform: rotate(-90deg) translateX(1px);
  152. }
  153. .active .line-box .line:last-child{
  154. transform: rotate(-90deg) translateX(-1px);
  155. }
  156. /* 菜单列表的变化 */
  157. .active .menu-list{
  158. opacity: 1;
  159. transform: scale(1);
  160. }
  161. .active .menu-list li{
  162. /* 执行动画:动画名 时长 线性 停留在最后一帧 */
  163. animation: fade-in-item 0.4s linear forwards;
  164. }
  165. /* 接下来为每一项设置动画延迟时间 */
  166. .active .menu-list li:nth-child(1){
  167. animation-delay: 0.1s;
  168. }
  169. .active .menu-list li:nth-child(2){
  170. animation-delay: 0.2s;
  171. }
  172. .active .menu-list li:nth-child(3){
  173. animation-delay: 0.3s;
  174. }
  175. .active .menu-list li:nth-child(4){
  176. animation-delay: 0.4s;
  177. }
  178. /* 定义动画 */
  179. @keyframes fade-in-item {
  180. 100%{
  181. transform: translateX(0);
  182. opacity: 1;
  183. }
  184. }
当我们点击右下角更多的时候会展开一个便捷界列表菜单
效果展示:
返回顶部
  • 提示