Learn about YUI. Ext on the 5th day -- drag and drop Darg & Drop_YUI.Ext

Source: Internet
Author: User
Learn YUI. Ext 5th day -- drag and Drop Darg & amp; Drop to drag and Drop an element Darg & Drop is an important feature of windows. How can we implement it in a browser? Let's take a look at the basic example:

The Code is as follows:


YAHOO. example. DDApp = function (){
Var dd;
Return {
Init2: function (){

// Var dropzone = ["dz"];
// For (I in dropzone ){
// New YAHOO. util. DDTarget (dropzone [I]);
//};
Var draggable = ["dd_1", "dd_2", "dd_3"]; // array stores the ID of DargDrop
Draggable = function (id, sGroup ){
// Create a DragDrop object. This must be called by the subclass of YAHOO. util. DragDrop.
// Sets up the DragDrop object. Must be called in the constructor of any YAHOO. util. DragDrop subclass
This. init (id, sGroup );
}
Draggable. prototype = new YAHOO. util. DD (); // inherits the parent class
Draggable. prototype. startDrag = function (x, y ){
YAHOO. util. Dom. setStyle (this. getEl (), "opacity", 0.5 );
}
Draggable. prototype. endDrag = function (e) {// return the origin point after dragging
Var draggable = this. getEl ();
YAHOO. util. Dom. setStyle (draggable, "ops", 1.0 );
Draggable. style. left = "0px ";
Draggable. style. top = "0px ";
}
For (I in draggable ){
New Draggable (draggable [I]);
}

}
}
}();

Note:

1. here we use an array to collect all the elements that require DD (Darg & Drop, the same below) first, and then use for to traverse new YAHOO. util. DD () object, "Bundled" into a class of objects with the same attributes: Draggable.

2. A problem that cannot be started:
Use YUI for Dragdrop. If clearType is enabled in your system, the font will hair when it is moved. It is estimated that the internal render of ie will be faulty. It was originally intended to be replaced by DDProxy, but it could not be inherited once it was used.

3. manually add the xhtml holder.


Okay, this example is coming to an end. Let's take a look at some interesting (demonstration ):


The Code is as follows:


Var correct = {opt0: "ans1", opt1: "ans2", opt2: "ans0"} // correct answer
Var answer = {opt0: "tmp0", opt1: "tmp1", opt2: "tmp2"} // answer

// Vertex
Function mark (event)
{
Var points = 0 ;//
Var max = 3 ;//

For (key in correct ){
Points + = correct [key] = answer [key]? 1: 0;
}
Var score = Math. floor (points/max * 100 );
Var result = document. getElementById ("result ");
Result. innerHTML = (score> 70? "Qualified": "unqualified") + ":" + score + "% ";
}

// Initialization
Function init (event)
{

Var dropzone = ["ans0", "ans1", "ans2", // Answer bar
"Tmp0", "tmp1", "tmp2"]; // temporary location (the place where the flag is started)
For (id in dropzone ){
New YAHOO. util. DDTarget (dropzone [id]);
}


Var draggable = ["opt0", "opt1", "opt2"]; // optional (FLAG)

Draggable = function (id, sGroup ){
This. init (id, sGroup );
}

Draggable. prototype = new YAHOO. util. DD ();

Draggable. prototype. canAccept = function (draggable, dropzone ){
If (dropzone. id. match (/^ opt [012] $ /)){
Return false;
}
For (key in answer ){
If (answer [key] = dropzone. id ){
Return false;
}
}
Return true;
}

Draggable. prototype. startDrag = function (x, y ){
YAHOO. util. Dom. setStyle (this. getEl (), "opacity", 0.5 );
}

Draggable. prototype. onDragEnter = function (e, id ){
Var dropzone = YAHOO. util. DDM. getElement (id );
Var draggable = this. getEl ();
If (this. canAccept (draggable, dropzone )){
Dropzone. style. backgroundColor = "orange ";
}
}

Draggable. prototype. onDragOut = function (e, id ){
Var dropzone = YAHOO. util. DDM. getElement (id );
Dropzone. style. backgroundColor = "white ";
}

Draggable. prototype. onDragDrop = function (e, id ){
Var dropzone = YAHOO. util. DDM. getElement (id );
Var draggable = this. getEl ();
If (this. canAccept (draggable, dropzone )){
Dropzone. style. backgroundColor = "white ";
Dropzone. appendChild (draggable );
Answer [draggable. id] = dropzone. id; // answers the updates
}
}

Draggable. prototype. endDrag = function (e ){
Var draggable = this. getEl ();
YAHOO. util. Dom. setStyle (draggable, "ops", 1.0 );
Draggable. style. left = "0px ";
Draggable. style. top = "0px ";
}

For (id in draggable ){
New Draggable (draggable [id]);
}

// Bind a button to trigger the event and calculate the score
YAHOO. util. Event. addListener ("submit", "click", mark );
}

YAHOO. util. Event. addListener (window, "load", init );


It is very long and annoying to post xhtml again. Look DEMO.

Okay. Today, it's in serious ot. Let's talk about it later.
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.