直接使用 jQuery:
如果我有一个固定框(例如,一个彩色矩形),并且将鼠标移入或移出其中,则如果我将鼠标光标以一种或另一种方式移动到框的边界上,jQuery 就会给我事件。
如果我有一个以编程方式移动的彩色矩形,例如向右移动,然后我将鼠标放在框的右侧并等待,该框将在鼠标光标下方移动并移过它,但不会生成任何鼠标事件(或者至少是我知道的鼠标事件)。
当对象正在移动而鼠标光标静止时,有哪些方法(如果有)可以接收语义上与“静止对象,移动鼠标光标”相当的内容?
尝试创建全局变量来存储当前pageX
, pageY
;使用设置全局变量mousemove
事件附加到window
; use step
的财产.animate()
计算动画元素当前位置、引用的选项offsetLeft
, offsetTop
, getBoundingClientRect().bottom
;检查当前鼠标位置相对于元素底部的 offsets 。
还可以通过在内部执行相同的检查来补充流程mousemove
事件处理程序
var x = 0,
y = 0;
$(window).on("mousemove", function(e) {
x = e.pageX;
y = e.pageY
})
$("div")
.animate({
left: window.innerWidth - 150
}, {
duration: 5000,
step: function() {
var l = this.offsetLeft,
t = this.offsetTop,
b = this.getBoundingClientRect().bottom;
// if element passes over mouse, log positions to `console`
if ((x === l || x === l + 1 || x === l - 1) && y > t && y < b)
console.log("pageX:", x
, "pageY:", y
, "offsetLeft:", l
, "offsetTop:", t
, "BoundingClientRect().bottom:", b)
}
})
div {
width: 100px;
height: 100px;
background: olive;
position: relative;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js">
</script>
<div></div>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)