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

前言

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

一、示例代码

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

html 复制代码
<template>
  <div class="hamburger-icon">
    <div class="hamburger-icon-box">
      <div class="hamburger-1" :class="isActiveHamburger1 ? '' : 'is-active'" @click="isActiveHamburger1 = !isActiveHamburger1">
        <span class="line" />
        <span class="line" />
        <span class="line" />
      </div>
    </div>

    <div class="hamburger-icon-box">
      <div class="hamburger-2" :class="isActiveHamburger2 ? '' : 'is-active'" @click="isActiveHamburger2 = !isActiveHamburger2">
        <span class="line" />
        <span class="line" />
        <span class="line" />
      </div>
    </div>

    <div class="hamburger-icon-box">
      <div class="hamburger-3" :class="isActiveHamburger3 ? '' : 'is-active'" @click="isActiveHamburger3 = !isActiveHamburger3">
        <span class="line" />
        <span class="line" />
        <span class="line" />
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data: () => ({
    isActiveHamburger1: true,
    isActiveHamburger2: true,
    isActiveHamburger3: true,
  }),
  methods: {
   // Todo 
  }
}
</script>

<style lang="less" scoped>
  .hamburger-icon {
    display: flex;
    width: 100%;
    height: 100%;
    background-color: #2c3e50;

    .hamburger-icon-box {
      position: relative;
      flex: 1;
      display: table;
      margin: auto;
    }

    /* ---- ^ hamburger-1 ---- */
    .hamburger-1 {
      position: relative;
      width: 100px;
      margin: auto;

      &:hover {
        cursor: pointer;
      }

      .line {
        width: 40px;
        height: 5px;
        background-color: #ffffff;
        border-radius: 5px;
        display: block;
        margin: 7.5px auto;
        -webkit-transition: all 0.3s ease-in-out;
        -o-transition: all 0.3s ease-in-out;
        transition: all 0.3s ease-in-out;
      }

      &.is-active .line:nth-child(1) {
        -webkit-transform: translateY(12.5px) rotate(45deg);
        -ms-transform: translateY(12.5px) rotate(45deg);
        -o-transform: translateY(12.5px) rotate(45deg);
        transform: translateY(12.5px) rotate(45deg);
      }

      &.is-active .line:nth-child(2) {
        opacity: 0;
      }

      &.is-active .line:nth-child(3) {
        -webkit-transform: translateY(-12.5px) rotate(-45deg);
        -ms-transform: translateY(-12.5px) rotate(-45deg);
        -o-transform: translateY(-12.5px) rotate(-45deg);
        transform: translateY(-12.5px) rotate(-45deg);
      }
    }
    /* ---- / hamburger-1 ---- */

    /* ---- ^ hamburger-2 ---- */
    .hamburger-2 {
      position: relative;
      -webkit-transition: all 0.3s ease-in-out;
      -o-transition: all 0.3s ease-in-out;
      transition: all 0.3s ease-in-out;

      &:hover {
        cursor: pointer;
      }

      .line {
        width: 40px;
        height: 5px;
        background-color: #ffffff;
        border-radius: 5px;
        display: block;
        margin: 8px auto;
        -webkit-transition: all 0.3s ease-in-out;
        -o-transition: all 0.3s ease-in-out;
        transition: all 0.3s ease-in-out;
      }

      &:before {
        content: "";
        position: absolute;
        -webkit-box-sizing: border-box;
        -moz-box-sizing: border-box;
        box-sizing: border-box;
        width: 70px;
        height: 70px;
        border: 5px solid transparent;
        top: calc(50% - 35px);
        left: calc(50% - 35px);
        border-radius: 100%;
        -webkit-transition: all 0.3s ease-in-out;
        -o-transition: all 0.3s ease-in-out;
        transition: all 0.3s ease-in-out;
      }

      &.is-active {
        -webkit-transform: rotate(45deg);
        -ms-transform: rotate(45deg);
        -o-transform: rotate(45deg);
        transform: rotate(45deg);
      }

      &.is-active:before {
        border: 5px solid #ecf0f1;
      }

      &.is-active .line {
        width: 35px;
      }

      &.is-active .line:nth-child(1) {
        -webkit-transform: translateY(13px);
        -ms-transform: translateY(13px);
        -o-transform: translateY(13px);
        transform: translateY(13px);
      }

      &.is-active .line:nth-child(2) {
        opacity: 0;
      }

      &.is-active .line:nth-child(3) {
        -webkit-transform: translateY(-13px) rotate(90deg);
        -ms-transform: translateY(-13px) rotate(90deg);
        -o-transform: translateY(-13px) rotate(90deg);
        transform: translateY(-13px) rotate(90deg);
      }
    }
    /* ---- / hamburger-2 ---- */

    /* ---- ^ hamburger-3 ---- */
    .hamburger-3 {
      position: relative;
      -webkit-transition: all 0.3s ease-in-out;
      -o-transition: all 0.3s ease-in-out;
      transition: all 0.3s ease-in-out;

      &:hover {
        cursor: pointer;
      }

      .line {
        width: 40px;
        height: 5px;
        background-color: #ffffff;
        border-radius: 5px;
        display: block;
        margin: 8px auto;
        -webkit-transition: all 0.3s ease-in-out;
        -o-transition: all 0.3s ease-in-out;
        transition: all 0.3s ease-in-out;
      }

      &.is-active {
        animation: smallbig 0.6s forwards;
      }

      &.is-active .line:nth-child(1) {
        -webkit-transform: translateY(13px) rotate(45deg);
        -ms-transform: translateY(13px) rotate(45deg);
        -o-transform: translateY(13px) rotate(45deg);
        transform: translateY(13px) rotate(45deg);
      }

      &.is-active .line:nth-child(2) {
        opacity: 0;
      }

      &.is-active .line:nth-child(3) {
        -webkit-transform: translateY(-13px) rotate(-45deg);
        -ms-transform: translateY(-13px) rotate(-45deg);
        -o-transform: translateY(-13px) rotate(-45deg);
        transform: translateY(-13px) rotate(-45deg);
      }

      .hamburger-3.is-active .line:nth-child(1),
      .hamburger-3.is-active .line:nth-child(2),
      .hamburger-3.is-active .line:nth-child(3) {
        -webkit-transition-delay: 0.2s;
        -o-transition-delay: 0.2s;
        transition-delay: 0.2s;
      }

      @keyframes smallbig {
        0%, 100% {
          -webkit-transform: scale(1);
          -ms-transform: scale(1);
          -o-transform: scale(1);
          transform: scale(1);
        }

        50% {
          -webkit-transform: scale(0);
          -ms-transform: scale(0);
          -o-transform: scale(0);
          transform: scale(0);
        }
      }
    }
    /* ---- / hamburger-3 ---- */
  }
</style>

二、运行效果

相关推荐
i听风逝夜33 分钟前
Web 3D地球实时统计访问来源
前端·后端
iMonster37 分钟前
React 组件的组合模式之道 (Composition Pattern)
前端
呐呐呐呐呢1 小时前
antd渐变色边框按钮
前端
元直数字电路验证1 小时前
Jakarta EE Web 聊天室技术梳理
前端
wadesir1 小时前
Nginx配置文件CPU优化(从零开始提升Web服务器性能)
服务器·前端·nginx
牧码岛1 小时前
Web前端之canvas实现图片融合与清晰度介绍、合并
前端·javascript·css·html·web·canvas·web前端
灵犀坠1 小时前
前端面试八股复习心得
开发语言·前端·javascript
9***Y481 小时前
前端动画性能优化
前端
网络点点滴1 小时前
Vue3嵌套路由
前端·javascript·vue.js
牧码岛1 小时前
Web前端之Vue+Element打印时输入值没有及时更新dom的问题
前端·javascript·html·web·web前端