Code for the drag-and-drop IFRAME Effect of JavaScript

Source: Internet
Author: User

// History
//------------------------------------------------------------------
// Jan 23,200 4: fixed problems which caused the script not to work in
// Some framed situations. Improved browser support.
// Added easier "addhandle" implentation.
// May 25,200 3: added Better event position detection, added caching
// Of IFRAME object references to avoid lookups. Added
// 'Move 'cursor to handles.
// May 24,200 3: Updated to fix bug with Netscape 7.x
// May 23,200 3: created
/*
Description: The purpose of this library is to allow IFRAME objects to be
Dragged around the screen in the same way that popup windows or draggable
Div tags are often used. Since IFRAME objects always cover form objects,
This makes an ideal solution for a simulated "popup window" on a page
Form objects.

Compatability: Tested successfully with IE 6.x, Netscape 6.2, 7.x, and
Mozilla 1.3. Since this script uses IFRAME objects and DHTML heavily,
Cross-browser compatability is a goal but there may be some quirks in
Various browser versions.

Usage:
1) include the source file in your main document which contains the IFRAME
Tags. Make sure each IFRAME has a unique "ID" attribute. For Best Browser
Compatability, also include a "name" attribute in the IFRAME tag that
Has the same value as the "ID" attribute.

2) In the document content of each IFRAME which will be draggable, do two
Things:
A) include the dragiframe. js file in the source
B) add this code to the <body> tag:
Onload = "addhandle (document. getelementbyid ('toolbar'), window );"
Where 'toolbar' is the ID of an element on the page which shocould be the 'handle'
By which the IFRAME shoshould be dragged (like a toolbar at the top ).
If you want the IFRAME to be draggable by clicking anywhere in the IFRAME
Document, use:
Onload = "addhandle (document. getelementsbytagname ('body'). Item (0), window );"

Note: The Code will automatically look up the window. parent tree to find
Parent document with draggable iframes enabled. It will attach itself to
First Document it finds, allowing it to work within a framed environment.

In your parent document (containing the iframes), you may set a couple of options:

// Set to true to always bring the selected IFRAME to the "TOP" of the zindex.
// Defaults to false
Bringselectediframetotop (true | false );

// Set to true to allow IFRAME objects to be dragged off the screen. This may
// Make the handle be no longer reachable by the mouse, causing the IFRAME
// Be stranded.
// Defaults to false
Allowdragoffscreen (true | false );

// You may manually set this variable to define the highest zindex used in
// Your main document. This is used to determine what zindex to give an IFRAME
// If it is selected and "bringselectediframetotop" is set to true.
// Defaults to 99.
Dif_highestzindex = 4;

Notes:
1) If you have defined onmousedown or onmouseup Event Handlers for your "handle"
object in the IFRAME, they will be over-written.
2) If you have defined an onmousemove handler in either the main document or
the IFRAME document, they will be over-written.
3) All objects must have an ID! &lt;Br&gt; */&lt;br&gt; // IFRAME attribute reference: http://www.phpx.com/man/dhtmlcn/objects/IFRAME.html &lt;br&gt; // http://www.mattkruse.com/javascript/dragiframe/ &lt;br&gt; // variables used for "draggable iframe" (DIF) functions &lt;br&gt; var dif_dragging = false; &lt;br&gt; var dif_iframebeingdragged = ""; &lt;br&gt; var dif_iframeobjects = new object (); &lt;br&gt; var dif_iframewindows = new object (); &lt;br&gt; var dif_iframemousedownleft = new object (); &lt;br&gt; var dif_iframemousedowntop = new object (); &lt;br&gt; var dif_pagemousedownleft = new object (); &lt;br&gt; var dif_pagemousedowntop = new object (); &lt;br&gt; var dif_handles = new object (); &lt;br&gt; var dif_highestzindex = 99; &lt;br&gt; var dif_raiseselectediframe = false; &lt;br&gt; var dif_allowdragoffscreen = false; &lt;/P&gt;

// Set to true to always raise the dragged IFRAME to top zindex
Function bringselectediframetotop (VAL ){
Dif_raiseselectediframe = val;
}

// Set to try to allow iframes to be dragged off the top/left of the document
Function allowdragoffscreen (VAL ){
Dif_allowdragoffscreen = val;
}

// Method to be used by IFRAME content document to specify what object can be draggable in the window
Function addhandle (O, Win ){
If (arguments. Length = 2 & Win = Window ){
// JS is wrongly ded In the IFRAME who has a handle, search up the chain to find a parent window that this one is dragged in
VaR P = win;
While (P = P. Parent ){
If (P. addhandle) {P. addhandle (O, win, true); return ;}
If (P = win. Top) {return;} // already reached the top, stop looking
}
Return; // if it reaches here, there is no parent with the addhandle function defined, so this frame can't be dragged!
}
VaR topref = win;
VaR toprefstr = "window ";
While (topref. Parent & topref. parent! = Window ){
Topref = topref. parent;
Toprefstr = toprefstr + ". Parent ";
}
// Add handlers to Child Window
If (typeof (win. dif_mainhandlersadded) = "undefined" |! Win. dif_mainhandlersadded ){
// This is done in a funky way to make Netscape happy
With (WIN ){
Eval ("function onmousedownhandler (EVT) {If (typeof (EVT) = 'undefined') {EVT = event;}" + toprefstr + ". parent. dif_begindrag (EVT, "+ toprefstr + ")}");
Eval ("document. onmousedown = onmousedownhandler ;");
Eval ("function onmouseuphandler (EVT) {If (typeof (EVT) = 'undefined') {EVT = event;}" + toprefstr + ". parent. dif_enddrag (EVT, "+ toprefstr + ")}");
Eval ("document. onmouseup = onmouseuphandler ;");
Eval ("function onmousemovehandler (EVT) {If (typeof (EVT) = 'undefined') {EVT = event;}" + toprefstr + ". parent. dif_iframemove (EVT, "+ toprefstr + ")}");
Eval ("document. onmousemove = onmousemovehandler ;");
Win. dif_handlersadded = true;
Win. dif_mainhandlersadded = true;
}
}
// Add handler to this window
If (typeof (window. dif_handlersadded )! = "Undefined" |! Window. dif_handlersadded ){
Eval ("function onmousemovehandler (EVT) {If (typeof (EVT) = 'undefined') {EVT = event;} dif_mousemove (EVT, window )}");
Eval ("document. onmousemove = onmousemovehandler ;");
Window. dif_handlersadded = true;
}
O. style. cursor = "";
VaR name = dif_getiframeid (topref );
If (dif_handles [name] = NULL ){
// Initialize relative positions for mouse down events
Dif_handles [name] = new array ();
Dif_iframemousedownleft [name] = 0;
Dif_iframemousedowntop [name] = 0;
Dif_pagemousedownleft [name] = 0;
Dif_pagemousedowntop [name] = 0;
}
Dif_handles [name] [dif_handles [name]. Length] = O;
}

// generalized function to get position of an event (like mousedown, mousemove, etc)
function dif_geteventposition (EVT) {
var Pos = new object ();
POS. X = 0;
POS. y = 0;
If (! EVT) {
EVT = Window. event;
}< br> If (typeof (EVT. pagex) = 'number') {
POS. X = EVT. pagex;
POS. y = EVT. pagey;
}< br> else {
POS. X = EVT. clientx;
POS. y = EVT. clienty;
If (! Top. Opera) {
If ((! Invalid parameter Doc ument. compatmode) | (window.doc ument. compatmode = 'background') {
POS. X + = too many Doc ument. body. scrollleft;
POS. Y + = too many Doc ument. body. scrolltop;
}< br> else {
POS. X + = registry.document.doc umentelement. scrollleft;
POS. Y + = policy.document.doc umentelement. scrolltop;
}< BR >}< br> return Pos;
}

// gets the ID of a frame given a reference to a window object.
// also stores a reference to the IFRAME object and it's window object
function dif_getiframeid (WIN) {
// loop through the window's IFRAME objects looking for a matching window object
var iframes = document. getelementsbytagname ("iframe");
for (VAR I = 0; I var o = iframes. item (I);
var W = NULL;
If (O. contentWindow) {
// For ie5.5 and IE6
W = O. contentWindow;
}< br> else if (window. frames & window. frames [O. id]. window) {
W = Window. frames [O. id];
}< br> If (W = Win) {
dif_iframewindows [O. id] = win;
dif_iframeobjects [O. id] = O;
return O. ID;
}< BR >}< br> return NULL;
}

// Gets the page X, Y coordinates of the IFRAME (or any object)
Function dif_getobjectxy (o ){
VaR res = new object ();
Res. x = 0; res. Y = 0;
If (o! = NULL ){
Res. x = O. style. Left. substring (0, O. style. Left. indexof ("PX "));
Res. Y = O. style. Top. substring (0, O. style. Top. indexof ("PX "));
}
Return res;
}

// Function to get the SRC element clicked for non-ie browsers
Function getsrcelement (e ){
VaR TGT = e.tar get;
While (TGT. nodetype! = 1) {TGT = TGT. parentnode ;}
Return TGT;
}

// Check if object clicked is a 'handle'-walk up the node tree if required
Function ishandleclicked (handle, objectclicked ){
If (handle = objectclicked) {return true ;}
While (objectclicked. parentnode! = NULL ){
If (objectclicked = handle ){
Return true;
}
Objectclicked = objectclicked. parentnode;
}
Return false;
}

// Called when user clicks an IFRAME that has a handle in it to begin dragging
Function dif_begindrag (E, Win ){
// Get the iframe id that was clicked on
VaR iframename = dif_getiframeid (WIN );
If (iframename = NULL) {return ;}
// Make sure that this IFRAME has a handle and that the handle was clicked
If (dif_handles [iframename] = NULL | dif_handles [iframename]. Length <1 ){
Return;
}
VaR ishandle = false;
VaR T = E. srcelement | getsrcelement (E );
For (VAR I = 0; I <dif_handles [iframename]. length; I ++ ){
If (ishandleclicked (dif_handles [iframename] [I], t )){
Ishandle = true;
Break;
}
}
If (! Ishandle) {return false ;}
Dif_iframebeingdragged = iframename;
If (dif_raiseselectediframe ){
Dif_iframeobjects [dif_iframebeingdragged]. style. zindex = dif_highestzindex ++;
}
Dif_dragging = true;
VaR Pos = dif_geteventposition (E );
Dif_iframemousedownleft [dif_iframebeingdragged] = pos. X;
Dif_iframemousedowntop [dif_iframebeingdragged] = pos. Y;
VaR o = dif_getobjectxy (dif_iframeobjects [dif_iframebeingdragged]);
Dif_pagemousedownleft [dif_iframebeingdragged] = O. X-0 + pos. X;
Dif_pagemousedowntop [dif_iframebeingdragged] = O. Y-0 + pos. Y;
}

// Called when mouse button is released after dragging an IFRAME
Function dif_enddrag (e ){
Dif_dragging = false;
Dif_iframebeingdragged = "";
}

// Called when mouse moves in the Main Window
Function dif_mousemove (e ){
If (dif_dragging ){
VaR Pos = dif_geteventposition (E );
Dif_drag (Pos. X-dif_pagemousedownleft [dif_iframebeingdragged], POS. Y-dif_pagemousedowntop [dif_iframebeingdragged]);
}
}

// Called when mouse moves in the IFRAME window
Function dif_iframemove (e ){
If (dif_dragging ){
VaR Pos = dif_geteventposition (E );
Dif_drag (Pos. X-dif_iframemousedownleft [dif_iframebeingdragged], POS. Y-dif_iframemousedowntop [dif_iframebeingdragged]);
}
}

// function which actually moves of the IFRAME object on the screen
function dif_drag (x, y) {
var o = dif_getobjectxy (dif_iframeobjects [dif_iframebeingdragged]);
// don't drag it off the top or left of the screen?
var newpositionx = O. x-0 + X;
var newpositiony = O. y-0 + Y;
If (! Dif_allowdragoffscreen) {
If (newpositionx <0) {newpositionx = 0 ;}< br> If (newpositiony <0) {newpositiony = 0 ;}
}< br> dif_iframeobjects [dif_iframebeingdragged]. style. left = newpositionx + "PX";
dif_iframeobjects [dif_iframebeingdragged]. style. top = newpositiony + "PX";
dif_pagemousedownleft [dif_iframebeingdragged] + = x;
dif_pagemousedowntop [dif_iframebeingdragged] + = y;
}< br> Online Demo http://img.jb51.net/online/IFRAMEWindows/index.html
package and download iframewindows.rar

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.