向汉堡菜单添加动画

时间:2021-01-11 16:15:12

标签: javascript html css vue.js vuejs2

我正在尝试创建一个带有动画的汉堡包菜单。我尝试在 y 轴上添加变换,但它不会随着动画滑出。链接也有下划线。我添加了 text-decoration: none 但链接仍然有下划线。对于这两个错误,我在代码中做错了什么?

codesandbox

<template>
  <nav>
    <div class="brand">brandName</div>

    <ul class="nav-links" :class="'nav-links--' + isOpen">
      <li v-for="(list, $index) in navLinks" :key="$index">
        <a :href="list.link">{{ list.name }}</a>
      </li>
      <li id="button-part">
        <button>Hello</button>
      </li>
    </ul>

    <div @click="mobileNav()" class="burger">
      <i class="fa fa-bars"></i>
    </div>
  </nav>
</template>

<script>
export default {
  name: "HelloWorld",
  data() {
    return {
      current: 0,
      isOpen: "closed",
      navLinks: [
        {
          name: "Home",
          link: "/home",
        },
        { name: "About", link: "/about" },
        {
          name: "Contacts",
          link: "/contact",
        },
      ],
    };
  },

  methods: {
    mobileNav() {
      if (this.isOpen === "closed") {
        this.isOpen = "open";
      } else {
        this.isOpen = "closed";
      }
      console.log(this.isOpen);
    },
  },
};
</script>

<style>
ul.nav-links li {
  list-style: none;
  text-decoration: none;
}
nav {
  display: flex;
  justify-content: space-around;
  background-color: grey;
  height: 20px;
  width: 100%;
  margin: 0;
  padding: 0;
  text-decoration: none;
}
.brand {
  margin-left: 2px;
}
ul.nav-links {
  text-decoration: none;
  position: absolute;
  display: flex;
  flex-direction: column;
  border-top: solid 1px black;
  width: 100%;
  top: 10px;
  background-color: red;
  align-items: center;
  opacity: 0.8;
  transition: transform 5s;
  transform: scaleY(1);
}

.nav-links--open {
  display: block;
  transform: scaleY(1);
}
.nav-links--closed {
  display: none !important;

  transform: scaleY(0);
}
.burger {
  display: block;
}
#button-part {
  margin-top: 2px;
}

@media (min-width: 768px) {
  .brand {
    margin-left: 5px;
  }

  ul.nav-links {
    position: absolute;
    display: flex;
    flex-direction: row;
    border-top: solid 1px black;
    align-items: center;
    justify-content: center;
    width: 100%;
    top: 10px;
    background-color: red;
    opacity: 0.8;
    transition: transform 0.5s ease-in;
    color: white;
    text-decoration: none;
  }
  .nav-links--open {
    display: block;
  }
  .nav-links--closed {
    display: none !important;
  }
  ul.nav-links li {
    opacity: 1;
    padding: 0 25px;
    text-decoration: none;
  }

  .burger {
    display: block;
    color: black;
  }
}

@media (min-width: 1200px) {
  nav {
    background-color: red;
    display: flex;
    justify-content: space-around;
  }
  .brand {
    display: flex;
    align-items: center;
    color: black;
    font-size: 20px;
    margin-left: 20px;
  }
  ul.nav-links {
    position: static;
    display: flex;
    flex-direction: row;
    align-items: center;
    justify-content: flex-end;
    border: none;
    text-decoration: none;
    padding-top: 0px;
    margin-top: 9px;
  }

  ul.nav-links li {
    color: black;
    text-decoration: none;
  }
  .burger {
    display: none;
    cursor: pointer;
  }
  .brand > img {
    height: 50px;
    width: 85px;
  }
}
</style>

1 个答案:

答案 0 :(得分:2)

对于链接下划线,请使用:

a{ text-decoration: none; }

更新

对于转换问题,请检查此问题:CSS3 transform not working