2

My toggled class rotates an element clockwise on 180 degrees when assigned. How can I modify my css to rotate an element from 180 to 0 degrees clockwise when toggled class removes?

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>
Anton
  • 2,217
  • 3
  • 21
  • 34

1 Answers1

1

I did some edits on your script and style and it work fine : 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;
}

the script:

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++;

});

Another Solution :

And this is another option but using jquery : https://jsfiddle.net/IA7medd/pr9akayk/

in the option you will need only to change your script like this :

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)'});
})
Ahmed Salama
  • 2,795
  • 1
  • 11
  • 15