Michael Siebert
2006-Jul-21 11:42 UTC
Cross-Browser way to find relative coordinates of a mouse event
Hello again, im searching for a way to find the relative coordinates of a mouse click.ifyou dont understand what i mean: i have a DIV of a certain size positioned somewhere on the site. the user can click on that div and the app needs to know where exactly that is, the upper left corner of the div being (0, 0) and the lower right being eg. (200, 135) i tried something like x = Math.round(e.pageX - e.originalTarget.offsetLeft) y = Math.round(e.pageY - e.originalTarget.offsetTop); but that doesnt work as i have found out a few minutes before (and ie an opera dont have originalTarget) can you give me a hint? greetz -- Michael Siebert <info-norLuBQQNv0t+8dGM1inlw@public.gmane.org> www.stellar-legends.de - Weltraum-Browsergame im Alpha-Stadium _______________________________________________ Rails-spinoffs mailing list Rails-spinoffs-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails-spinoffs
Thomas Fuchs
2006-Jul-21 12:12 UTC
Re: Cross-Browser way to find relative coordinates of a mouse event
This works for me, but it''s a bit ugly... :) Event.localPointer = function(event){ var p = [Event.pointerX(event), Event.pointerY(event)]; var element = arguments[1] || Event.element(event); var e = Position.page($(element)); return [ p[0]-(e[0]+(window.pageXOffset || document.documentElement.scrollLeft || document.body.scrollLeft || 0)), p[1]-(e[1]+(window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop || 0))]; } Best, Thomas Am 21.07.2006 um 13:42 schrieb Michael Siebert:> Hello again, > im searching for a way to find the relative coordinates of a mouse > click.if you dont understand what i mean: i have a DIV of a certain > size positioned somewhere on the site. the user can click on that > div and the app needs to know where exactly that is, the upper left > corner of the div being (0, 0) and the lower right being eg. (200, > 135) > > i tried something like > x = Math.round(e.pageX - e.originalTarget.offsetLeft) > y = Math.round(e.pageY - e.originalTarget.offsetTop); > > but that doesnt work as i have found out a few minutes before (and > ie an opera dont have originalTarget) > > can you give me a hint? > > greetz > > -- > Michael Siebert <info-norLuBQQNv0t+8dGM1inlw@public.gmane.org> > > www.stellar-legends.de - Weltraum-Browsergame im Alpha-Stadium > _______________________________________________ > Rails-spinoffs mailing list > Rails-spinoffs-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails-spinoffs-- Thomas Fuchs wollzelle http://www.wollzelle.com questentier on AIM madrobby on irc.freenode.net http://www.fluxiom.com :: online digital asset management http://script.aculo.us :: Web 2.0 JavaScript http://mir.aculo.us :: Where no web developer has gone before _______________________________________________ Rails-spinoffs mailing list Rails-spinoffs-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails-spinoffs
Michael Siebert
2006-Jul-21 12:26 UTC
Re: Cross-Browser way to find relative coordinates of a mouse event
Thomas, that code kinda sucks but it works :) and if the other browsers (non-gecko, in this case) play nicely with it too, thats great. Thanks 2006/7/21, Thomas Fuchs <t.fuchs-moWQItti3gBl57MIdRCFDg@public.gmane.org>:> > This works for me, but it''s a bit ugly... :) > > Event.localPointer = function(event){ > var p = [Event.pointerX(event), Event.pointerY(event)]; > var element = arguments[1] || Event.element(event); > var e = Position.page($(element)); > return [ > p[0]-(e[0]+(window.pageXOffset || document.documentElement.scrollLeft|| > document.body.scrollLeft || 0)), > p[1]-(e[1]+(window.pageYOffset || document.documentElement.scrollTop > || document.body.scrollTop || 0))]; > } > > Best, > Thomas > > Am 21.07.2006 um 13:42 schrieb Michael Siebert: > > Hello again, > im searching for a way to find the relative coordinates of a mouse > click.if you dont understand what i mean: i have a DIV of a certain size > positioned somewhere on the site. the user can click on that div and the app > needs to know where exactly that is, the upper left corner of the div being > (0, 0) and the lower right being eg. (200, 135) > > i tried something like > x = Math.round(e.pageX - e.originalTarget.offsetLeft) > y = Math.round(e.pageY - e.originalTarget.offsetTop); > > but that doesnt work as i have found out a few minutes before (and ie an > opera dont have originalTarget) > > can you give me a hint? > > greetz > > -- > Michael Siebert <info-norLuBQQNv0t+8dGM1inlw@public.gmane.org> > > www.stellar-legends.de - Weltraum-Browsergame im Alpha-Stadium > _______________________________________________ > Rails-spinoffs mailing list > Rails-spinoffs-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails-spinoffs > > -- > Thomas Fuchs > wollzelle > > http://www.wollzelle.com > > questentier on AIM > madrobby on irc.freenode.net > > http://www.fluxiom.com :: online digital asset management > http://script.aculo.us :: Web 2.0 JavaScript > http://mir.aculo.us :: Where no web developer has gone before > > > > > > _______________________________________________ > Rails-spinoffs mailing list > Rails-spinoffs-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails-spinoffs > > >-- Michael Siebert <info-norLuBQQNv0t+8dGM1inlw@public.gmane.org> www.stellar-legends.de - Weltraum-Browsergame im Alpha-Stadium _______________________________________________ Rails-spinoffs mailing list Rails-spinoffs-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails-spinoffs