我想点击一个div并旋转另一个div然后再次点击第一个div时另一个div旋转回原来的位置。
如果需要,我可以引用此库http://ricostacruz.com/jquery.transit。
答案 0 :(得分:43)
要旋转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>
答案 1 :(得分:5)
假设您正在使用jQuery和css3:
,可以很容易地完成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');
});
});
答案 2 :(得分:2)
我最近不得不构建类似的东西。您可以在下面的代码段中查看它。
我必须构建的版本使用相同的按钮来启动和停止微调器,但是如果您有一个按钮可以开始旋转并且可以使用另一个按钮来停止旋转,则可以操纵代码
基本上,我的代码是这样的...
运行代码段
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>