PhpRiot
Download This Article
Download this article or the entire “Eight Weeks of Prototype” series with all listings and files.




More information
Related Books
JavaScript: The Good Parts

JavaScript: The Good Parts

Most programming languages contain good and bad parts, but JavaScript has more than its share of...

ASP.NET AJAX in Action

ASP.NET AJAX in Action

Ajax has revolutionized the way users interact with web pages today. Gone are frustrating page...
Browse Articles
Ajax (4), APC (1), CAPTCHA (1), CSS (3), Debugging (1), File Upload (1), Google (3), Google Maps (2), JavaScript (12), JSON (2), MVC (1), MySQL (7), onbeforeunload (1), OOP (1), PHP (28), PhpDoc (1), PostgreSQL (6), Prototype (11), Reflection (1), RFC 1867 (1), Robots (1), Scriptaculous (1), SEO (1), Sessions (1), SimpleXML (1), Smarty (5), SOAP (1), SPL (1), Templates (2), W3C (1), XHTML (1), Zend Framework (1), Zend_Search_Lucene (1)

PhpRiot Newsletter
Your Email Address:

Eight Weeks of Prototype: Week 4, Event Handling in Prototype

Handling Mouse Events

It is possible to return the coordinates of the mouse for a particular event using the Event.pointerX() and Event.pointerY() functions. You simply pass in the event object passed to the event handler as the first and only argument to receive the integer value representing the location of the mouse.

These methods return values relative to the entire page, not just what is currently visible to you. In other words, if you've scrolled down on the page the returned values are still relative to the very top of the page.

Listing 13 shows an example of tracking the mouse movement within a particular element using the mousemove event. This event is triggered every time the mouse is moved whilst over this element. In the handler function we read the X and Y position of the mouse and update the element to display this information.

Listing 13 Reading the X and Y coordinates of the mouse (listing-13.html)
<html>
    <head>
        <title>Reading the X and Y coordinates of the mouse</title>
        <script type="text/javascript" src="/js/prototype.js"></script>
    </head>
    <body>
        <div id="foo">
            Move the mouse over me
        </div>
 
        <script type="text/javascript">
            function onMouseMove(e)
            {
                var element = Event.element(e);
                element.update(Event.pointerX(e) + 'x' + Event.pointerY(e));
            }
 
            $('foo').observe('mousemove', onMouseMove);
        </script>
    </body>
</html>

In This Article


Tagged in ,