CSS 100 挑战 - Day 002

93 阅读1分钟

先看效果吧

image.png

image.png

html

<div class="frame">
  <div class="center ">
    <div class="menu">
      <div class="line-1"></div>
      <div class="line-2"></div>
      <div class="line-3"></div>
    </div>
  </div>
</div>

CSS


// 定义相关变量
$line-height: 8px;
$line-width: 80px;
$line-margin: 14px;
$line-translation-y: $line-height + $line-margin;
$cubic-bezier-in: cubic-bezier(0.30,1,0.70,1);


.frame {
  position: absolute;
  top: 50%;
  left: 50%;
  width: 400px;
  height: 400px;
  margin-top: -200px;
  margin-left: -200px;
  border-radius: 2px;
  box-shadow: 4px 8px 16px 0 rgba(0,0,0,0.1);
  background: #3FAF82;
  overflow: hidden;
  color: #333;
  font-family: 'Open Sans', Helvetica, sans-serif;
  -webkit-font-smoothing: antialiased;
	-moz-osx-font-smoothing: grayscale;
}


.center {
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
}

.menu {
  &:hover {
    cursor: pointer;
  }
  
  & > div {
    width: $line-width;
    height: $line-height;
    background: #FFF;
    border-radius: 3px;
    box-shadow: 0 2px 10px rgba(0, 0, 0, 0.3)
  }
  
  .line-2 {
    margin: 14px 0;
  }
}


.menu--animation--off {
	.line-1 {
		animation: animate-line-1-off .7s ease-in-out;
	}

	.line-2 {
		animation: animate-line-2-off .7s ease-in-out; 
	}

	.line-3 {
		animation: animate-line-3-off .7s ease-in-out;
	}
}


.menu--animation--on {
  .line-1 {
    animation: animate-line-1 .7s $cubic-bezier-in forwards
  }
  .line-2 {
		animation: animate-line-2 .7s $cubic-bezier-in forwards
	}
	.line-3 {
		animation: animate-line-3 .7s $cubic-bezier-in forwards
	}
  
}



// KEYFRAMES
@keyframes animate-line-1 {
  0% {
    transform: rotate(0);
  }
  
  50% {
    transform: translate3d(0, $line-translation-y, 0) rotate(0);
  }
  100% {
    transform: translate3d(0, $line-translation-y, 0) rotate(45deg);
  }
}


@keyframes animate-line-2 {
    0% {
    transform: scale(1);
    opacity: 1;
  }
  100% {
    transform: scale(0);
    opacity: 0;
  }
}

@keyframes animate-line-3 {
	0% {
		transform: translate3d(0, 0, 0) rotate(0);
  }
  50% {
		transform: translate3d(0, -$line-translation-y, 0) rotate(0);
	}
  100% {
		transform: translate3d(0, -$line-translation-y, 0) rotate(135deg);
	}
}

  
  
@keyframes animate-line-1-off {
    0% {
		transform: translate3d(0, $line-translation-y, 0) rotate(45deg);
	}
    50% {
		transform: translate3d(0, $line-translation-y, 0) rotate(0);
	}
   100% {
		transform: translate3d(0, 0, 0) rotate(0);
	  }
  }
  
  
@keyframes animate-line-2-off {
    0% {
      transform: scale(0);
      opacity: 0;
    }
    100% {
      transform: scale(1);
      opacity: 1;
    }
  }
  
@keyframes animate-line-3-off {
   0% {
		transform: translate3d(0, -$line-translation-y, 0) rotate(135deg);
	  }
	50% {
    transform: translate3d(0, -$line-translation-y, 0) rotate(0);
	  }
  100% {
      transform: translate3d(0, 0, 0) rotate(0);
   }
}

JS

let menu = document.getElementsByClassName("menu")[0];


function toggleMenuAnimation() {
	if (menu.classList.contains("menu--animation--on")) {
		menu.classList.remove("menu--animation--on")
		menu.classList.add("menu--animation--off")
		return;
	}
	menu.classList.remove("menu--animation--off")
	menu.classList.add("menu--animation--on")
}

menu.onclick = toggleMenuAnimation;

主要就是给加上帧动画