<i id='pLHdm'><tr id='pLHdm'><dt id='pLHdm'><q id='pLHdm'><span id='pLHdm'><b id='pLHdm'><form id='pLHdm'><ins id='pLHdm'></ins><ul id='pLHdm'></ul><sub id='pLHdm'></sub></form><legend id='pLHdm'></legend><bdo id='pLHdm'><pre id='pLHdm'><center id='pLHdm'></center></pre></bdo></b><th id='pLHdm'></th></span></q></dt></tr></i><div id='pLHdm'><tfoot id='pLHdm'></tfoot><dl id='pLHdm'><fieldset id='pLHdm'></fieldset></dl></div>
    1. <small id='pLHdm'></small><noframes id='pLHdm'>

        <bdo id='pLHdm'></bdo><ul id='pLHdm'></ul>

      <tfoot id='pLHdm'></tfoot>

        <legend id='pLHdm'><style id='pLHdm'><dir id='pLHdm'><q id='pLHdm'></q></dir></style></legend>
      1. 使用 javascript 旋转 div

        时间:2023-10-13
              <legend id='YSFh1'><style id='YSFh1'><dir id='YSFh1'><q id='YSFh1'></q></dir></style></legend>
                <tbody id='YSFh1'></tbody>

            1. <i id='YSFh1'><tr id='YSFh1'><dt id='YSFh1'><q id='YSFh1'><span id='YSFh1'><b id='YSFh1'><form id='YSFh1'><ins id='YSFh1'></ins><ul id='YSFh1'></ul><sub id='YSFh1'></sub></form><legend id='YSFh1'></legend><bdo id='YSFh1'><pre id='YSFh1'><center id='YSFh1'></center></pre></bdo></b><th id='YSFh1'></th></span></q></dt></tr></i><div id='YSFh1'><tfoot id='YSFh1'></tfoot><dl id='YSFh1'><fieldset id='YSFh1'></fieldset></dl></div>

              <tfoot id='YSFh1'></tfoot>
                <bdo id='YSFh1'></bdo><ul id='YSFh1'></ul>

                1. <small id='YSFh1'></small><noframes id='YSFh1'>

                  本文介绍了使用 javascript 旋转 div的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我想单击一个 div 并旋转另一个 div,然后当再次单击第一个 div 时,另一个 div 会旋转回原来的位置.

                  I want to click on one div and rotate another div then when the firsts div is clicked again the other div rotates back to its original position.

                  如果需要,我可以参考这个库http://ricostacruz.com/jquery.transit.

                  I can reference this library if required http://ricostacruz.com/jquery.transit.

                  推荐答案

                  要旋转 DIV,我们可以添加一些 CSS,使用 CSS transform rotate 旋转 DIV.

                  To rotate a DIV we can add some CSS that, well, rotates the DIV using CSS transform rotate.

                  要切换旋转,我们可以保留一个标志,一个带有布尔值的简单变量,告诉我们旋转的方式.

                  To toggle the rotation we can keep a flag, a simple variable with a boolean value that tells us what way to rotate.

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

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

                  #div {
                      position:relative; 
                      height: 200px; 
                      width: 200px; 
                      margin: 30px;
                      background: red;
                  }

                  <button id="button">rotate</button>
                  <br /><br />
                  <div id="div"></div>

                  要为旋转添加一些动画,我们所要做的就是添加 CSS 过渡

                  To add some animation to the rotation all we have to do is add CSS transitions

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

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

                  #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;
                  }

                  <button id="button">rotate</button>
                  <br /><br />
                  <div id="div"></div>

                  另一种方法是使用类,并在样式表中设置所有样式,从而将它们排除在 javascript 之外

                  Another way to do it is using classes, and setting all the styles in a stylesheet, thus keeping them out of the javascript

                  document.getElementById('button').onclick = function() {
                      document.getElementById('div').classList.toggle('rotated');
                  }
                  

                  document.getElementById('button').onclick = function() {
                      document.getElementById('div').classList.toggle('rotated');
                  }

                  #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); 
                  }

                  <button id="button">rotate</button>
                  <br /><br />
                  <div id="div"></div>

                  这篇关于使用 javascript 旋转 div的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:如何在Javascript中计算二维旋转 下一篇:根据单独元素中的光标位置旋转元素

                  相关文章

                  最新文章

                2. <tfoot id='c4bqg'></tfoot>
                    <bdo id='c4bqg'></bdo><ul id='c4bqg'></ul>
                  1. <legend id='c4bqg'><style id='c4bqg'><dir id='c4bqg'><q id='c4bqg'></q></dir></style></legend>
                    <i id='c4bqg'><tr id='c4bqg'><dt id='c4bqg'><q id='c4bqg'><span id='c4bqg'><b id='c4bqg'><form id='c4bqg'><ins id='c4bqg'></ins><ul id='c4bqg'></ul><sub id='c4bqg'></sub></form><legend id='c4bqg'></legend><bdo id='c4bqg'><pre id='c4bqg'><center id='c4bqg'></center></pre></bdo></b><th id='c4bqg'></th></span></q></dt></tr></i><div id='c4bqg'><tfoot id='c4bqg'></tfoot><dl id='c4bqg'><fieldset id='c4bqg'></fieldset></dl></div>

                    1. <small id='c4bqg'></small><noframes id='c4bqg'>