简体   繁体   English

当我单击按钮时,我想随机旋转“ div”

[英]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.

相关问题 我只是想随机点击一些按钮然后当我点击Sum按钮时它会显示总和 - I just want to randomly click some button and then when I click Sum button it will show the sum 当你点击一个按钮时,我想让一个 div 出现 - I want to make a div appear when you click a button 我要始终在单击“提交”按钮时删除div容器吗? - I want to remove the div container always when click submit button? 当我单击打开和关闭div按钮时,我想通过JavaScript切换div - I want to toggle div by JavaScript when i click on the button open and close the div 当我点击按钮时 Vue JS 我希望 div 滚动到 div 的底部 - Vue JS when I click on button I want that the div scroll to bottom of the div 单击按钮时随机选择要再现的声音的脚本 - Script that randomly choose a sound to reproduce when I click on a button 当有人点击 html 按钮或 div 时,我想 select 并在 google Geochart 上突出显示国家/地区 - I want to select and highlight country on google Geochart when someone click on a html button or div 单击按钮时如何使图像旋转 - How to make a image rotate when i click the button 我想当我点击模态上的单选按钮(objectart)时,它应该在另一个 div(objecttype)中更改模态上的其他 div 数据(获取数据库) - i want when i click on radio button (objectart) on modal then it should change other div data (fetch database) on modal in another div(objecttype) 单击任何按钮时关闭div - Close div when I click on any button
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM