带有平滑效果的sidenav动画

时间:2020-08-15 15:32:19

标签: javascript html css css-animations

我需要制作一个侧面导航菜单

我到目前为止有什么:

function toggle() {
  document
    .querySelector('.nuxt-link-exact-active')
    .classList.remove('nuxt-link-exact-active')

  event.target.classList.add('nuxt-link-exact-active')
}
.default-layout {
  background: purple;
  min-height: 100%;
  display: grid;
  grid-template-columns: 230px 1fr;
}
main {
  background: #000;
  border-radius: 0.9rem;
  margin: 1.5rem 2rem 1.5rem 0;
  padding: 2rem;
}
nav ul {
  display: grid;
  grid-gap: 1rem;
  justify-content: center;
  padding: 5rem 0;
  grid-template-columns: 100%;
  padding-left: 2rem;
  list-style: none;
}
nav ul li .nav-item {
  cursor: pointer;
  color: #fff;
  display: block;
  padding: 1.5rem;
}
nav ul li .nav-item.nuxt-link-exact-active {
  background: #000;
  border-top-left-radius: 10px;
  border-bottom-left-radius: 10px;
}
<div class="default-layout">
  <nav>
    <ul>
      <li>
        <a class="nav-item nuxt-link-exact-active" onclick="toggle()"
          >Menu 1</a
        >
      </li>
      <li>
        <a class="nav-item" onclick="toggle()">Menu 2</a>
      </li>
      <li>
        <a class="nav-item" onclick="toggle()">Menu 3</a>
      </li>
      <li>
        <a class="nav-item" onclick="toggle()">Menu 4</a>
      </li>
    </ul>
  </nav>
  <main>
    <p>content</p>
  </main>
</div>

我需要的外观,但没有成功:

enter image description here

除了这种外观外,我还需要在项目之间导航时使用效果,但是我不知道该怎么做,这里有一些我想要的平滑效果示例:

https://dribbble.com/shots/5357786-Desktop-Messenger-Spaces-Animation

感谢所有帮助

2 个答案:

答案 0 :(得分:0)

我现在做造型。以后再添加动画效果。

<style type="text/css">
  .default-layout {
  background: purple;
  min-height: 100%;
  display: grid;
  grid-template-columns: 230px 1fr;
}
main {
  background: #000;
  border-radius: 0.9rem;
  margin: 1.5rem 2rem 1.5rem 0;
  padding: 2rem;
}
nav ul {
  display: grid;
  grid-gap: 1rem;
  justify-content: center;
  padding: 5rem 0;
  grid-template-columns: 100%;
  padding-left: 2rem;
  list-style: none;
}
nav ul li .nav-item {
  cursor: pointer;
  color: #fff;
  display: block;
  padding: 1.5rem;
  transition: width 200ms;
  width: 160px;
  float: right;
}
nav ul li .nav-item.nuxt-link-exact-active {
  background: #000;
  border-top-left-radius: 20px;
  border-bottom-left-radius: 20px;
  position:relative;
  z-index:9999;
}
nav ul li:first-child .nav-item.nuxt-link-exact-active.tobottom{
  animation: fadeInbottomsmall 1s ease-in-out;
}
nav ul li:last-child .nav-item.nuxt-link-exact-active.totop{
  animation: fadeIntopsmall 1s ease-in-out;
}
nav ul li .nav-item.nuxt-link-exact-active.totop{
  animation: fadeIntop 1s ease-in-out;
} 
nav ul li .nav-item.nuxt-link-exact-active.tobottom{
  animation: fadeInbottom 1s ease-in-out;
} 
nav ul li .nav-item:before{
  position:absolute;
  top: -50px;
  right: 0;
  width: 50px;
  height: 50px;
  border-radius: 54% 0 40% 0;
  content:'';
  z-index:9999999;
}
nav ul li .nav-item.nuxt-link-exact-active:before,nav ul li .nav-item.nuxt-link-exact-active:after{
   box-shadow: 15px 15px 0 0 #000;
}
nav ul li .nav-item:after{
  position:absolute;
  bottom: -50px;
  right: 0;
  width: 50px;
  height: 50px;
  border-radius: 0 0 54% 40%;
  content:'';
  transform:rotate(270deg);
  z-index:9999999999;
}
@keyframes fadeInbottom{
  0%{
     transform:translateY(-67px);
  }100%{
     transform:translateY(0px);
  }
}
@keyframes fadeIntop{
  0%{
     transform:translateY(0px);
  }100%{
     transform:translateY(-67px);
  }
}

@keyframes fadeInbottomsmall{
  0%{
     transform:translateY(-33px}!important;
  }100%{
     transform:translateY(0px) !important;
  }
}
@keyframes fadeIntopsmall{
  0%{
     transform:translateY(0px);
  }100%{
     transform:translateY(-33px);
  }
}
</style>


<div class="default-layout">
  <nav>
    <ul>
      <li>
        <a class="nav-item nuxt-link-exact-active tobottom" id="menu1" onclick="anim()"
          >Menu 1</a
        >
      </li>
      <li>
        <a class="nav-item" onclick="anim()" id="menu2">Menu 2</a>
      </li>
      <li>
        <a class="nav-item" onclick="anim()" id="menu3">Menu 3</a>
      </li>
      <li>
        <a class="nav-item" onclick="anim()" id="menu4">Menu 4</a>
      </li>
    </ul>
  </nav>
  <main>
    <p>content</p>
  </main>
</div>

<script type="text/javascript">
  function anim() {
    var all = document.querySelectorAll('.nav-item');
    var prev,prevElement,nxtElement,nxt,dist;
    for (var i = 0; i < all.length; i++) {
       nxtElement = event.target;
      // console.log(prevElement)
      if(all[i].classList.contains('nuxt-link-exact-active')){
        prevElement = all[i];
        prev = i + 1;
        prevElement.classList.remove('nuxt-link-exact-active');
        // nxtElement   = prevElement[i];
      }
        if(all[i]==nxtElement){
          nxt = i+1;
        }
    }
    console.log(prev);
    console.log(nxt);

    if(nxt > prev){
      nxtElement.classList.add('nuxt-link-exact-active','tobottom');
      nxtElement.classList.remove('totop');
      prevElement.classList.remove('tobottom',"totop");
      dist=(nxt-prev)*-67;

    }else{
      nxtElement.classList.add('nuxt-link-exact-active','totop');
      nxtElement.classList.remove('tobottom');
      prevElement.classList.remove('tobottom',"totop");
      dist=((prev-nxt)-1)*-67;
    }
    var style = document.createElement('style');

    // console.log(distance);
  style.type = 'text/css';
  
var keyFrames = '\
  @keyframes fadeInbottom {\
      0% {\
           transform:translateY('+dist+'px);\
      }\
      100% {\
           transform:translateY(0);\
      }\
  };\
    @keyframes fadeIntop {\
      0% {\
           transform:translateY(0px);\
      }\
      100% {\
           transform:translateY('+dist+'px);\
      }\
  };';
  style.innerHTML = keyFrames;
  console.log(style.innerHTML);
  document.head.appendChild(style);
}
</script>

答案 1 :(得分:0)

尝试一下

function toggle() {
  document
    .querySelector('.nuxt-link-exact-active')
    .classList.remove('nuxt-link-exact-active')

  event.target.classList.add('nuxt-link-exact-active')
}
.default-layout {
  background: purple;
  min-height: 100%;
  display: grid;
  grid-template-columns: 230px 1fr;
}
main {
  background: #000;
  border-radius: 0.9rem;
  margin: 1.5rem 2rem 1.5rem 0;
  padding: 2rem;
}
nav ul {
  display: grid;
  grid-gap: 1rem;
  justify-content: center;
  padding: 5rem 0;
  grid-template-columns: 100%;
  padding-left: 2rem;
  list-style: none;
}
nav ul li .nav-item {
  cursor: pointer;
  color: #fff;
  display: block;
  padding: 1.5rem;
    transition: width 300ms;
  width: 120px;
  float: right;
}
nav ul li .nav-item.nuxt-link-exact-active {
  background: #000;
  border-top-left-radius: 20px;
  border-bottom-left-radius: 20px;
  position:relative;
  z-index:9999;
  transition:200ms;
  width: 160px;
}
nav ul li .nav-item:before{
  position:absolute;
  top: -50px;
  right: 0;
  width: 50px;
  height: 50px;
  border-radius: 54% 0 40% 0;
  content:'';
  z-index:-1;
  transition: 300ms;
  transition-timing-function: ease-in-out;
  
}
nav ul li .nav-item.nuxt-link-exact-active:before,nav ul li .nav-item.nuxt-link-exact-active:after{
   box-shadow: 15px 15px 0 0 #000;
   transition: 300ms;
  transition-timing-function: ease-in-out;
}
nav ul li .nav-item:after{
  position:absolute;
  bottom: -50px;
  right: 0;
  width: 50px;
  height: 50px;
  border-radius: 0 0 54% 40%;
  content:'';
  transform:rotate(270deg);
  z-index:-1;
}
<div class="default-layout">
  <nav>
    <ul>
      <li>
        <a class="nav-item nuxt-link-exact-active" onclick="toggle()"
          >Menu 1</a
        >
      </li>
      <li>
        <a class="nav-item" onclick="toggle()">Menu 2</a>
      </li>
      <li>
        <a class="nav-item" onclick="toggle()">Menu 3</a>
      </li>
      <li>
        <a class="nav-item" onclick="toggle()">Menu 4</a>
      </li>
    </ul>
  </nav>
  <main>
    <p>content</p>
  </main>
</div>

您可以根据情况更改宽度大小。