43

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

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

4

3 回答 3

65

要旋转 DIV,我们可以添加一些 CSS,使用 CSS 变换旋转来旋转 DIV。

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

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 过渡

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 之外

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>

于 2013-10-01T21:45:00.803 回答
5

假设您使用的是 jQuery 和 css3,可以很容易地完成:

http://jsfiddle.net/S7JDU/8/

HTML:

<div id="clicker">Click Here</div>
<div id="rotating"></div>

CSS:

#clicker { 
    width: 100px; 
    height: 100px; 
    background-color: Green; 
}

#rotating { 
    width: 100px; 
    height: 100px; 
    background-color: Red; 
    margin-top: 50px; 
    -webkit-transition: all 0.3s ease-in-out;
    -moz-transition: all 0.3s ease-in-out;
    -o-transition: all 0.3s ease-in-out;
    transition: all 0.3s ease-in-out;
}

.rotated { 
    transform:rotate(25deg); 
    -webkit-transform:rotate(25deg); 
    -moz-transform:rotate(25deg); 
    -o-transform:rotate(25deg); 
}

JS:

$(document).ready(function() {
    $('#clicker').click(function() {
        $('#rotating').toggleClass('rotated');
    });
});
于 2013-10-01T21:58:34.690 回答
5

我最近不得不建立类似的东西。您可以在下面的代码段中查看它。

我必须构建的版本使用相同的按钮来启动和停止微调器,但是如果您有一个按钮来启动旋转器和一个不同的按钮来停止旋转器,您可以操作代码

基本上,我的代码看起来像这样......

运行代码片段

var rocket = document.querySelector('.rocket');
var btn = document.querySelector('.toggle');
var rotate = false;
var runner;
var degrees = 0;

function start(){
    runner = setInterval(function(){
        degrees++;
        rocket.style.webkitTransform = 'rotate(' + degrees + 'deg)';
    },50)
}

function stop(){
    clearInterval(runner);
}

btn.addEventListener('click', function(){
    if (!rotate){
        rotate = true;
        start();
    } else {
        rotate = false;
        stop();
    }
})
body {
  background: #1e1e1e;
}    

.rocket {
    width: 150px;
    height: 150px;
    margin: 1em;
    border: 3px dashed teal;
    border-radius: 50%;
    background-color: rgba(128,128,128,0.5);
    display: flex;
    justify-content: center;
    align-items: center;
  }
  
  .rocket h1 {
    margin: 0;
    padding: 0;
    font-size: .8em;
    color: skyblue;
    letter-spacing: 1em;
    text-shadow: 0 0 10px black;
  }
  
  .toggle {
    margin: 10px;
    background: #000;
    color: white;
    font-size: 1em;
    padding: .3em;
    border: 2px solid red;
    outline: none;
    letter-spacing: 3px;
  }
<div class="rocket"><h1>SPIN ME</h1></div>
<button class="toggle">I/0</button>

于 2019-05-13T08:23:24.400 回答