我的toggled类在分配后将元素顺时针旋转180度。 toggled类删除后,如何修改css将元素从顺时针旋转180度到0度?



var el = document.getElementById("wrapper");
el.addEventListener("click", function(){
  this.classList.toggle("toggled");
});

#wrapper {
  position: relative;
  top: 20px;
  left: 20px;
  padding: 10px;
  cursor: pointer;
  display: inline-block;
}

#wrapper.toggled {
  transform: rotateZ(180deg);
}

#wrapper > div {
  width: 35px;
  height: 3px;
  margin-bottom: 10px;
  background-color: #3f51b5;
}

#wrapper > div:last-child {
  margin-bottom: 0;
}

#wrapper, #wrapper > div {
  transition-property: transform;
  transition-duration: 0.5s;
  transition-timing-function: cubic-bezier(0.35, 0, 0.25, 1);
}

#wrapper.toggled >div:first-child {
  transform: rotateZ(45deg) rotateY(45deg) translateX(14px) translateY(-3px);
}

#wrapper.toggled >div:last-child {
  transform: rotateZ(-45deg) rotateY(45deg) translateY(3px) translateX(14px);
}

<div id="wrapper">
  <div></div>
  <div></div>
  <div></div>
</div>

最佳答案

我对您的脚本和样式进行了一些编辑,并且效果很好:https://jsfiddle.net/IA7medd/rkk0zc8k/

HTML:

<div id="wrapper">
  <div></div>
  <div></div>
  <div></div>
</div>


CSS:

#wrapper {
  position: relative;
  top: 20px;
  left: 20px;
  padding: 10px;
  cursor: pointer;
  display: inline-block;
  transform: rotateZ(0deg);
}

#wrapper > div {
  width: 35px;
  height: 3px;
  margin-bottom: 10px;
  background-color: #3f51b5;
}

#wrapper > div:last-child {
  margin-bottom: 0;
}

#wrapper, #wrapper > div {
  transition-property: transform;
  transition-duration: 0.5s;
  transition-timing-function: cubic-bezier(0.35, 0, 0.25, 1);
}

#wrapper.toggled >div:first-child {
  transform: rotateZ(45deg) rotateY(45deg) translateX(14px) translateY(-3px);
}

#wrapper.toggled >div:last-child {
  transform: rotateZ(-45deg) rotateY(45deg) translateY(3px) translateX(14px);
}

.animated {
    -webkit-animation-duration: 0.5s;
       -moz-animation-duration: 0.5s;
         -o-animation-duration: 0.5s;
            animation-duration: 0.5s;
    -webkit-animation-fill-mode: both;
       -moz-animation-fill-mode: both;
         -o-animation-fill-mode: both;
            animation-fill-mode: both;
}

.animated.notAnimated {
    -webkit-animation-duration: 0s;
       -moz-animation-duration: 0s;
         -o-animation-duration: 0s;
            animation-duration: 0s;
}

@-webkit-keyframes rotate180 {
    0%{transform: rotateZ(0deg);}
    100% {transform: rotateZ(180deg);}
}

@-moz-keyframes rotate180 {
    0%{transform: rotateZ(0deg);}
    100% {transform: rotateZ(180deg);}
}

@-o-keyframes rotate180 {
    0%{transform: rotateZ(0deg);}
    100% {transform: rotateZ(180deg);}
}

@keyframes rotate180 {
    0%{transform: rotateZ(0deg);}
    100% {transform: rotateZ(180deg);}
}

.animated.rotate180 {
    -webkit-animation-name: rotate180;
    -moz-animation-name: rotate180;
    -o-animation-name: rotate180;
    animation-name: rotate180;
}

@-webkit-keyframes rotate360 {
    0%{transform: rotateZ(180deg);}
    100% {transform: rotateZ(360deg);}
}

@-moz-keyframes rotate360 {
    0%{transform: rotateZ(180deg);}
    100% {transform: rotateZ(360deg);}
}

@-o-keyframes rotate360 {
    0%{transform: rotateZ(180deg);}
    100% {transform: rotateZ(360deg);}
}

@keyframes rotate360 {
    0%{transform: rotateZ(180deg);}
    100% {transform: rotateZ(360deg);}
}

.animated.rotate360 {
    -webkit-animation-name: rotate360;
    -moz-animation-name: rotate360;
    -o-animation-name: rotate360;
    animation-name: rotate360;
}

@-webkit-keyframes rotate0 {
    0%{transform: rotateZ(360deg);}
    100% {transform: rotateZ(0deg);}
}

@-moz-keyframes rotate0 {
    0%{transform: rotateZ(360deg);}
    100% {transform: rotateZ(0deg);}
}

@-o-keyframes rotate0 {
    0%{transform: rotateZ(360deg);}
    100% {transform: rotateZ(0deg);}
}

@keyframes rotate0 {
    0%{transform: rotateZ(360deg);}
    100% {transform: rotateZ(0deg);}
}

.animated.rotate0 {
    -webkit-animation-name: rotate0;
    -moz-animation-name: rotate0;
    -o-animation-name: rotate0;
    animation-name: rotate0;
}


剧本:

var el = document.getElementById("wrapper");

var counter = 1;
el.addEventListener("click", function(){

    this.classList.toggle("toggled");
  if(counter > 2){
    counter = 1;
    this.classList.toggle("rotate360");
  }
  if(counter == 1){
    this.classList.toggle("rotate180");
  }
  else if(counter == 2){
    this.classList.toggle("rotate180");
    this.classList.toggle("rotate360");
  }
  counter++;

});


另一个解决方案:

这是另一个选择,但使用jquery:https://jsfiddle.net/IA7medd/pr9akayk/

在选项中,您只需要像这样更改脚本即可:

var currentRotate = 0;
var $el = $("#wrapper");


$('#wrapper').click(function(){
    currentRotate+=180;
    $(this).toggleClass('toggled');
  $el.css({ WebkitTransform: 'rotate(' + currentRotate + 'deg)'});
  $el.css({ '-moz-transform': 'rotate(' + currentRotate + 'deg)'});
})

09-10 08:46