MooTools provides a tremendous feature that helps you add drag and
drop drag functionalities to your web page elements. We can do this by
creating our own new Drag.Move object. Using this object, you can
define your options and events. Drag and Drag.Move classes are from the
MooTools More library.
Let us discuss the options and events of Drag.Move object.
The following are the supported events −
Syntax
Syntax
Syntax
Syntax
Syntax
Syntax
Example
Output
Let us discuss the options and events of Drag.Move object.
Drag.Move
Drag.Move is an Object used to add Drag and Drop feature to the html elements. Drag.Move extends Drag, so we can use all the Options and Events of Drag class by Drag.Move object. Take a look at the following syntax and understand how to use Drag.Move object.Syntax
var myDrag = new Drag.Move(dragElement, { // Drag.Move Options droppables: dropElement, container: dragContainer, // Drag Options handle: dragHandle, // Drag.Move Events // the Drag.Move events pass the dragged element, // and the dropped into droppable element onDrop: function(el, dr) { //will alert the id of the dropped into droppable element alert(dr.get('id')); }, // Drag Events // Drag events pass the dragged element onComplete: function(el) { alert(el.get('id')); } });
Drag.Move Options
Drag.Move provides the following options to maintain html elements with Drag and Drop features −- droppable − This helps you set the selector of droppable elements (the elements that register on drop-related events).
- container − This helps you set the drag element’s container (keeps the element inside).
- snap − This helps you set how many px the user must drag the cursor before the draggable element starts dragging. The default is 6, and you can set it to any number of variable representing a number.
- handle − This helps you add a handle to your draggable element. Handle becomes the only element that will accept the grab.
Syntax
//here we define a single element by id var dragElement = $('drag_element'); //here we define an array of elements by class var dropElements = $$('.drag_element'); var dragContainer = $('drag_container'); var dragHandle = $('drag_handle'); //now we set up our Drag.Move object var myDrag = new Drag.Move(dragElement , { // Drag.Move Options // set up our droppables element with the droppables var we defined above droppables: dropElements , // set up our container element with the container element var container: dragContainer // set up pixels the user must drag. Snap: 10 // Adds a handle to your draggable element handle: dragHandle });
Drag.Move events
Drag.Move events provide different functions that can be used in different levels of the action. For example, when you start to drag or drop an object, each Drag.Move event will pass the dragged element or the dropped element as parameters.The following are the supported events −
onStart()
This raises an event on the start of drag. If you set a long snap, then this event would not raise until the mouse is at a distance. Take a look at the following syntax.Syntax
var myDrag = new Drag.Move(dragElement , { // Drag options will pass the dragged element as a parameter onStart: function(el) { // put whatever you want to happen on start in here } });
onDrag()
This raises an event continuously while you are dragging an element. Take a look at the following syntax.Syntax
var myDrag = new Drag.Move(dragElement , { // Drag options will pass the dragged element as a parameter onDrag: function(el) { // put whatever you want to happen on drag in here } });
onDrop()
This raises an event when you drop the draggable element into a droppable element. Take a look at the following syntax.Syntax
var myDrag = new Drag.Move(dragElement , { // It will pass the draggable element ('el' in this case) // and the droppable element the draggable is interacting with ('dr' here) onDrop: function(el, dr) { // put whatever you want to happen on drop in here } });
onLeave()
This raises an event when a draggable element leaves a droppable element’s bounds. Take a look at the following syntax.Syntax
var myDrag = new Drag.Move(dragElement , { // It will pass the draggable element ('el' in this case) // and the droppable element the draggable is interacting with ('dr' here) onLeave: function(el, dr) { // put whatever you want to happen on Leave from droppable area in here } });
onEnter()
This raises when a draggable element enters a droppable element area. Take a look at the following syntax.Syntax
var myDrag = new Drag.Move(dragElement , { // It will pass the draggable element ('el' in this case) // and the droppable element the draggable is interacting with ('dr' here) onEnter: function(el, dr) { // this will fire when a draggable enters a droppable element } });
onComplete()
This raises an event. onComplete refers to when you drop a droppable, and it will raise whether or not you land in a droppable. Take a look at the following syntax.Syntax
var myDrag = new Drag.Move(dragElement , { // Drag Options // Drag options will pass the dragged element as a parameter onComplete: function(el) { // put whatever you want to happen on complete } });Let us take an example that will explore all the features explained in this chapter. The features are — Drag, Drag.Move, onEnter, onLeave, onDrop, onStart, onDrag, and onComplete. In this example, we are providing one HANDLE, using that you can drag the draggable object anywhere into the container. For every action, there is a notification on the left side (indicated in blue color). There is a Droppable area in the container. If the Draggable object enters into the Droppable area, then the last three indicators are activated. Take a look at the following code.
Example
<!DOCTYPE html> <html> <head> <style> /* this is generally a good idea */ body { margin: 0; padding: 0; } /* make sure the draggable element has "position: absolute" and then top and left are set for the start position */ #drag_me { width: 100px; height: 100px; background-color: #333; position: absolute; top: 0; left: 0; } #drop_here { width: 80%; height: 200px; background-color: #eee; margin-left: 100px; margin-top: -200px !important; } /* make sure the drag container is set with position relative */ #drag_cont { background-color: #ccc; height: auto; width: 500px; position:relative; margin-top: 20px; margin-left: 20px; margin-bottom: auto; } #drag_me_handle { width: 100%; height: auto; background-color: #F5B041; } #drag_me_handle span { display: block; padding: 20px; } .indicator { width: 100px; height: auto; background-color: #0066FF; border-bottom: 1px solid #eee; } .indicator span { padding: 10px; display: block; } .draggable { width: 200px; height: 200px; background-color: blue; } </style> <script type = "text/javascript" src = "MooTools-Core-1.6.0.js"></script> <script type = "text/javascript" src = "MooTools-More-1.6.0.js"></script> <script type = "text/javascript"> window.addEvent('domready', function() { var dragElement = $('drag_me'); var dragContainer = $('drag_cont'); var dragHandle = $('drag_me_handle'); var dropElement = $$('.draggable'); var startEl = $('start'); var completeEl = $('complete'); var dragIndicatorEl = $('drag_ind'); var enterDrop = $('enter'); var leaveDrop = $('leave'); var dropDrop = $('drop_in_droppable'); var myDrag = new Drag.Move(dragElement, { // Drag.Move options droppables: dropElement, container: dragContainer, // Drag options handle: dragHandle, // Drag.Move Events onDrop: function(el, dr) { if (!dr) { }else { dropDrop.highlight('#FB911C'); //flashes orange el.highlight('#fff'); //flashes white dr.highlight('#667C4A'); //flashes green }; }, onLeave: function(el, dr) { leaveDrop.highlight('#FB911C'); //flashes orange }, onEnter: function(el, dr) { enterDrop.highlight('#FB911C'); //flashes orange }, // Drag Events onStart: function(el) { startEl.highlight('#FB911C'); //flashes orange }, onDrag: function(el) { dragIndicatorEl.highlight('#FB911C'); //flashes orange }, onComplete: function(el) { completeEl.highlight('#FB911C'); //flashes orange } }); }); </script> </head> <body> <p align = "center">Drag and Drop Application</p> <div id = "drag_cont"> <div id = "start" class = "indicator"><span>Start</span></div> <div id = "drag_ind" class = "indicator"><span>Drag</span></div> <div id = "complete" class = "indicator"><span>Complete</span></div> <div id = "enter" class = "indicator"><span>Enter Droppable Element</span></div> <div id = "leave" class = "indicator"><span>Leave Droppable Element</span></div> <div id = "drop_in_droppable" class = "indicator"> <span>Dropped in Droppable Element</span> </div> <div id = "drag_me"> <div id = "drag_me_handle"><span>HANDLE</span></div> </div> <div id = "drop_here" class = "draggable"> <p align = "center">Droppable Area</p> </div> </div> </body> </html>You will receive the following output wherein, you have to click on Handle and Drag it. You can now find the notification indications on the left hand side.
Output
No comments:
Post a Comment