如何在相反方向更改导航栏动画?

时间:2019-04-21 17:31:50

标签: javascript jquery html css3 web

因此,我正在创建一个网站设计,我想将导航栏的位置从右上更改为左上,并想将导航栏打开的动画更改为相反的方向。 我的意思是整个导航栏将在右上角。

我在CSS和Web开发方面没有太多经验,所以请帮助我,我在学校的项目中需要它。 谢谢大家! 这是代码:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
     
    <style>
    
body {
  
    font-family: 'Roboto Condensed', sans-serif;  
  font-size: 16px;
    overflow: hidden;
    text-transform: uppercase;
    font-weight: bold;
}
.menu {
  height: 70px;
  width: 70px;
  right: 70px;
  top: 20px;
  text-align: center;
  position: absolute;
  background: #f463c3;
  overflow: hidden;
  transition: all 0.2s ease;
  z-index: 999;
}
.menu.active {
  width:85%;
  transition:width 0.3s;
   -webkit-transition:width .3s;
   -moz-transition: width 0.3s;
    
}
.menu.active .menuContent * {
  opacity: 1;
}
.menu.active span i:nth-child(1) {
  transform: rotate(-45deg) translate(-50%, -50%);
  top: 50%;
}
.menu.active span i:nth-child(2) {
  transform: translateX(-100px);
  opacity: 0;
}
.menu.active span i:nth-child(3) {
  transform: rotate(45deg) translate(-50%, -50%);
  top: 50%;
}
.menu span {
  width: 70px;
  height: 70px;
  position: absolute;
  right: 0;
  cursor: pointer;
  background: #f463c3;
  z-index: 1;
}
.menu span i {
  position: absolute;
  transform-origin: 50% 50%;
  width: 45%;
  height: 2px;
  left: 0;
  right: 0;
  margin: auto;
  background-color: #ccc;
  transition: transform 0.3s ease, opacity 0.1s ease 0.1s;
}
.menu span i:nth-child(1) {
  top: 40%;
}
.menu span i:nth-child(2) {
  top: 50%;
}
.menu span i:nth-child(3) {
  top: 60%;
}
.menu .menuContent {
  position: absolute;
  width: 100%;
  height: 100%;
  line-height: 40px;
  right: 0px;
  text-align: center;
}
.menu .menuContent * {
  opacity: 0;
}
.menu .menuContent ul li {
  display: inline-block;
  margin-left: 50px;
  margin-right: 50px;
  color: #2d3235;
  transition: opacity 0.3s ease 0.3s;
  cursor: pointer;
  position: relative;
}
.menu .menuContent ul li:hover:before {
  opacity: 0.8;
  top: 13px;
  left: 20px;
}
.menu .menuContent ul li:hover:after {
  opacity: 0.8;
  bottom: 13px;
  left: -20px;
}
        
    </style>
</head>
<body>
    <div class='menu'>
  <span class='toggle'>
    <i></i>
    <i></i>
    <i></i>
  </span>
  <div class='menuContent'>
    <ul>
      <li>Home</li>
      <li>About</li>
      <li>Contact</li>
      <li>Testimonial</li>
      <li>About us</li>
    </ul>
  </div>
</div>
 
<script
  src="https://code.jquery.com/jquery-3.3.1.js"
   ></script>
<script>
    $('.toggle').on('click', function() {
	$('.menu').toggleClass('active');
});
    
</script>
</body>
</html>

2 个答案:

答案 0 :(得分:0)

我认为以下代码将为您提供帮助。 transform: rotate(-145deg)代表孩子1,transform: rotate(145deg) translate(-50%, -50%);代表孩子3

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
     
    <style>
    
body {
  
    font-family: 'Roboto Condensed', sans-serif;  
  font-size: 16px;
    overflow: hidden;
    text-transform: uppercase;
    font-weight: bold;
}
.menu {
  height: 70px;
  width: 70px;
  left: 70px;
  top: 20px;
  text-align: center;
  position: absolute;
  background: #f463c3;
  overflow: hidden;
  transition: all 0.2s ease;
  z-index: 999;
}
.menu.active {
  width:85%;
  transition:width 0.3s;
   -webkit-transition:width .3s;
   -moz-transition: width 0.3s;
    
}
.menu span {
width: 70px;
height: 70px;
position: absolute;
right: 0;
cursor: pointer;
background: #f463c3;
z-index: 1;
left: 0;
}
.menu.active .menuContent * {
  opacity: 1;
}
.menu.active span i:nth-child(1) {
  transform: rotate(-145deg) translate(-50%, -50%);
   top: 50%;
}
.menu.active span i:nth-child(2) {
  transform: translateX(-100px);
  opacity: 0;
}
.menu.active span i:nth-child(3) {
  transform: rotate(145deg) translate(-50%, -50%);
  top: 50%;
}
.menu span {
  width: 70px;
  height: 70px;
  position: absolute;
  right: 0;
  cursor: pointer;
  background: #f463c3;
  z-index: 1;
}
.menu span i {
  position: absolute;
  transform-origin: 50% 50%;
  width: 45%;
  height: 2px;
  left: 0;
  right: 0;
  margin: auto;
  background-color: #ccc;
  transition: transform 0.3s ease, opacity 0.1s ease 0.1s;
}
.menu span i:nth-child(1) {
  top: 40%;
}
.menu span i:nth-child(2) {
  top: 50%;
}
.menu span i:nth-child(3) {
  top: 60%;
}
.menu .menuContent {
  position: absolute;
  width: 100%;
  height: 100%;
  line-height: 40px;
  right: 0px;
  text-align: center;
}
.menu .menuContent * {
  opacity: 0;
}
.menu .menuContent ul li {
  display: inline-block;
  margin-left: 50px;
  margin-right: 50px;
  color: #2d3235;
  transition: opacity 0.3s ease 0.3s;
  cursor: pointer;
  position: relative;
}
.menu .menuContent ul li:hover:before {
  opacity: 0.8;
  top: 13px;
  left: 20px;
}
.menu .menuContent ul li:hover:after {
  opacity: 0.8;
  bottom: 13px;
  left: -20px;
}
        
    </style>
</head>
<body>
    <div class='menu'>
  <span class='toggle'>
    <i></i>
    <i></i>
    <i></i>
  </span>
  <div class='menuContent'>
    <ul>
      <li>Home</li>
      <li>About</li>
      <li>Contact</li>
      <li>Testimonial</li>
      <li>About us</li>
    </ul>
  </div>
</div>
 
<script
  src="https://code.jquery.com/jquery-3.3.1.js"
   ></script>
<script>
    $('.toggle').on('click', function() {
	$('.menu').toggleClass('active');
});
    
</script>
</body>
</html>

答案 1 :(得分:0)

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
     
    <style>
    
body {
  
    font-family: 'Roboto Condensed', sans-serif;  
  font-size: 16px;
    overflow: hidden;
    text-transform: uppercase;
    font-weight: bold;
}
.menu {
  height: 70px;
  width: 70px;
  left: 70px;
  top: 20px;
  text-align: center;
  position: absolute;
  background: #f463c3;
  overflow: hidden;
  transition: all 0.2s ease;
  z-index: 999;
}
.menu.active {
  width:85%;
  transition:width 0.3s;
   -webkit-transition:width .3s;
   -moz-transition: width 0.3s;
    
}
.menu.active .menuContent * {
  opacity: 1;
}
.menu.active span i:nth-child(1) {
  transform: rotate(-45deg) translate(-50%, -50%);
  top: 50%;
}
.menu.active span i:nth-child(2) {
  transform: translateX(-100px);
  opacity: 0;
}
.menu.active span i:nth-child(3) {
  transform: rotate(45deg) translate(-50%, -50%);
  top: 50%;
}
.menu span {
  width: 70px;
  height: 70px;
  position: absolute;
  left: 10px;
  cursor: pointer;
  background: #f463c3;
  z-index: 1;
}
.menu span i {
  position: absolute;
  transform-origin: 50% 50%;
  width: 45%;
  height: 2px;
  left: 0;
  right: 0;
  margin: auto;
  background-color: #ccc;
  transition: transform 0.3s ease, opacity 0.1s ease 0.1s;
}
.menu span i:nth-child(1) {
  top: 40%;
}
.menu span i:nth-child(2) {
  top: 50%;
}
.menu span i:nth-child(3) {
  top: 60%;
}
.menu .menuContent {
  position: absolute;
  width: 100%;
  height: 100%;
  line-height: 40px;
  right: 0px;
  text-align: center;
}
.menu .menuContent * {
  opacity: 0;
}
.menu .menuContent ul li {
  display: inline-block;
  margin-left: 50px;
  margin-right: 50px;
  color: #2d3235;
  transition: opacity 0.3s ease 0.3s;
  cursor: pointer;
  position: relative;
}
.menu .menuContent ul li:hover:before {
  opacity: 0.8;
  top: 13px;
  left: 20px;
}
.menu .menuContent ul li:hover:after {
  opacity: 0.8;
  bottom: 13px;
  left: -20px;
}
        
    </style>
</head>
<body>
    <div class='menu'>
  <span class='toggle'>
    <i></i>
    <i></i>
    <i></i>
  </span>
  <div class='menuContent'>
    <ul>
      <li>Home</li>
      <li>About</li>
      <li>Contact</li>
      <li>Testimonial</li>
      <li>About us</li>
    </ul>
  </div>
</div>
 
<script
  src="https://code.jquery.com/jquery-3.3.1.js"
   ></script>
<script>
    $('.toggle').on('click', function() {
	$('.menu').toggleClass('active');
});
    
</script>
</body>
</html>