Animated transform from three lines menu to cross

感情迁移 提交于 2020-06-27 12:19:26

问题


I have a three-line animated menu that switches to a cross when you clicked on it. First you see the three lines go to one, and then switch to a cross. But I want skip the step from three lines to one.

How can I do that?

Here's the fiddle http://jsfiddle.net/adyocsm9/

$(document).ready(function() {
  $(document).on('click', ".lines-button", function() {
    $('.lines-button').addClass('close');
  });
  $(document).on('click', ".lines-button.close", function() {
    $('.lines-button').removeClass('close');
  });
});
body {
  background: #000;
  padding-right: 100px; /* inserted padding so stackoverflows fullscreen button does not overlay */
}
.lines-button {
  position: relative;
  float: right;
  overflow: hidden;
  margin: 0;
  padding: 0;
  width: 96px;
  height: 56px;
  font-size: 0;
  text-indent: -9999px;
  -webkit-appearance: none;
  -moz-appearance: none;
  appearance: none;
  box-shadow: none;
  border-radius: none;
  border: none;
  cursor: pointer;
  -webkit-transition: background 0.3s;
  transition: background 0.3s;
}
.lines-button:focus {
  outline: none;
}
.lines-button span {
  display: block;
  position: absolute;
  left: 18px;
  right: 18px;
  height: 8px;
  background: white;
  border-radius: 0.57143rem;
}
.lines-button span::before,
.lines-button span::after {
  position: absolute;
  display: block;
  left: 0;
  width: 100%;
  height: 8px;
  background-color: #fff;
  border-radius: 0.57143rem;
  content: "";
}
.lines-button span::before {
  top: -15px;
}
.lines-button span::after {
  bottom: -15px;
}
.lines {
  background: none;
}
.lines span {
  -webkit-transition: background 0s 0.3s;
  transition: background 0s 0.3s;
}
.lines span::before,
.lines span::after {
  -webkit-transition-duration: 0.3s, 0.3s;
  transition-duration: 0.3s, 0.3s;
  -webkit-transition-delay: 0.3s, 0s;
  transition-delay: 0.3s, 0s;
}
.lines span::before {
  -webkit-transition-property: top, -webkit-transform;
  transition-property: top, transform;
}
.lines span::after {
  -webkit-transition-property: bottom, -webkit-transform;
  transition-property: bottom, transform;
}
.lines.close {
  background: none;
}
.lines.close span {
  background: none;
}
.lines.close span::before {
  top: 0;
  -webkit-transform: rotate(45deg);
  -ms-transform: rotate(45deg);
  transform: rotate(45deg);
}
.lines.close span::after {
  bottom: 0;
  -webkit-transform: rotate(-45deg);
  -ms-transform: rotate(-45deg);
  transform: rotate(-45deg);
}
.lines.close span::before,
.lines.close span::after {
  -webkit-transition-delay: 0s, 0.3s;
  transition-delay: 0s, 0.3s;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button class="lines-button lines">
  <span></span>
</button>

回答1:


You can use this updated code. I removed the transition-delays wherever I found them, set the span to visibility: hidden; and set the pseudo-elements to visibility: visible; in the toggled class.

However, due to a bug in Internet Explorer, this method won't work in that browser. Expand my second snippet below to see a method that works in IE.

$(document).ready(function () {
    $(document).on('click', ".lines-button", function () {
        $('#overlay').show();
        $('.lines-button').addClass('close');
    });
    $(document).on('click', ".lines-button.close", function () {
        $('#overlay').hide();
        $('.lines-button').removeClass('close');
    });
});
body {
    background: #000;
}
.lines-button {
    position: relative;
    float:right;
    overflow: hidden;
    margin: 0;
    padding: 0;
    width: 96px;
    height: 56px;
    font-size: 0;
    text-indent: -9999px;
    -webkit-appearance: none;
    -moz-appearance: none;
    appearance: none;
    box-shadow: none;
    border-radius: none;
    border: none;
    cursor: pointer;
    -webkit-transition: background 0.3s;
    transition: background 0.3s;
}
.lines-button:focus {
    outline: none;
}
.lines-button span {
    display: block;
    position: absolute;
    left: 18px;
    right: 18px;
    height: 8px;
    background: white;
    border-radius: 0.57143rem;
}
.lines-button span::before, .lines-button span::after {
    position: absolute;
    display: block;
    left: 0;
    width: 100%;
    height: 8px;
    background-color: #fff;
    border-radius: 0.57143rem;
    content:"";
}
.lines-button span::before {
    top: -15px;
}
.lines-button span::after {
    bottom: -15px;
}
.lines {
    background: none;
}
.lines span {
    -webkit-transition: background 0s 0.3s;
    transition: background 0s 0.3s;
}
.lines span::before, .lines span::after {
    -webkit-transition-duration: 0.3s, 0.3s;
    transition-duration: 0.3s, 0.3s;
}
.lines span::before {
    -webkit-transition-property: top, -webkit-transform;
    transition-property: top, transform;
}
.lines span::after {
    -webkit-transition-property: bottom, -webkit-transform;
    transition-property: bottom, transform;
}
.lines.close {
    background: none;
}
.lines.close span {
    visibility: hidden;
}
.lines.close span::before {
    top: 0;
    -webkit-transform: rotate(45deg);
    -ms-transform: rotate(45deg);
    transform: rotate(45deg);
    visibility: visible;
}
.lines.close span::after {
    bottom: 0;
    -webkit-transform: rotate(-45deg);
    -ms-transform: rotate(-45deg);
    transform: rotate(-45deg);
    visibility: visible;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button class="lines-button lines"><span></span></button>

IE-Compatible version:

This version uses CSS only; the checkbox method. It also works in all modern browsers and IE9+.

body {
    background: #000;
    margin: 0;
}
#hamburger {
    position: fixed;
    left: -9999px;
}
.line {
    display: block;
    height: 8px;
    width: 60px;
    background: white;
    border-radius: 0.57143rem;
    cursor: pointer;
    position: absolute;
    top: 35px;
    right: 24px;
}
.top, .bottom {
    display: block;
    position: absolute;
    height: 8px;
    background: white;
    border-radius: 0.57143rem;
    top: 20px;
}
.bottom {
    top: 50px;
}
.line {
    -webkit-transition: background 0s 0s;
    transition: background 0s 0s;
}
.top, .bottom {
    -webkit-transition-duration: 0.3s, 0.3s;
    transition-duration: 0.3s, 0.3s;
    -webkit-transition-property: top, -webkit-transform;
    transition-property: top, transform;
}
.middle {
    -webkit-transition-property: opacity, -webkit-transform;
    transition-property: opacity, transform;
}
#hamburger:checked ~ .middle {
    opacity: 0;
}
#hamburger:checked + .top {
    -webkit-transform: rotate(45deg);
    -ms-transform: rotate(45deg);
    transform: rotate(45deg);
    top: 35px;
}
#hamburger:checked ~ .bottom {
    -webkit-transform: rotate(-45deg);
    -ms-transform: rotate(-45deg);
    transform: rotate(-45deg);
    top: 35px;
}
<input type="checkbox" id="hamburger" />
<label for="hamburger" class="top line"></label>
<label for="hamburger" class="middle line"></label>
<label for="hamburger" class="bottom line"></label>


来源:https://stackoverflow.com/questions/33639040/animated-transform-from-three-lines-menu-to-cross

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!