YUI 3.x Home -

YUI Library Examples: Drag & Drop: Drag Node Plugin

Drag & Drop: Drag Node Plugin

This example shows using the Drag Node Plugin.

x

Drag Me

Setting up the Node

First we need to create an HTML Node to make draggable.

  1. <div id="demo"><h2>x</h2>Drag Me</div>
<div id="demo"><h2>x</h2>Drag Me</div>

Now we give that Node some CSS to make it visible.

  1. #demo {
  2. height: 100px;
  3. width: 100px;
  4. border: 1px solid black;
  5. background-color: #8DD5E7;
  6. padding: 7px;
  7. }
  8. #demo h2 {
  9. padding: 0;
  10. margin: 0;
  11. position: absolute;
  12. top: 5px;
  13. right: 5px;
  14. font-size: 110%;
  15. color: black;
  16. font-weight: bold;
  17. cursor: move;
  18. }
#demo {
    height: 100px;
    width: 100px;
    border: 1px solid black;
    background-color: #8DD5E7;
    padding: 7px;
}
#demo h2 {
    padding: 0;
    margin: 0;
    position: absolute;
    top: 5px;
    right: 5px;
    font-size: 110%;
    color: black;
    font-weight: bold;
    cursor: move;
}

Setting up the YUI Instance

Now we need to create our YUI instance and tell it to load the dd-plugin module.

  1. YUI().use('dd-plugin');
YUI().use('dd-plugin');

Making the Node draggable with the Drag Plugin

Now that we have a YUI instance with the dd-plugin module, we need to instantiate a Node instance on this Node.

  1. YUI().use('dd-plugin', function(Y) {
  2. var node = Y.get('#demo');
  3. node.plug(Y.Plugin.Drag);
  4. });
YUI().use('dd-plugin', function(Y) {
    var node = Y.get('#demo');
    node.plug(Y.Plugin.Drag);
});

Accessing the Drag instance

Now that we have a draggable Node, you can get access to the Drag Instance from the dd namespace on the Node instance.

  1. YUI().use('dd-plugin', function(Y) {
  2. var node = Y.get('#demo');
  3. node.plug(Y.Plugin.Drag);
  4.  
  5. //Now you can only drag it from the x in the corner
  6. node.dd.addHandle('h2');
  7. });
YUI().use('dd-plugin', function(Y) {
    var node = Y.get('#demo');
    node.plug(Y.Plugin.Drag);
 
    //Now you can only drag it from the x in the corner
    node.dd.addHandle('h2'); 
});

Copyright © 2009 Yahoo! Inc. All rights reserved.

Privacy Policy - Terms of Service - Copyright Policy - Job Openings