我的网页上有Bing地图,我想检测用户在窗口中点击的时间。但是,我不希望检测用户何时拖动地图(这也会生成“点击”事件)。只获得“纯粹”点击事件的最佳方式是什么?
答案 0 :(得分:3)
我的解决方案最终是手动检查点击位置是否接近鼠标被按下的位置。
Microsoft.Maps.Events.addHandler(map, "click", clickHandler);
Microsoft.Maps.Events.addHandler(map, "mousedown", function(me) { lastMouseDownPoint = new Microsoft.Maps.Point(me.getX(), me.getY());});
function clickHandler(mouseEventArgs){
var point = new Microsoft.Maps.Point(mouseEventArgs.getX(), mouseEventArgs.getY());
//Drag detection
// Edited since the comma is incorrect, should be a plus as per pythagorean theorem
var dist = Math.sqrt(Math.pow(point.x-lastMouseDownPoint.x,2) + Math.pow(point.y-lastMouseDownPoint.y,2));
if(dist > 5) {
// We call this a drag
return;
}
// We have a "pure" click and can process it
}
答案 1 :(得分:1)
很简单:
Microsoft.Maps.Events.addHandler(map, 'click', onClick);
function onClick(e) {
if (e.mouseMoved === false && e.isPrimary === true) {
// Left click not being a drag
...
}
}
mouseMoved在拖放时为true,否则为false。
MouseEventArgs文档http://msdn.microsoft.com/en-us/library/gg406731.aspx未引用mouseMoved:/