2020年3月16日 星期一

Rotation animation (div)

Ref: https://www.w3schools.com/js/tryit.asp?filename=tryjs_dom_animate_3
<!DOCTYPE html>
<html>
<style>
#myContainer {
  width: 400px;
  height: 400px;
  position: relative;
  background: yellow;
}
/*
center @ 25, 100
*/
#div1 {
  width: 50px;
  height: 200px;
  position: absolute;
  background-color: transpency;

}
/*  25 - w/2 = 50, 100  */
#div2 {
  left: 0px;
  top:100px;
  width: 50px;
  height: 100px;
  position: absolute;
  background-color: blue;
  border-style: solid;
  border-color: green;
}
/*  25 - w/2 = 50, 100  */
#diva {
  width: 50px;
  height: 200px;
  position: absolute;
  background-color: transpency;

}
/*  25 - w/2 = 50, 100  */
#divb {
  left: 0px;
  top:100px;
  width: 50px;
  height: 100px;
  position: absolute;
  background-color: blue;
  border-style: solid;
  border-color: green;
}
</style>
<body>

<p>
<button onclick="myMove()">Click Me</button>
</p>

<div id ="myContainer">

<div id="div1" style="top:0px; left:100px; transform: rotate(-30deg)">
  <div id="div2">
    <div id="diva" style="top:0px; left:0px; transform: rotate(10deg)">
      <div id="divb">

      </div>
    </div>
  </div>
</div>

<script>
function myMove() {

  var pos = -30;
  var dir = 1;
  var c1 = document.getElementById("div1");
  var ca = document.getElementById("diva");
  var id = setInterval(frame, 10);
  function frame() {
    if (pos == 30) {
      clearInterval(id);
    } else {
      redraw();
    }
  }
  function redraw()
  {
    pos++;
    //elem.style.top = pos + 'px';
    //elem.style.left = pos + 'px';
    c1.style.transform = "rotate(" + (pos) + "deg)";
    ca.style.transform = "rotate(" + (pos) + "deg)";
  }
}
</script>

</body>
</html>