Clicking the button will use Dom's getXY
method to get the position of the red element and pass it to the setXY
method, which will move the blue element to that position.
getXY
, part of the YUI Dom Collection, makes it easy to get an element's position relative to the document.
To illustrate the use of getXY
, we'll create a <div>
called foo
and a <div>
called bar
. When the document is clicked, foo
will move to the top left corner of bar
.
Start with some simple CSS rules, markup for the demo elements, and a button to trigger the demo:
1 | <style type="text/css"> |
2 | #foo { |
3 | background-color:#00f; |
4 | height:10px; |
5 | width:10px; |
6 | } |
7 | #bar { |
8 | background-color:#f00; |
9 | height:100px; |
10 | width:100px; |
11 | margin:0 100px 1em; |
12 | } |
13 | </style> |
14 | |
15 | <div id="foo"></div> |
16 | <div id="bar"></div> |
17 | <button id="demo-run">run</button> |
view plain | print | ? |
Now we will define the function that moves foo
to the xy
position of bar
. The only argument of the getXY
method is either the ID of an HTMLElement, or an actual HTMLElement object. The getXY
method returns an array containing two values: [x, y]
where x
is the distance from the left edge of the document, and y
is the distance from the top edge of the document. The YUI Dom Collection provides a setXY
that accepts an array in the same format.
1 | <script type="text/javascript"> |
2 | var move = function() { |
3 | var xy = YAHOO.util.Dom.getXY('bar'); |
4 | YAHOO.util.Dom.setXY('foo', xy); |
5 | }; |
6 | </script> |
view plain | print | ? |
Next we will use the YUI Event Utility's on
method to listen for clicks on the button.
1 | YAHOO.util.Event.on('demo-run', 'click', move); |
view plain | print | ? |
This is a simple example of how to use the YAHOO.util.Dom.getXY
method. One of the powerful things about this is that regardless of what is influencing the element's position, be it positioning (absolute, relative, etc.), margins, an offsetParent
(any positioned ancestor), or any other factors that may affect it, getXY
will return a position in document coordinates (e.g. [0, 0]
will be the upper left corner of the document).
You can load the necessary JavaScript and CSS for this example from Yahoo's servers. Click here to load the YUI Dependency Configurator with all of this example's dependencies preconfigured.
INFO 38ms (+0) 6:10:20 PM:
Dom
generateId generating yui-log-filter-Dom010
WARN 38ms (+0) 6:10:20 PM:
Dom
batch called with invalid arguments
INFO 38ms (+0) 6:10:20 PM:
Dom
generateId generating yui-log-filter-LogReader09
WARN 38ms (+0) 6:10:20 PM:
Dom
batch called with invalid arguments
INFO 38ms (+0) 6:10:20 PM:
Dom
generateId generating yui-log-filter-Get08
WARN 38ms (+0) 6:10:20 PM:
Dom
batch called with invalid arguments
INFO 38ms (+0) 6:10:20 PM:
Dom
generateId generating yui-log-filter-example07
WARN 38ms (+0) 6:10:20 PM:
Dom
batch called with invalid arguments
INFO 38ms (+0) 6:10:20 PM:
Dom
generateId generating yui-log-filter-global06
WARN 38ms (+0) 6:10:20 PM:
Dom
batch called with invalid arguments
INFO 38ms (+0) 6:10:20 PM:
Dom
generateId generating yui-log-filter-window05
WARN 38ms (+0) 6:10:20 PM:
Dom
batch called with invalid arguments
INFO 38ms (+0) 6:10:20 PM:
Dom
generateId generating yui-log-filter-time04
WARN 38ms (+0) 6:10:20 PM:
Dom
batch called with invalid arguments
INFO 38ms (+0) 6:10:20 PM:
Dom
generateId generating yui-log-filter-error03
WARN 38ms (+0) 6:10:20 PM:
Dom
batch called with invalid arguments
INFO 38ms (+0) 6:10:20 PM:
Dom
generateId generating yui-log-filter-warn02
WARN 38ms (+0) 6:10:20 PM:
Dom
batch called with invalid arguments
INFO 38ms (+0) 6:10:20 PM:
Dom
generateId generating yui-log-filter-info01
WARN 38ms (+1) 6:10:20 PM:
Dom
batch called with invalid arguments
INFO 37ms (+1) 6:10:20 PM:
Dom
generateId generating yui-log-hd00
INFO 36ms (+1) 6:10:20 PM:
LogReader instance0
LogReader initialized
INFO 35ms (+1) 6:10:20 PM:
Get
Appending node: ../../../2.x/build/event-mouseenter/event-mouseenter-min.js
INFO 34ms (+0) 6:10:20 PM:
Get
attempting to load ../../../2.x/build/event-mouseenter/event-mouseenter-min.js
INFO 34ms (+31) 6:10:20 PM:
Get
_next: q0, loaded: undefined
INFO 3ms (+3) 6:10:20 PM:
example
The example has finished loading; as you interact with it, you'll see log messages appearing here.
INFO 0ms (+0) 6:10:20 PM:
global
Logger initialized
Note: You are viewing this example in debug mode with logging enabled. This can significantly slow performance.
Copyright © 2011 Yahoo! Inc. All rights reserved.
Privacy Policy - Terms of Service - Copyright Policy - Job Openings