<?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>Jon Allured &#187; javascript</title>
	<atom:link href="http://www.jonallured.com/archives/category/javascript/feed" rel="self" type="application/rss+xml" />
	<link>http://www.jonallured.com</link>
	<description>Geeky web developer, standards advocate and picky web surfer</description>
	<lastBuildDate>Wed, 30 Dec 2009 17:35:58 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Finding the mouse position when an event fires</title>
		<link>http://www.jonallured.com/archives/1</link>
		<comments>http://www.jonallured.com/archives/1#comments</comments>
		<pubDate>Fri, 25 Dec 2009 14:34:53 +0000</pubDate>
		<dc:creator>Jon Allured</dc:creator>
				<category><![CDATA[firebug]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[jquery]]></category>

		<guid isPermaLink="false">http://www.jonallured.com/wordpress/?p=1</guid>
		<description><![CDATA[How to work with the event object in jQuery to determine the mouse coordinates and a clever technique to keep a scrolling div scrolled all the way down to the bottom.]]></description>
			<content:encoded><![CDATA[<p>We recently had a project where we needed to find out where the mouse was (its coordinates) when an event fired (hover). I wanted to create a little <a href="http://files.jonallured.com/examples/mouse_position.html">example page</a> that I could play around with to see how this worked.</p>
<p>The markup is really simple and just creates four divs in red &#8211; on <code>over</code> their background color is changed to green and then its changed back on <code>out</code>.  We&#8217;re using jQuery here, so the syntax is based on the <a href="http://docs.jquery.com/Events/hover">hover documentation</a> and the solution as inspired by their <a href="http://docs.jquery.com/Tutorials:Mouse_Position">Mouse Position tutorial</a>.</p>
<p>The magic is really just the bit where you define the hover functions and then the <code>pageX</code> and <code>pageY</code> attributes of <code>e</code>:</p>
<pre><code>$('div.block').hover(
   function (e) {
      ...
      var msg = 'ON! x=' + e.pageX + ', y=' + e.pageY;
      ...
   },
   function (e) {
      ...
      var msg = 'OFF! x=' + e.pageX + ', y=' + e.pageY;
      ...
   }
);</code></pre>
<p>If you pass the hover functions a variable, it will give you access to the <code><a href="http://docs.jquery.com/Events/jQuery.Event">event object</a></code> through which you can determine the mouse&#8217;s position by using its <code>pageX</code> and <code>pageY</code> attributes &#8211; slick, huh?</p>
<p>So, I really liked this and wanted to see it in action &#8211; I thought <a href="http://getfirebug.com/console.html">logging the info to the FireBug console</a> would be a nice way to see it, but later I realized that I wanted to run this code in other browsers (IE), so I made my own console div and then wrote the coordinates to it.</p>
<p>When I tested this out, however, I didn&#8217;t like that I had to keep scrolling the div to the bottom, so I found a <a href="http://radio.javaranch.com/pascarello/2005/12/14/1134573598403.html">nice technique</a> to keep it at the bottom:</p>
<pre><code>function (e) {
   ...
   var consoleDiv = document.getElementById('console');
   consoleDiv.scrollTop = consoleDiv.scrollHeight;
}</code></pre>
<p>The <code>div</code> element&#8217;s <code>scrollTop</code> is set to its <code>scrollHeight</code> so that it will remain scrolled to the bottom as its size grows. I had never heard of these attributes, but they are nicely documented in Mozilla&#8217;s Developer Center: <a href="https://developer.mozilla.org/en/DOM/element.scrollTop"><code>scrollTop</code></a>, <a href="https://developer.mozilla.org/en/DOM/element.scrollHeight"><code>scrollHeight</code></a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.jonallured.com/archives/1/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
