Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>To rotate a DIV we can add some CSS that, well, rotates the DIV using CSS transform rotate.</p> <p>To toggle the rotation we can keep a flag, a simple variable with a boolean value that tells us what way to rotate.</p> <pre><code>var rotated = false; document.getElementById('button').onclick = function() { var div = document.getElementById('div'), deg = rotated ? 0 : 66; div.style.webkitTransform = 'rotate('+deg+'deg)'; div.style.mozTransform = 'rotate('+deg+'deg)'; div.style.msTransform = 'rotate('+deg+'deg)'; div.style.oTransform = 'rotate('+deg+'deg)'; div.style.transform = 'rotate('+deg+'deg)'; rotated = !rotated; } </code></pre> <p><div class="snippet" data-lang="js" data-hide="true" data-console="false" data-babel="false"> <div class="snippet-code snippet-currently-hidden"> <pre class="snippet-code-js lang-js prettyprint-override"><code>var rotated = false; document.getElementById('button').onclick = function() { var div = document.getElementById('div'), deg = rotated ? 0 : 66; div.style.webkitTransform = 'rotate('+deg+'deg)'; div.style.mozTransform = 'rotate('+deg+'deg)'; div.style.msTransform = 'rotate('+deg+'deg)'; div.style.oTransform = 'rotate('+deg+'deg)'; div.style.transform = 'rotate('+deg+'deg)'; rotated = !rotated; }</code></pre> <pre class="snippet-code-css lang-css prettyprint-override"><code>#div { position:relative; height: 200px; width: 200px; margin: 30px; background: red; }</code></pre> <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;button id="button"&gt;rotate&lt;/button&gt; &lt;br /&gt;&lt;br /&gt; &lt;div id="div"&gt;&lt;/div&gt;</code></pre> </div> </div> </p> <p>To add some animation to the rotation all we have to do is add CSS transitions</p> <pre><code>div { -webkit-transition: all 0.5s ease-in-out; -moz-transition: all 0.5s ease-in-out; -o-transition: all 0.5s ease-in-out; transition: all 0.5s ease-in-out; } </code></pre> <p><div class="snippet" data-lang="js" data-hide="true" data-console="false" data-babel="false"> <div class="snippet-code snippet-currently-hidden"> <pre class="snippet-code-js lang-js prettyprint-override"><code>var rotated = false; document.getElementById('button').onclick = function() { var div = document.getElementById('div'), deg = rotated ? 0 : 66; div.style.webkitTransform = 'rotate('+deg+'deg)'; div.style.mozTransform = 'rotate('+deg+'deg)'; div.style.msTransform = 'rotate('+deg+'deg)'; div.style.oTransform = 'rotate('+deg+'deg)'; div.style.transform = 'rotate('+deg+'deg)'; rotated = !rotated; }</code></pre> <pre class="snippet-code-css lang-css prettyprint-override"><code>#div { position:relative; height: 200px; width: 200px; margin: 30px; background: red; -webkit-transition: all 0.5s ease-in-out; -moz-transition: all 0.5s ease-in-out; -o-transition: all 0.5s ease-in-out; transition: all 0.5s ease-in-out; }</code></pre> <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;button id="button"&gt;rotate&lt;/button&gt; &lt;br /&gt;&lt;br /&gt; &lt;div id="div"&gt;&lt;/div&gt;</code></pre> </div> </div> </p> <p>Another way to do it is using classes, and setting all the styles in a stylesheet, thus keeping them out of the javascript</p> <pre><code>document.getElementById('button').onclick = function() { document.getElementById('div').classList.toggle('rotated'); } </code></pre> <p><div class="snippet" data-lang="js" data-hide="true" data-console="false" data-babel="false"> <div class="snippet-code snippet-currently-hidden"> <pre class="snippet-code-js lang-js prettyprint-override"><code>document.getElementById('button').onclick = function() { document.getElementById('div').classList.toggle('rotated'); }</code></pre> <pre class="snippet-code-css lang-css prettyprint-override"><code>#div { position:relative; height: 200px; width: 200px; margin: 30px; background: red; -webkit-transition: all 0.5s ease-in-out; -moz-transition: all 0.5s ease-in-out; -o-transition: all 0.5s ease-in-out; transition: all 0.5s ease-in-out; } #div.rotated { -webkit-transform : rotate(66deg); -moz-transform : rotate(66deg); -ms-transform : rotate(66deg); -o-transform : rotate(66deg); transform : rotate(66deg); }</code></pre> <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;button id="button"&gt;rotate&lt;/button&gt; &lt;br /&gt;&lt;br /&gt; &lt;div id="div"&gt;&lt;/div&gt;</code></pre> </div> </div> </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload