【发布时间】:2022-01-14 22:35:38
【问题描述】:
我遇到了一些问题,导致图像在跟随光标后正确旋转。我认为它是根据它的原始位置旋转的,但我不确定。
有没有机会让它旋转 360 度,而不管它的最终位置如何?
代码:
$(document).mousemove(function(e) {
/*duration determines the speed of the animation (in this case, the speed to which prgm follows)*/
$("#image").stop().animate({
left: e.pageX,
top: e.pageY
}, {
duration: 5000
});
});
let box = document.querySelector(".box");
let boxBoundingRect = box.getBoundingClientRect();
let boxCenter = {
x: boxBoundingRect.left + boxBoundingRect.width / 2,
y: boxBoundingRect.top + boxBoundingRect.height / 2
};
document.addEventListener("mousemove", e => {
let angle = Math.atan2(e.pageX - boxCenter.x, -(e.pageY - boxCenter.y) )*(180 / Math.PI);
box.style.transform = `rotate(${angle}deg)`;
})
.box {
background-color: black;
width: 30px;
height: 30px;
position: absolute;
left: 200px;
top: 200px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<body>
<div class="box" id="image"> </div>
</body>
【问题讨论】:
-
如果我的回答对您有帮助,请务必将其标记为已选答案!
标签: javascript html jquery