如何制作垂直菜单图标线

时间:2018-10-30 11:46:22

标签: javascript html css

我需要帮助来制作菜单图标,该图标代替三条水平线,而具有三条垂直线,单击时会变成X ...就像在此网站example ...

到目前为止,我最大的努力使我走了这么远... JSFiddle

我需要一些帮助使动画工作并使线垂直...

我的HTML:

<div class="hamburger" id="menu-button">
    <span class="line"></span>
    <span class="line"></span>
    <span class="line"></span>
    <span class="menu-button_lable">Menu</span>
</div>

我的SCSS:

#menu-button {
  position: fixed;
  top: 50%;
  left: 0;
  width              : 60px;
  height             : 45px;
  -webkit-transform  : rotate(0deg);
  -moz-transform     : rotate(0deg);
  -o-transform       : rotate(0deg);
  transform          : rotate(0deg);
  -webkit-transition : .5s ease-in-out;
  -moz-transition    : .5s ease-in-out;
  -o-transition      : .5s ease-in-out;
  transition         : .5s ease-in-out;
  cursor             : pointer;
  .line {
    display: block;
    position: absolute;
    height: 2px;
    width: 25px;
    background: #d3531a;
    opacity: 1;
    left: 0;
    -webkit-transform: rotate(0deg);
    -moz-transform: rotate(0deg);
    -o-transform: rotate(0deg);
    transform: rotate(0deg);
    -webkit-transition: .25s ease-in-out;
    -moz-transition: .25s ease-in-out;
    -o-transition: .25s ease-in-out;
    transition: .25s ease-in-out;
    &:nth-child(1) {
      top                      : 12px;
      -webkit-transform-origin : left center;
      -moz-transform-origin    : left center;
      -o-transform-origin      : left center;
      transform-origin         : left center;
    }
    &:nth-child(2) {
      top                      : 18px;
      -webkit-transform-origin : left center;
      -moz-transform-origin    : left center;
      -o-transform-origin      : left center;
      transform-origin         : left center;
    }
    &:nth-child(3) {
      top                      : 24px;
      -webkit-transform-origin : left center;
      -moz-transform-origin    : left center;
      -o-transform-origin      : left center;
      transform-origin         : left center;
    }
  }
  .menu-button_lable {
    font-size: .7857142857rem;
    letter-spacing: .06em;
    line-height: 1.3;
    display: inline-block;
    vertical-align: middle;
    margin-left: 2.2rem;
    margin-top: 0.75rem;
    font-weight: 500;
    position: relative;
  }
  &.open span {
    &:nth-child(1) {
      -webkit-transform : rotate(45deg);
      -moz-transform    : rotate(45deg);
      -o-transform      : rotate(45deg);
      transform         : rotate(45deg);
      top               : 6px;
      left              : 8px;
    }
    &:nth-child(2) {
      width   : 0;
      opacity : 0;
    }
    &:nth-child(3) {
      -webkit-transform : rotate(-45deg);
      -moz-transform    : rotate(-45deg);
      -o-transform      : rotate(-45deg);
      transform         : rotate(-45deg);
      top               : 24px;
      left              : 8px;
    }
  }
}

和我的JS:

$(document).ready(function(){
    $('#nmenu-button').click(function(){
        $(this).toggleClass('is-active');
    });
});

4 个答案:

答案 0 :(得分:1)

请参阅下文。 为每行添加了额外的CSS,以使其在活动时正确定位。

$(document).on("click", "#menu-button", function() {
  $(this).toggleClass('is-active');
});
#menu-button {
  cursor: pointer;
  position: relative;
  margin-top: 1rem;
}

.line {
  display: block;
  position: absolute;
  height: 2px;
  width: 25px;
  background: #d3531a;
  opacity: 1;
  transform: rotate(90deg);
  transition: .25s ease-in-out;
}

.hamburger .line:nth-child(1) {
  top: 0;
  left: 0;
}
/* Added */
.hamburger.is-active .line:nth-child(1) {
  transform: rotate(45deg);
  transform-origin: left top;
  top: -9px;
  left: 9px;
}

.hamburger .line:nth-child(2) {
  top: 0;
  left: 10px;
}
/* Added */
.hamburger.is-active .line:nth-child(2) {
  opacity: 0;
}

.hamburger .line:nth-child(3) {
  top: 0;
  left: 20px;
}
/* Added */
.hamburger.is-active .line:nth-child(3) {
  transform: rotate(-45deg);
  transform-origin: right top;
  top: -9px;
  left: 1px;
}

.menu-button_lable {
  font-size: .7857142857rem;
  letter-spacing: .06em;
  display: flex;
  align-items: center;
  font-weight: 500;
  position: absolute;
  left: 45px;
  top: 50%;
  transform: translate(0, -50%);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="hamburger" id="menu-button">
  <span class="line"></span>
  <span class="line"></span>
  <span class="line"></span>
  <span class="menu-button_lable">Menu</span>
</div>

答案 1 :(得分:0)

这只是一个纯CSS汉堡菜单图标,不需要点击事件触发,简单的悬停就可以了。为了形成“ X”,我降低了 .strip.two 的不透明度。旋转并翻译 .strip.one .strip.three 以获得“ X”。如果您愿意,可以添加click事件,您只需根据需要添加和删除类即可。

    .menuIcon{
      display:inline-block;
      width:2em;
      height:2.2em;
      
    }
    
    .strip{
      width:3px;
      height:2em;
      display:inline-block;
      background:black;
    }
    .menuIcon:hover{
      cursor:pointer;
    }
    
    .menuIcon:hover .strip.two{
      opacity:0;
    }
    .strip.one,.strip.two,.strip.three{transform-origin:50% 50%;transition:all 100ms ease-in-out;}
    .menuIcon:hover .strip.one{
      transform:rotate(45deg) translateY(-0.5em);
    }
    .menuIcon:hover .strip.three{
      transform:rotate(45deg) translateY(-0.5em);
    }
<div class="menuIcon">
  <span class="strip one"></span>
  <span class="strip two"></span>
  <span class="strip three"></span>
</div>

答案 2 :(得分:0)

尝试使用此代码

$(document).ready(function(){
  $('.hamburger').click(function(e){
   	$(".hamburger").toggleClass("is-active");
  });
});
	 #menu-button {
  position: fixed;
  top: 50%;
  left: 0;
  width              : 60px;
  height             : 45px;
  -webkit-transform  : rotate(0deg);
  -moz-transform     : rotate(0deg);
  -o-transform       : rotate(0deg);
  transform          : rotate(0deg);
  -webkit-transition : .5s ease-in-out;
  -moz-transition    : .5s ease-in-out;
  -o-transition      : .5s ease-in-out;
  transition         : .5s ease-in-out;
  cursor             : pointer;
	 }
#menu-button .line {
    display: block;
    position: absolute;
    height: 2px;
    width: 25px;
    background: #d3531a;
    opacity: 1;
    left: 0;
    -webkit-transform: rotate(0deg);
    -moz-transform: rotate(0deg);
    -o-transform: rotate(0deg);
    transform: rotate(0deg);
    -webkit-transition: .25s ease-in-out;
    -moz-transition: .25s ease-in-out;
    -o-transition: .25s ease-in-out;
    transition: .25s ease-in-out;
}
#menu-button .line:nth-child(1) {
      top                      : 12px;
      -webkit-transform-origin : left center;
      -moz-transform-origin    : left center;
      -o-transform-origin      : left center;
      transform-origin         : left center;
    }
#menu-button .line:nth-child(2) {
      top                      : 18px;
      -webkit-transform-origin : left center;
      -moz-transform-origin    : left center;
      -o-transform-origin      : left center;
      transform-origin         : left center;
    }
#menu-button .line:nth-child(3) {
      top                      : 24px;
      -webkit-transform-origin : left center;
      -moz-transform-origin    : left center;
      -o-transform-origin      : left center;
      transform-origin         : left center;
    }

.menu-button_lable {
    font-size: .7857142857rem;
    letter-spacing: .06em;
    line-height: 1.3;
    display: inline-block;
    vertical-align: middle;
    margin-left: 2.2rem;
    margin-top: 0.75rem;
    font-weight: 500;
    position: relative;
  }
#menu-button.open span { }
#menu-button.is-active span:nth-child(1) {
      -webkit-transform : rotate(45deg);
      -moz-transform    : rotate(45deg);
      -o-transform      : rotate(45deg);
      transform         : rotate(45deg);
      top               : 6px;
      left              : 8px;
    }
#menu-button.is-active span:nth-child(2) {
      width   : 0;
      opacity : 0;
    }
#menu-button.is-active span:nth-child(3) {
      -webkit-transform : rotate(-45deg);
      -moz-transform    : rotate(-45deg);
      -o-transform      : rotate(-45deg);
      transform         : rotate(-45deg);
      top               : 24px;
      left              : 8px;
    }
 

.line {
  position: relative;
  display: inline-block;
  vertical-align: middle;
  width: 1px;
  height: 20px;
  -webkit-clip-path: polygon(0 0, 100% 0, 100% 100%, 0 100%);
  clip-path: polygon(0 0, 100% 0, 100% 100%, 0 100%);
  background: red;
}
.hamburger{
  cursor:pointer;
}
.menu-button_lable{
 color:red;
 padding-left:10px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="hamburger" id="menu-button">
    <span class="line"></span>
    <span class="line"></span>
    <span class="line"></span>
    <span class="menu-button_lable">Menu</span>
</div>

答案 3 :(得分:0)

这是立式汉堡的样式。

.hamburger {
  width: 14px;
  height: 18px;
  display: inline-block;
  vertical-align: middle;
  text-align: center;
  position: relative;
}
.line {
  display: inline-block;
  width: 2px;
  height: 100%;
  background-color: #000;
  vertical-align: middle;
  text-align: center;
}

.line::before,
.line::after {
  content: "";
  position: absolute;
  top: 0;
  bottom: 0;
  background-color: #000;
  width: 2px;
}

.line::after {
  right: 0;
}

.line::before {
  left: 0;
}
	<div class="hamburger" id="menu-button">
		<span class="line"></span>
	</div>