javascript - 为什么通过 mousemove 旋转 div 不起作用 javascript?
问题描述
我想要的是让 div 从 0 旋转到 360 而没有 -。旋转应顺时针从 0 度到 360 度。在这段代码中,它的范围从 0 到 180 和从 -180 到 0。
第二个问题:每次我点击“rotator div”我的位置都会改变???
代码:
document.getElementById("rotator").addEventListener('mousedown', mouseDownRotator, false);
function mouseDownRotator(e) {
window.addEventListener("mousemove", mouseMoveRotator, true);
}
function mouseMoveRotator(e) {
window.addEventListener('mouseup', mouseUpRotator, true);
var arrow = document.getElementById("rotator");
var arrowRects = document.getElementById("dragdiv").getBoundingClientRect();
var center_x = (arrowRects.left) + (arrowRects.width / 2);
var center_y = (arrowRects.top) + (arrowRects.height / 2);
var mouse_x = e.pageX;
var mouse_y = e.pageY;
var radians = Math.atan2(mouse_x - center_x, mouse_y - center_y);
document.getElementById("dragdiv").style.transform = "rotate(" + (radians * (180 / Math.PI) * -1) + 90 + "deg)";
}
function mouseUpRotator() {
window.removeEventListener("mousemove", mouseMoveRotator, true);
}
#dragdiv {
width: 100px;
height: 100px;
background-color: red;
position: absolute;
margin: 250px;
}
#rotator {
width: 10px;
height: 10px;
border-radius: 50%;
background: white;
border: 3px solid #4286f4;
position: absolute;
top: -50px;
right: 43%;
cursor: nwse-resize;
}
<div id="container">
<div id="dragdiv"></div>
</div>
解决方案
推荐阅读
- r - 当它们是“因素”时,从 R 中的数据框中提取字符串列
- model-view-controller - 如何在取消选择时更改剑道图表图例项目颜色?
- laravel - 在 laravel vue 中检查用户状态
- html - 使用 css 命令更改选项卡标题
- android - 当没有唯一的 id/类名时,如何在 appium 中选择复选框?
- python - 使用 pandas.read_csv 读取文件名中包含 str() 的文件
- ns2 - NS2:接收数据包为零
- android - 未调用 RecognitionListener 中的 onBufferReceived。如何构建 BufferReceived 以使用 RecognizerIntent 捕获语音
- python - Python Pandas groupby:A组与非A组?
- raspberry-pi - 无磁盘写入的运动检测