• 使用Vue+CSS实现汉堡图标过渡为叉号图标,有点意思


    前言

    本文给大家分享三个具有过渡效果的汉堡图标,当点击汉堡图标时,过渡为叉号图标。这种具有过渡特效的图标挺炫酷的,感觉一下子给网页增加一点新颖特色。早在2015年左右,国外挺多优秀门户网站都有使用类似的图标,那时不知道怎么实现,现在研究了一下,大概是使用了CSS伪类和动画的技巧来实现。

    一、示例代码

    (1)/src/views/Example/HamburgerIcon/index.vue

    1. <template>
    2. <div class="hamburger-icon">
    3. <div class="hamburger-icon-box">
    4. <div class="hamburger-1" :class="isActiveHamburger1 ? '' : 'is-active'" @click="isActiveHamburger1 = !isActiveHamburger1">
    5. <span class="line" />
    6. <span class="line" />
    7. <span class="line" />
    8. div>
    9. div>
    10. <div class="hamburger-icon-box">
    11. <div class="hamburger-2" :class="isActiveHamburger2 ? '' : 'is-active'" @click="isActiveHamburger2 = !isActiveHamburger2">
    12. <span class="line" />
    13. <span class="line" />
    14. <span class="line" />
    15. div>
    16. div>
    17. <div class="hamburger-icon-box">
    18. <div class="hamburger-3" :class="isActiveHamburger3 ? '' : 'is-active'" @click="isActiveHamburger3 = !isActiveHamburger3">
    19. <span class="line" />
    20. <span class="line" />
    21. <span class="line" />
    22. div>
    23. div>
    24. div>
    25. template>
    26. <script>
    27. export default {
    28. data: () => ({
    29. isActiveHamburger1: true,
    30. isActiveHamburger2: true,
    31. isActiveHamburger3: true,
    32. }),
    33. methods: {
    34. // Todo
    35. }
    36. }
    37. script>
    38. <style lang="less" scoped>
    39. .hamburger-icon {
    40. display: flex;
    41. width: 100%;
    42. height: 100%;
    43. background-color: #2c3e50;
    44. .hamburger-icon-box {
    45. position: relative;
    46. flex: 1;
    47. display: table;
    48. margin: auto;
    49. }
    50. /* ---- ^ hamburger-1 ---- */
    51. .hamburger-1 {
    52. position: relative;
    53. width: 100px;
    54. margin: auto;
    55. &:hover {
    56. cursor: pointer;
    57. }
    58. .line {
    59. width: 40px;
    60. height: 5px;
    61. background-color: #ffffff;
    62. border-radius: 5px;
    63. display: block;
    64. margin: 7.5px auto;
    65. -webkit-transition: all 0.3s ease-in-out;
    66. -o-transition: all 0.3s ease-in-out;
    67. transition: all 0.3s ease-in-out;
    68. }
    69. &.is-active .line:nth-child(1) {
    70. -webkit-transform: translateY(12.5px) rotate(45deg);
    71. -ms-transform: translateY(12.5px) rotate(45deg);
    72. -o-transform: translateY(12.5px) rotate(45deg);
    73. transform: translateY(12.5px) rotate(45deg);
    74. }
    75. &.is-active .line:nth-child(2) {
    76. opacity: 0;
    77. }
    78. &.is-active .line:nth-child(3) {
    79. -webkit-transform: translateY(-12.5px) rotate(-45deg);
    80. -ms-transform: translateY(-12.5px) rotate(-45deg);
    81. -o-transform: translateY(-12.5px) rotate(-45deg);
    82. transform: translateY(-12.5px) rotate(-45deg);
    83. }
    84. }
    85. /* ---- / hamburger-1 ---- */
    86. /* ---- ^ hamburger-2 ---- */
    87. .hamburger-2 {
    88. position: relative;
    89. -webkit-transition: all 0.3s ease-in-out;
    90. -o-transition: all 0.3s ease-in-out;
    91. transition: all 0.3s ease-in-out;
    92. &:hover {
    93. cursor: pointer;
    94. }
    95. .line {
    96. width: 40px;
    97. height: 5px;
    98. background-color: #ffffff;
    99. border-radius: 5px;
    100. display: block;
    101. margin: 8px auto;
    102. -webkit-transition: all 0.3s ease-in-out;
    103. -o-transition: all 0.3s ease-in-out;
    104. transition: all 0.3s ease-in-out;
    105. }
    106. &:before {
    107. content: "";
    108. position: absolute;
    109. -webkit-box-sizing: border-box;
    110. -moz-box-sizing: border-box;
    111. box-sizing: border-box;
    112. width: 70px;
    113. height: 70px;
    114. border: 5px solid transparent;
    115. top: calc(50% - 35px);
    116. left: calc(50% - 35px);
    117. border-radius: 100%;
    118. -webkit-transition: all 0.3s ease-in-out;
    119. -o-transition: all 0.3s ease-in-out;
    120. transition: all 0.3s ease-in-out;
    121. }
    122. &.is-active {
    123. -webkit-transform: rotate(45deg);
    124. -ms-transform: rotate(45deg);
    125. -o-transform: rotate(45deg);
    126. transform: rotate(45deg);
    127. }
    128. &.is-active:before {
    129. border: 5px solid #ecf0f1;
    130. }
    131. &.is-active .line {
    132. width: 35px;
    133. }
    134. &.is-active .line:nth-child(1) {
    135. -webkit-transform: translateY(13px);
    136. -ms-transform: translateY(13px);
    137. -o-transform: translateY(13px);
    138. transform: translateY(13px);
    139. }
    140. &.is-active .line:nth-child(2) {
    141. opacity: 0;
    142. }
    143. &.is-active .line:nth-child(3) {
    144. -webkit-transform: translateY(-13px) rotate(90deg);
    145. -ms-transform: translateY(-13px) rotate(90deg);
    146. -o-transform: translateY(-13px) rotate(90deg);
    147. transform: translateY(-13px) rotate(90deg);
    148. }
    149. }
    150. /* ---- / hamburger-2 ---- */
    151. /* ---- ^ hamburger-3 ---- */
    152. .hamburger-3 {
    153. position: relative;
    154. -webkit-transition: all 0.3s ease-in-out;
    155. -o-transition: all 0.3s ease-in-out;
    156. transition: all 0.3s ease-in-out;
    157. &:hover {
    158. cursor: pointer;
    159. }
    160. .line {
    161. width: 40px;
    162. height: 5px;
    163. background-color: #ffffff;
    164. border-radius: 5px;
    165. display: block;
    166. margin: 8px auto;
    167. -webkit-transition: all 0.3s ease-in-out;
    168. -o-transition: all 0.3s ease-in-out;
    169. transition: all 0.3s ease-in-out;
    170. }
    171. &.is-active {
    172. animation: smallbig 0.6s forwards;
    173. }
    174. &.is-active .line:nth-child(1) {
    175. -webkit-transform: translateY(13px) rotate(45deg);
    176. -ms-transform: translateY(13px) rotate(45deg);
    177. -o-transform: translateY(13px) rotate(45deg);
    178. transform: translateY(13px) rotate(45deg);
    179. }
    180. &.is-active .line:nth-child(2) {
    181. opacity: 0;
    182. }
    183. &.is-active .line:nth-child(3) {
    184. -webkit-transform: translateY(-13px) rotate(-45deg);
    185. -ms-transform: translateY(-13px) rotate(-45deg);
    186. -o-transform: translateY(-13px) rotate(-45deg);
    187. transform: translateY(-13px) rotate(-45deg);
    188. }
    189. .hamburger-3.is-active .line:nth-child(1),
    190. .hamburger-3.is-active .line:nth-child(2),
    191. .hamburger-3.is-active .line:nth-child(3) {
    192. -webkit-transition-delay: 0.2s;
    193. -o-transition-delay: 0.2s;
    194. transition-delay: 0.2s;
    195. }
    196. @keyframes smallbig {
    197. 0%, 100% {
    198. -webkit-transform: scale(1);
    199. -ms-transform: scale(1);
    200. -o-transform: scale(1);
    201. transform: scale(1);
    202. }
    203. 50% {
    204. -webkit-transform: scale(0);
    205. -ms-transform: scale(0);
    206. -o-transform: scale(0);
    207. transform: scale(0);
    208. }
    209. }
    210. }
    211. /* ---- / hamburger-3 ---- */
    212. }
    213. style>

    二、运行效果

  • 相关阅读:
    华为认证 | 11月底这门HCIP认证即将发布!
    [附源码]Python计算机毕业设计Django网上书城网站
    k8s常用命令1
    常量左值引用作为形参来接收右值引用实参所带来的问题
    Elasticsearch与Sping Data框架的集成
    Spring之BeanFactory
    Windows 下 Kafka 2.8.1 启动报错“输入行太长”问题解决方案
    Element Plus table formatter函数返回html内容
    二叉排序树(BST)和平衡二叉树(AVL)基础概念
    vue3 中使用echarts图表——柱状图
  • 原文地址:https://blog.csdn.net/Cai181191/article/details/132033330