Today, all the sites using AJAX technology have implemented the Drag and Drop effects, and the Dojo framework can also be easily implemented. Compared with other frameworks, the code is less, it also provides better compatibility with browsers.
The following figure shows the effect of the home page of the 51AJAX.com website. Each module can be dragged as needed:
Screen. width * 0.7) {this. resized = true; this. width = screen. width * 0.7; this. style. cursor = 'hand'; this. alt = 'click here to open new window \ nCTRL + Mouse wheel to zoom in/out';} "onclick =" if (! This. resized) {return true;} else {window. open ('/upload/200741022625273.jpg');} "alt =" "src =" http://www.bkjia.com/uploads/allimg/131015/15401SJ9-0.jpg "onload =" if (this. width> screen. width * 0.7) {this. resized = true; this. width = screen. width * 0.7; this. alt = 'click here to open new window \ nCTRL + Mouse wheel to zoom in/out';} "border = 0>
How to implement it? The procedure is as follows. For simplicity, a Demo page of Drag and Drop is created:
Screen. width * 0.7) {this. resized = true; this. width = screen. width * 0.7; this. style. cursor = 'hand'; this. alt = 'click here to open new window \ nCTRL + Mouse wheel to zoom in/out';} "onclick =" if (! This. resized) {return true;} else {window. open ('/upload/200741022628497.jpg');} "alt =" "src =" http://www.bkjia.com/uploads/allimg/131015/15401S2N-1.jpg "onload =" if (this. width> screen. width * 0.7) {this. resized = true; this. width = screen. width * 0.7; this. alt = 'click here to open new window \ nCTRL + Mouse wheel to zoom in/out';} "border = 0>
The procedure is as follows:
1.html
To implement drag, you must first have a container, and secondly have the elements that can be dragged. Here, we set three divs as containers, whose IDs are container1, container2, and container3. Each container has a Div as a drag element, and their class is divdrag.
2. javascript code
First, add the following code to the header. js references, and then register the elements whose class is divdrag as dojo. dnd. the HtmlDragSource object registers container1, container2, and container3 as three containers, and the drag elements in the specified container can be dragged to the container. The preceding events are encapsulated into a function, add to window. onload event.
To download the complete Dojo framework, click here to download: http://download.dojotoolkit.org/release-0.3.1/dojo-0.3.1-ajax.zip.
Copy codeThe Code is as follows:
// Reference the following two dojo packages
Dojo. require ("dojo. style ");
Dojo. require ("dojo. dnd .*");
Function init (){
// Use classname to retrieve the element List and register it as dojo. dnd. HtmlDragSour
Var arr=dojo.html. getElementsByClass ('divdrag ')
For (var I = 0; I <arr. length; I ++ ){
Var parentDiv = arr [I]. parentNode. id
New dojo. dnd. HtmlDragSource (arr [I], parentDiv );
}
// Define the container
New dojo. dnd. HtmlDropTarget ("container1", ["container1", "container2", "container3"]);
New dojo. dnd. HtmlDropTarget ("container2", ["container1", "container2", "container3"]);
New dojo. dnd. HtmlDropTarget ("container3", ["container1", "container2", "container3"]);
}
// Add to window. onload event
Window. onload = function () {init ();}