CSS动画不适用于移动设备

时间:2014-09-17 15:00:05

标签: css animation mobile browser sidebar

我目前正在为移动网站的侧边栏导航工作,我正在尝试为主菜单按钮添加以下动画。

这适用于不同的浏览器,但运行iOS的移动设备除外 - Safari和Android - Android浏览器。我错过了什么?感谢。

http://jsfiddle.net/wvck5xnL/

HTML

<div class="mainBtn">
  <input id="mainmenuBtn" name="exit" type="checkbox" />
  <label for="mainmenuBtn"><span class="burger"></span></label>
</div>

CSS

body {
    padding:0;
    margin:0;
    overflow: hidden;
   background:white;
}

/* burger icon animation  */
.mainBtn{
  display: block;
  width: 40px;
  padding-left: 10px;
  text-align: left;
}

.mainBtn input {
  display: none;
}

.mainBtn label {
  position: relative;
  width: 48px;
  height: 57px;
  display: block;
  cursor: pointer;
  background: transparent;
}

/* Exit Icon */

.mainBtn label:before,
.mainBtn input:checked + label:before {
  content: '';
  position: absolute;
  top: 50%;
  margin-top: -2px;
  width: 30px;
  height: 4px;
  border-radius: 2px;
  background: black;
}

.mainBtn label:before {
  -webkit-animation: animationOneReverse 1s ease forwards;
  -moz-animation: animationOneReverse 1s ease forwards;
  -o-animation: animationOneReverse 1s ease forwards;
  -ms-animation: animationOneReverse 1s ease forwards;
  animation: animationOneReverse 1s ease forwards;
}

@-webkit-keyframes animationOneReverse {
  0% { -webkit-transform: rotate(315deg); }
  50%, 100% { -webkit-transform: rotate(0deg); }
}
@-moz-keyframes animationOneReverse {
  0% { -moz-transform: rotate(315deg); }
  50%, 100% { -moz-transform: rotate(0deg); }
}
@-o-keyframes animationOneReverse {
  0% { -o-transform: rotate(315deg); }
  50%, 100% { -o-transform: rotate(0deg); }
}
@-ms-keyframes animationOneReverse {
  0% { -ms-transform: rotate(315deg); }
  50%, 100% { -ms-transform: rotate(0deg); }
}
@keyframes animationOneReverse {
  0% { transform: rotate(315deg); }
  50%, 100% { transform: rotate(0deg); }
}

.mainBtn input:checked + label:before {
  -webkit-animation: animationOne 1s ease forwards;
  -moz-animation: animationOne 1s ease forwards;
  -o-animation: animationOne 1s ease forwards;
  -ms-animation: animationOne 1s ease forwards;
  animation: animationOne 1s ease forwards;
}

@-webkit-keyframes animationOne {
  0%, 50% { -webkit-transform: rotate(0deg); }
  100% { -webkit-transform: rotate(315deg); }
}
@-moz-keyframes animationOne {
  0% { -moz-transform: rotate(0deg); }
  50%, 100% { -moz-transform: rotate(315deg); }
}
@-o-keyframes animationOne {
  0% { -o-transform: rotate(0deg); }
  50%, 100% { -o-transform: rotate(315deg); }
}
@-ms-keyframes animationOne {
  0% { -ms-transform: rotate(0deg); }
  50%, 100% { -ms-transform: rotate(315deg); }
}
@keyframes animationOne {
  0%, 50% { transform: rotate(0deg); }
  100% { transform: rotate(315deg); }
}

.mainBtn label:after,
.mainBtn input:checked + label:after {
  content: '';
  position: absolute;
  top: 50%;
  margin-top: -2px;
  width: 30px;
  height: 4px;
  border-radius: 2px;
  background: black;
}

.mainBtn label:after {
  -webkit-animation: animationTwoReverse 1s ease forwards;
  -moz-animation: animationTwoReverse 1s ease forwards;
  -o-animation: animationTwoReverse 1s ease forwards;
  -ms-animation: animationTwoReverse 1s ease forwards;
  animation: animationTwoReverse 1s ease forwards;
}

@-webkit-keyframes animationTwoReverse {
  0% { -webkit-transform: rotate(405deg); }
  50%, 100% { -webkit-transform: rotate(0deg); }
}
@-moz-keyframes animationTwoReverse {
  0% { -moz-transform: rotate(405deg); }
  50%, 100% { -moz-transform: rotate(0deg); }
}
@-o-keyframes animationTwoReverse {
  0% { -o-transform: rotate(405deg); }
  50%, 100% { -o-transform: rotate(0deg); }
}
@-ms-keyframes animationTwoReverse {
  0% { -ms-transform: rotate(405deg); }
  50%, 100% { -ms-transform: rotate(0deg); }
}
@keyframes animationTwoReverse {
  0% { transform: rotate(405deg); }
  50%, 100% { transform: rotate(0deg); }
}

.mainBtn input:checked + label:after {
  -webkit-animation: animationTwo 1s ease forwards;
  -moz-animation: animationTwo 1s ease forwards;
  -o-animation: animationTwo 1s ease forwards;
  -ms-animation: animationTwo 1s ease forwards;
  animation: animationTwo 1s ease forwards;
}

@-webkit-keyframes animationTwo {
  0%, 50% { -webkit-transform: rotate(0deg); }
  100% { -webkit-transform: rotate(405deg); }
}
@-moz-keyframes animationTwo {
  0% { -moz-transform: rotate(0deg); }
  50%, 100% { -moz-transform: rotate(405deg); }
}
@-o-keyframes animationTwo {
  0% { -o-transform: rotate(0deg); }
  50%, 100% { -o-transform: rotate(405deg); }
}
@-ms-keyframes animationTwo {
  0% { -ms-transform: rotate(0deg); }
  50%, 100% { -ms-transform: rotate(405deg); }
}
@keyframes animationTwo {
  0%, 50% { transform: rotate(0deg); }
  100% { transform: rotate(405deg); }
}

/* Burger Icon */

.mainBtn label .burger:before {
  content: '';
  position: absolute;
  top: 17px;
  width: 30px;
  height: 4px;
  border-radius: 2px;
  background: black;
  -webkit-animation: animationBurgerTopReverse 1s ease forwards;
  -moz-animation: animationBurgerTopReverse 1s ease forwards;
  -o-animation: animationBurgerTopReverse 1s ease forwards;
  -ms-animation: animationBurgerTopReverse 1s ease forwards;
  animation: animationBurgerTopReverse 1s ease forwards;
}

@-webkit-keyframes animationBurgerTopReverse {
  0%, 50% { -webkit-transform: translateY(12px); opacity: 0; }
  51% { -webkit-transform: translateY(12px); opacity: 1; }
  100% { -webkit-transform: translateY(0px); opacity: 1; }
}
@-moz-keyframes animationBurgerTopReverse {
  0%, 50% { -moz-transform: translateY(12px); opacity: 0; }
  51% { -moz-transform: translateY(12px); opacity: 1; }
  100% { -moz-transform: translateY(0px); opacity: 1; }
}
@-o-keyframes animationBurgerTopReverse {
  0%, 50% { -o-transform: translateY(12px); opacity: 0; }
  51% { -o-transform: translateY(12px); opacity: 1; }
  100% { -o-transform: translateY(0px); opacity: 1; }
}
@-ms-keyframes animationBurgerTopReverse {
  0%, 50% { -ms-transform: translateY(12px); opacity: 0; }
  51% { -ms-transform: translateY(12px); opacity: 1; }
  100% { -ms-transform: translateY(0px); opacity: 1; }
}
@keyframes animationBurgerTopReverse {
  0%, 50% { transform: translateY(12px); opacity: 0; }
  51% { transform: translateY(12px); opacity: 1; }
  100% { transform: translateY(0px); opacity: 1; }
}

.mainBtn input:checked + label .burger:before {
  -webkit-animation: animationBurgerTop 1s ease forwards;
  -moz-animation: animationBurgerTop 1s ease forwards;
  -o-animation: animationBurgerTop 1s ease forwards;
  -ms-animation: animationBurgerTop 1s ease forwards;
  animation: animationBurgerTop 1s ease forwards;
}

@-webkit-keyframes animationBurgerTop {
  0% { -webkit-transform: translateY(0px); opacity: 1; }
  50% { -webkit-transform: translateY(12px); opacity: 1; }
  51%, 100% { -webkit-transform: translateY(12px); opacity: 0; }
}
@-moz-keyframes animationBurgerTop {
  0% { -moz-transform: translateY(0px); opacity: 1; }
  50% { -moz-transform: translateY(12px); opacity: 1; }
  51%, 100% { -moz-transform: translateY(12px); opacity: 0; }
}
@-o-keyframes animationBurgerTop {
  0% { -o-transform: translateY(0px); opacity: 1; }
  50% { -o-transform: translateY(12px); opacity: 1; }
  51%, 100% { -o-transform: translateY(12px); opacity: 0; }
}
@-ms-keyframes animationBurgerTop {
  0% { -ms-transform: translateY(0px); opacity: 1; }
  50% { -ms-transform: translateY(12px); opacity: 1; }
  51%, 100% { -ms-transform: translateY(12px); opacity: 0; }
}
@keyframes animationBurgerTop {
  0% { transform: translateY(0px); opacity: 1; }
  50% { transform: translateY(12px); opacity: 1; }
  51%, 100% { transform: translateY(12px); opacity: 0; }
}

.mainBtn label .burger:after {
  content: '';
  position: absolute;
  bottom: 16px;
  width: 30px;
  height: 4px;
  border-radius: 2px;
  background: black;
  -webkit-animation: animationBurgerBottomReverse 1s ease forwards;
  -moz-animation: animationBurgerBottomReverse 1s ease forwards;
  -o-animation: animationBurgerBottomReverse 1s ease forwards;
  -ms-animation: animationBurgerBottomReverse 1s ease forwards;
  animation: animationBurgerBottomReverse 1s ease forwards;
}

@-webkit-keyframes animationBurgerBottomReverse {
  0%, 50% { -webkit-transform: translateY(-12px); opacity: 0; }
  51% { -webkit-transform: translateY(-12px); opacity: 1; }
  100% { -webkit-transform: translateY(0px); opacity: 1; }
}
@-moz-keyframes animationBurgerBottomReverse {
  0%, 50% { -moz-transform: translateY(-12px); opacity: 0; }
  51% { -moz-transform: translateY(-12px); opacity: 1; }
  100% { -moz-transform: translateY(0px); opacity: 1; }
}
@-o-keyframes animationBurgerBottomReverse {
  0%, 50% { -o-transform: translateY(-12px); opacity: 0; }
  51% { -o-transform: translateY(-12px); opacity: 1; }
  100% { -o-transform: translateY(0px); opacity: 1; }
}
@-ms-keyframes animationBurgerBottomReverse {
  0%, 50% { -ms-transform: translateY(-12px); opacity: 0; }
  51% { -ms-transform: translateY(-12px); opacity: 1; }
  100% { -ms-transform: translateY(0px); opacity: 1; }
}
@keyframes animationBurgerBottomReverse {
  0%, 50% { transform: translateY(-12px); opacity: 0; }
  51% { transform: translateY(-12px); opacity: 1; }
  100% { transform: translateY(0px); opacity: 1; }
}

.mainBtn input:checked + label .burger:after {
  -webkit-animation: animationBurgerBottom 1s ease forwards;
  -moz-animation: animationBurgerBottom 1s ease forwards;
  -o-animation: animationBurgerBottom 1s ease forwards;
  -ms-animation: animationBurgerBottom 1s ease forwards;
  animation: animationBurgerBottom 1s ease forwards;
}

@-webkit-keyframes animationBurgerBottom {
  0% { -webkit-transform: translateY(0px); opacity: 1; }
  50% { -webkit-transform: translateY(-12px); opacity: 1; }
  51%, 100% { -webkit-transform: translateY(-12px); opacity: 0; }
}
@-moz-keyframes animationBurgerBottom {
  0% { -moz-transform: translateY(0px); opacity: 1; }
  50% { -moz-transform: translateY(-12px); opacity: 1; }
  51%, 100% { -moz-transform: translateY(-12px); opacity: 0; }
}
@-o-keyframes animationBurgerBottom {
  0% { -o-transform: translateY(0px); opacity: 1; }
  50% { -o-transform: translateY(-12px); opacity: 1; }
  51%, 100% { -o-transform: translateY(-12px); opacity: 0; }
}
@-ms-keyframes animationBurgerBottom {
  0% { -ms-transform: translateY(0px); opacity: 1; }
  50% { -ms-transform: translateY(-12px); opacity: 1; }
  51%, 100% { -ms-transform: translateY(-12px); opacity: 0; }
}
@keyframes animationBurgerBottom {
  0% { transform: translateY(0px); opacity: 1; }
  50% { transform: translateY(-12px); opacity: 1; }
  51%, 100% { transform: translateY(-12px); opacity: 0; }
}

1 个答案:

答案 0 :(得分:2)

嗨,我试过你的小提琴,它适用于我的手机,即iphone5s。 但无论如何,据我所知,当你想使用关键帧 - 翻译元素时。元素必须是显示块,或者必须用div包装,默认也显示为块。

&#13;
&#13;
a { 
  background:orange;
}
div {
  -webkit-animation: move 5s infinite;
}

@-webkit-keyframes move {
  0% { -webkit-transform: translate(20px); }
  100% { -webkit-transform: translate(500px); }
}
&#13;
<div class="wrapper"><a href="#">Test</a></div>
&#13;
&#13;
&#13;

我想也许翻译并不完全支持某些元素。例如,当您要翻译标记元素时,您必须设置元素 display:block

但是,如果要在div元素上应用translate,如果在div元素 display:inline-block 上设置css,则translate仍然可以在该div上运行。

所以也许尝试设置你想要移动的元素 display:block