YUI 3.x Home -

YUI Library Examples: Drag & Drop: List reorder w/Scrolling

Note: This is YUI 3.x. Looking for YUI 2.x?

Drag & Drop: List reorder w/Scrolling

This example shows how to make a sortable list using Custom Event Bubbling and Node scrolling.

  • Item #1
  • Item #2
  • Item #3
  • Item #4
  • Item #5
  • Item #6
  • Item #7
  • Item #8
  • Item #9
  • Item #10
  • Item #11
  • Item #12
  • Item #13
  • Item #14
  • Item #15
  • Item #16
  • Item #17
  • Item #18
  • Item #19
  • Item #20

Setting up the lists

First we will make a list that we want to make sortable.

Make it scrollable

Now add some CSS to make it scrollable.

Setting up the YUI Instance

Now we need to create our YUI instance and tell it to load the dd-constrain, dd-proxy, dd-drop and dd-scroll, modules.

Making the Nodes Drag Instances and Drop Targets

Now we have our YUI instance ready, we can make the list items draggable. We will do this using Y.Node.all

We will be passing the selector string #play ul li to Y.Node.all to have it return us a NodeList of the li's in our 2 lists. Using this selector syntax we will be able to add new list markup to the #play div and not have to change our code.

Then we will walk that NodeList and create our draggable Nodes.

Note that we are setting the following configs on the Drag instance: proxy, moveOnEnd, constrain2node, target.

Making the List Drop Target's too

We need to make the UL nodes a Drop Target so we can catch drops on the empty space of the list. Using this selector syntax we will be able to add new list markup to the #play div and not have to change our code.

Using Event Bubbling

By default, all Drag and Drop instances bubble their event's up to the DragDropMgr. In this example we are assuming that there are no other Drag Operations in this YUI Instance.

Start Drag Event

The first thing we will do is handle the drag:start event. In this event, we will setup some styles to apply to the node and dragNode of the current Drag instance.

We will also be copying the innerHTML of the node and copying that to the innerHTML of the dragNode.

It should be noted, that doing this will also copy any id's of the nodes inside the node. So if you are using this on something that is id based, you may need to remove the id's of the nodes inside the node that is being dragged.

End Drag Event

In this event, we will reset some of the styles set in the drag:start event.

Drag Event

In this event, we will track the up/down movement for later use.

Over Drop Event

In this event, know which Target we are over, so we add the Drag node to the list either above or below the current Drop Target.

Drop Hit Event

In this event, we check to see if the target that was dropped on was not an LI node. If it wasn't, then we know it was dropped on the empty space of the UL.

Full Javascript Source

Copyright © 2009 Yahoo! Inc. All rights reserved.

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