动画转换从三行菜单到交叉

时间:2015-11-10 20:32:14

标签: jquery css css3 css-transitions css-transforms

我有一个三行动画菜单,当你点击它时会切换到一个十字架。首先,您看到三条线转到一条,然后切换到十字形。但我想跳过三行到一步的步骤。

我该怎么做?

这里是小提琴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>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:3)

您可以使用此更新的代码。我在找到它们的地方删除了transition-delay,将范围设置为visibility: hidden;并在切换类中将伪元素设置为visibility: visible;

但是,由于a bug in Internet Explorer,此方法无法在该浏览器中运行。展开下面的第二个片段,看看在IE中有效的方法。

&#13;
&#13;
$(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');
    });
});
&#13;
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;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button class="lines-button lines"><span></span></button>
&#13;
&#13;
&#13;

IE兼容版本:

此版本仅使用CSS;复选框方法。它也适用于所有现代浏览器和IE9 +。

&#13;
&#13;
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;
}
&#13;
<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>
&#13;
&#13;
&#13;