[英]I want to rotate a “div” randomly, when i click a button
"Pijl" is the div I want to rotate randomly. “ Pijl”是我要随机旋转的div。 "Knop" is the button that is needed to make the div rotate.
“旋钮”是使div旋转所需的按钮。
var pijl = document.querySelector("#arrow");
var knop = document.querySelector("#bottom");
var spin = Math.round(Math.random() * (360));
knop.addEventListener("click", draai);
function draai(evt) {
pijl.rotate(spin + "deg");
}
There is no rotate()
method, you need apply the css style property instead. 没有
rotate()
方法,您需要改用CSS样式属性。 You can refer the following question : How to set the style -webkit-transform dynamically using JavaScript? 您可以参考以下问题: 如何使用JavaScript动态设置样式-webkit-transform? or Rotate a div using javascript .
或使用javascript旋转div 。 Also you need to move the variable spin inside the function if you want to generate random values on each click.
如果要在每次单击中生成随机值,还需要在函数内部移动变量旋转。
var pijl = document.querySelector("#arrow"); var knop = document.querySelector("#bottom"); knop.addEventListener("click", draai); function draai(evt) { var spin = Math.round(Math.random() * (360)); pijl.style.transform = 'rotate(' + spin + 'deg)'; }
<div id="arrow" style="margin:50px;width:50px;height:20px;background:red">arrow</div> <button id="bottom">click</button>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.