[OpenLayers-Trac] Re: [OpenLayers] #2247: getMousePosition off due to borders and relative/absolute positioning

OpenLayers trac-20090302 at openlayers.org
Mon Nov 1 15:07:03 EDT 2010

#2247: getMousePosition off due to borders and relative/absolute positioning
 Reporter:  chrelad@…                                                                          |       Owner:  euzuro      
     Type:  bug                                                                                |      Status:  new         
 Priority:  critical                                                                           |   Milestone:  2.11 Release
Component:  Events                                                                             |     Version:  2.8         
 Keywords:  mouse position getMousePosition offset click coordinates border relative position  |       State:  Review      
Changes (by ahocevar):

  * state:  Needs More Work => Review


 The issue reported here turned out to be a serious rabbit hole:
  * !OpenLayers.Util.pagePosition returns incorrect positions if a map div
 with {{{position: relative}}} or {{{position: absolute}}} is nested in a
 stack of containers that have borders. The attached patch fixes this issue
 by replacing !OpenLayers.Util.pagePosition with a function inspired by
 http://code.google.com/p/doctype/wiki/ArticlePageOffset, which uses code
 from Yahoo's getXY method.
  * The map div's border - set to 1px in all our examples except
 fullScreen.html - causes an offset of 1px. Thicker borders (like in
 attachment:mouseoffset.html) increase the offset. This is independent of
 the positioning method of the map div. The patch fixes this by attaching
 the Events instance to the map's viewPortDiv instead of the div. This is
 perfectly sane because the map div just serves as a container.
  * The page's scroll offset only works correctly due to adjustments in
 !OpenLayers.Event.getMousePosition. By adding a new
 !OpenLayers.Util.getViewportElement function, which determines the
 viewport element (which page position calculations are based on) the same
 way the new pagePosition method does, we now have a reliable base for
 scrollTop and scrollLeft. This function uses code taken from

 The patch adds a new manual test (tests/manual/page-position.html), which
 reveals all issues. Unit tests continue to pass in FF3.6, Safari 5 and
 IE8. The manual test displays fine in the same browsers, and also in Opera
 10.6 and Chrome 7.

 Thanks for any review.

Ticket URL: <http://trac.openlayers.org/ticket/2247#comment:3>
OpenLayers <http://openlayers.org/>
A free AJAX map viewer

More information about the Trac mailing list