纯CSS动画使用单选按钮激活移动菜单

时间:2017-08-09 17:45:18

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

我有一个Mobile菜单,点击时应该形成X,但它不会响应并且卡在它的形状上。我不知道为什么会这样做,但据我所知,这段代码应该在菜单动画处理时点击形成X.我附上代码供参考。

这些是CSS和HTML



/* Checkbox Hack */
input[type=checkbox] {
  display: block;
  width: 40px;
  height: 32px;
  position: absolute;
  top: -7px;
  left: -5px;
  cursor: pointer;
  opacity: 0;
  z-index: 2;
  -webkit-touch-callout: none;
}

/* Default State */
.divko {
  background: green;
  width: 400px;
  height: 100px;
  line-height: 100px;
  color: white;
  text-align: center;
}

/* Toggled State */
.myspan {
  display: block;
  width: 33px;
  height: 4px;
  margin-bottom: 5px;
  position: relative;
  background: #cdcdcd;
  border-radius: 3px;
  z-index: 1;
  transform-origin: 4px 0px;
  transition: transform 0.5s cubic-bezier(0.77, 0.2, 0.05, 1.0), background 0.5s cubic-bezier(0.77, 0.2, 0.05, 1.0), opacity 0.55s ease;
}

.myspan:first-child {
  transform-origin: 0% 0%;
}

.myspan:nth-last-child(2) {
  transform-origin: 0% 100%;
}

input[type=checkbox]:checked ~ .myspan {
  opacity: 1;
  transform: rotate(45deg) translate(-2px, -1px);
  background: #232323;
}

input[type=checkbox]:checked ~ .divko {
  background: red;
}

<div for="toggle-1"> <span class="myspan"></span>
  <span class="myspan"></span>
  <span class="myspan"></span></div>
<input type="checkbox" id="toggle-1">
<div class="divko">I'm controlled by toggle. No JavaScript!</div>
&#13;
&#13;
&#13;

这是dabblet http://dabblet.com/gist/5b4667ecc7255c228cd488c080140d95

1 个答案:

答案 0 :(得分:3)

将复选框放在div之前我已将.menu课程提供给input[type=checkbox]:checked ~ .menu > .myspan。并在/* Checkbox Hack */ input[type=checkbox] { display: block; width: 40px; height: 32px; position: absolute; top: -7px; left: -5px; cursor: pointer; opacity: 0; z-index: 2; -webkit-touch-callout: none; } /* Default State */ .divko { background: green; width: 400px; height: 100px; line-height: 100px; color: white; text-align: center; } /* Toggled State */ .myspan { display: block; width: 33px; height: 4px; margin-bottom: 5px; position: relative; background: #cdcdcd; border-radius: 3px; z-index: 1; transform-origin: 4px 2px; transition: transform 0.5s cubic-bezier(0.77, 0.2, 0.05, 1.0), background 0.5s cubic-bezier(0.77, 0.2, 0.05, 1.0), opacity 0.55s ease; } input[type=checkbox]:checked ~ .menu > .myspan { opacity: 1; background: #232323; } input[type=checkbox]:checked ~ .menu > .myspan:first-child { transform: rotate(45deg); } input[type=checkbox]:checked ~ .menu > .myspan:nth-child(2) { transform: translate(-100px); } input[type=checkbox]:checked ~ .menu > .myspan:last-child { transform: rotate(-45deg); } input[type=checkbox]:checked ~ .divko { background: red; }上应用规则。

&#13;
&#13;
<input type="checkbox" id="toggle-1">

<div for="toggle-1" class="menu">
  <span class="myspan"></span>
  <span class="myspan"></span>
  <span class="myspan"></span>
</div>

<div class="divko">I'm controlled by toggle. No JavaScript!</div>
&#13;
throw 'allowIllegalResourceCall is false.';
{
  "id" : "52104", 
}
&#13;
&#13;
&#13;