<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Martini Lab Blog &#187; event</title>
	<atom:link href="http://www.martinilab.com/blog/tag/event/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.martinilab.com/blog</link>
	<description>Web design, CSS, scripting, Adobe, tips and other scraps of things that come my way</description>
	<lastBuildDate>Thu, 09 Sep 2010 15:06:18 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>jQuery missing layerX</title>
		<link>http://www.martinilab.com/blog/68/jquery-missing-layerx/</link>
		<comments>http://www.martinilab.com/blog/68/jquery-missing-layerx/#comments</comments>
		<pubDate>Wed, 08 Apr 2009 18:41:46 +0000</pubDate>
		<dc:creator>Chris Williams</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[event]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[tips]]></category>

		<guid isPermaLink="false">http://www.martinilab.com/blog/?p=68</guid>
		<description><![CDATA[Getting the Mouse position in jQuery is very simple. However, getting the position from within an element take some extra steps. Normally, a javascript event such as onmousemove would use event.layerX and event.layerY to get the coordinates of our mouse &#8230; <a href="http://www.martinilab.com/blog/68/jquery-missing-layerx/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Getting the Mouse position in jQuery is very simple.  However, getting the position from within an element take some extra steps.</p>
<p>Normally, a javascript event such as <code>onmousemove</code> would use <code>event.layerX</code> and <code>event.layerY</code> to get the coordinates of our mouse when it moves.</p>
<p>For jQuery, <code>pageX</code> and <code>pageY</code> will return values from the top left of the page and not the layer.  Unless the element you’re working with is left-aligned, you need to calculate the offset of that element as well.  Not only that (unless there is a better way to do this) the offset values returned doesn’t include the offset values of the parent tag.</p>
<p>In this example, I have two divs.</p>
<pre lang="html4strict" line="1">
&lt;div id="frame"&gt;
	&lt;div id="content"&gt;&lt;/div&gt;
&lt;/div&gt;
</pre>
<p>And you can imagine that <code>#frame</code> is centered and has padding. Getting <code>#content</code>’s offset won’t include anything outside of the frame so <code>$(#content).offset().left;</code> would only get you the distance of itself and its parent.</p>
<h2>Get your layerX for jQuery</h2>
<p>For this example, we are using a known set of divs so we can just write out each div in this script.</p>
<pre lang="javascript" line="1">

$(document).ready(function(){
	$('#canvasForeground').mousemove(function(e){
		var myLayerX = $('#content').offset().left + $('#frame').offset().left;
		var myLayerY = $('#content').offset().top + $('#frame').offset().top;
		var x = 0, y = 0;
		x = e.pageX - myLayerX;
		y = e.pageY - myLayerY;
	});
});
</pre>
<p>The variables are declared inside the event function to handle window resizing.  Now were this a more complicated page and the element in question has deeply nested or even dynamically generated, writing our all the parent tags wouldn’t necessarily work (or be very efficient).* But the approach would remain the same.  That’s it!</p>
<p>*<em>standard apology for poor code writing skills</em></p>
]]></content:encoded>
			<wfw:commentRss>http://www.martinilab.com/blog/68/jquery-missing-layerx/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
