我有一个简单的问题(我希望很简单)。我使用 10 个 div(div1、div2...),我想计算其中两个之间的距离。我已经点击了一个,它负责上课(img.home)。我有计算两个对象之间的距离 showdistance(div1, div2)
的函数,但是如何使用它来计算 showdistance($('img.home').parent() [0], $(this) )
其中 (this) 是 div,我的光标此时指向 ( .mouseenter(function() {$(this).text(showdistance) });)
提前致谢, 如果需要,我可以用整个代码制作一个jsfiddle,但我相信这是一件相当简单的事情,我不知道而且代码很大,这样更容易。
致以诚挚的问候。
最佳答案
将最后单击的元素存储在变量中。 然后在输入元素时比较位置。
如果您愿意,您可以应用顶部
、左
、右
或底部
。
var clicked = null;
$("someSelection").click(function() {
clicked = $(this);
});
$("someSelection").mouseenter(function() {
// if-statement to ensure that the event won't fire if you haven't clicked one
if(clicked !== null) {
console.log($(this).position().top - clicked.position().top);
console.log($(this).position().left - clicked.position().left);
}
});
关于javascript - 这个对象在 .mouseenter() 函数中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24368199/