【发布时间】:2018-03-15 23:05:16
【问题描述】:
我正在尝试创建一个圆形导航按钮,以在光标位于某个框内时跟随鼠标移动。
var cer = document.getElementById('cerchio');
var pro = document.getElementById('prova');
pro.addEventListener("mouseover", function() {
var e = window.event;
var x = e.clientX;
var y = e.clientY;
cer.style.top = y + "px";
cer.style.left = x + "px";
cer.style.transition = "2s";
});
pro.addEventListener("mouseout", function() {
cer.style.top = "15px";
cer.style.left = "15px";
});
#prova {
width: 200px;
height: 200px;
border: 1px solid black;
}
#cerchio {
width: 90px;
height: 90px;
border: 1px solid red;
border-radius: 90px;
position: absolute;
left: 15px;
top: 15px;
}
#innercircle {
width: 120px;
height: 120px;
position: relative;
left: 40px;
top: 30px;
border: 1px solid red;
}
<div id="prova">
<div id="innercircle">
<div id="cerchio"></div>
</div>
</div>
所以它实际上跟随鼠标在黑色边框内的第一个位置,我希望它每次都更新光标位置并跟随它,我也不希望红色圆圈走出红色框,任何建议?请只使用javascript而不是jquery,谢谢!
【问题讨论】:
-
window.event是非标准的。请改用e作为参数。如果您希望圆圈每次都移动,请使用mousemove。
标签: javascript