Twitter iPhone pliant OnePlus 11 PS5 Disney+ Orange Livebox Windows 11

Correction Javascript drag&drop pour Firefox 2.0

10 réponses
Avatar
Stéphane
Bonjour,

Ce script marche bien sous IE , mais "freeze" sous Firefox. Quelqu'un aurait
une petite modif à faire pour le faire fonctionner sous Firefox svp? Merci.

-----------------------------------------------------
// iMouseDown represents the current mouse button state: up or down
/*
lMouseState represents the previous mouse button state so that we can
check for button clicks and button releases:

if(iMouseDown && !lMouseState) // button just clicked!
if(!iMouseDown && lMouseState) // button just released!
*/
var mouseOffset = null;
var iMouseDown = false;
var lMouseState = false;
var dragObject = null;

var DragDrops = [];
var curTarget = null;
var lastTarget = null;
var lastCont = null;
var dragHelper = null;
var tempDiv = null;
var rootParent = null;
var rootSibling = null;

Number.prototype.NaN0=function(){return isNaN(this)?0:this;}

function CreateDragContainer(){
/*
Create a new "Container Instance" so that items from one "Set" can not
be dragged into items from another "Set"
*/
var cDrag = DragDrops.length;
DragDrops[cDrag] = [];

/*
Each item passed to this function should be a "container". Store each
of these items in our current container
*/
for(var i=0; i<arguments.length; i++){
var cObj = arguments[i];
DragDrops[cDrag].push(cObj);
cObj.setAttribute('DropObj', cDrag);

/*
Every top level item in these containers should be draggable. Do this
by setting the DragObj attribute on each item and then later checking
this attribute in the mouseMove function
*/
for(var j=0; j<cObj.childNodes.length; j++){

// Firefox puts in lots of #text nodes...skip these
if(cObj.childNodes[j].nodeName=='#text') continue;

cObj.childNodes[j].setAttribute('DragObj', cDrag);
}
}
}

function getPosition(e){
var left = 0;
var top = 0;
while (e.offsetParent){
left += e.offsetLeft +
(e.currentStyle?(parseInt(e.currentStyle.borderLeftWidth)).NaN0():0);
top += e.offsetTop +
(e.currentStyle?(parseInt(e.currentStyle.borderTopWidth)).NaN0():0);
e = e.offsetParent;
}


left += e.offsetLeft +
(e.currentStyle?(parseInt(e.currentStyle.borderLeftWidth)).NaN0():0);
top += e.offsetTop +
(e.currentStyle?(parseInt(e.currentStyle.borderTopWidth)).NaN0():0);

return {x:left, y:top};

}

function mouseCoords(ev){
if(ev.pageX || ev.pageY){
return {x:ev.pageX, y:ev.pageY};
}
return {
x:ev.clientX + document.body.scrollLeft - document.body.clientLeft,
y:ev.clientY + document.body.scrollTop - document.body.clientTop
};
}

function writeHistory(object, message){
if(!object || !object.parentNode || !object.parentNode.getAttribute)
return;
var historyDivName = object.parentNode.getAttribute('history');
if(historyDivName){
var historyDiv = document.getElementById(historyDivName);
if (historyDiv) {
historyDiv.appendChild(document.createTextNode(object.id+': '+message));
historyDiv.appendChild(document.createElement('BR'));

historyDiv.scrollTop += 50;
}
}
}

function getMouseOffset(target, ev){
ev = ev || window.event;

var docPos = getPosition(target);
var mousePos = mouseCoords(ev);
var contX;
var contY;

if (target.parentElement) {
contX = target.parentElement.scrollLeft;
contY = target.parentElement.scrollTop;
} else {
contX = 0;
contY = 0;
}
return {x:mousePos.x - docPos.x + contX, y:mousePos.y - docPos.y + contY};
}

function mouseMove(ev){
ev = ev || window.event;

/*
We are setting target to whatever item the mouse is currently on

Firefox uses event.target here, MSIE uses event.srcElement
*/
var target = ev.target || ev.srcElement;
var mousePos = mouseCoords(ev);


// mouseOut event - fires if the item the mouse is on has changed
if(lastTarget && (target!=lastTarget)){
writeHistory(lastTarget, 'Mouse Out Fired');

// reset the classname for the target element
var origClass = lastTarget.getAttribute('origClass');
if(origClass) {
lastTarget.className = origClass;
if (lastTarget.parentElement && lastTarget.parentElement.id !=
"DragContainer1" && lastTarget.filters[0]){
lastTarget.filters[0].opacity = 100;
}
}

}

/*
dragObj is the grouping our item is in (set from the createDragContainer
function).
if the item is not in a grouping we ignore it since it can't be dragged
with this
script.
*/
var dragObj = target.getAttribute('DragObj');

// if the mouse was moved over an element that is draggable
if(dragObj!=null){

// mouseOver event - Change the item's class if necessary
if(target!=lastTarget){
writeHistory(target, 'Mouse Over Fired');

var oClass = target.getAttribute('overClass');
if(oClass){
target.setAttribute('origClass', target.className);
target.className = oClass;
}
}

// if the user is just starting to drag the element
if(iMouseDown && !lMouseState){
writeHistory(target, 'Start Dragging');

// mouseDown target
curTarget = target;

// Record the mouse x and y offset for the element
rootParent = curTarget.parentNode;
rootSibling = curTarget.nextSibling;

mouseOffset = getMouseOffset(target, ev);

// We remove anything that is in our dragHelper DIV so we can put a new
item in it.
for(var i=0; i<dragHelper.childNodes.length; i++)
dragHelper.removeChild(dragHelper.childNodes[i]);

// Make a copy of the current item and put it in our drag helper.
dragHelper.appendChild(curTarget.cloneNode(true));
dragHelper.style.display = 'block';

// set the class on our helper DIV if necessary
var dragClass = curTarget.getAttribute('dragClass');
if(dragClass){
dragHelper.firstChild.className = dragClass;
}

// disable dragging from our helper DIV (it's already being dragged)
dragHelper.firstChild.removeAttribute('DragObj');

/*
Record the current position of all drag/drop targets related
to the element. We do this here so that we do not have to do
it on the general mouse move event which fires when the mouse
moves even 1 pixel. If we don't do this here the script
would run much slower.
*/
var dragConts = DragDrops[dragObj];

/*
first record the width/height of our drag item. Then hide it since
it is going to (potentially) be moved out of its parent.
*/
curTarget.setAttribute('startWidth', parseInt(curTarget.offsetWidth));
curTarget.setAttribute('startHeight', parseInt(curTarget.offsetHeight));
curTarget.style.display = 'none';

// loop through each possible drop container
for(var i=0; i<dragConts.length; i++){
with(dragConts[i]){
var pos = getPosition(dragConts[i]);

/*
save the width, height and position of each container.

Even though we are saving the width and height of each
container back to the container this is much faster because
we are saving the number and do not have to run through
any calculations again. Also, offsetHeight and offsetWidth
are both fairly slow. You would never normally notice any
performance hit from these two functions but our code is
going to be running hundreds of times each second so every
little bit helps!

Note that the biggest performance gain here, by far, comes
from not having to run through the getPosition function
hundreds of times.
*/
setAttribute('startWidth', parseInt(offsetWidth));
setAttribute('startHeight', parseInt(offsetHeight));
setAttribute('startLeft', pos.x);
setAttribute('startTop', pos.y);
}

// loop through each child element of each container
for(var j=0; j<dragConts[i].childNodes.length; j++){
with(dragConts[i].childNodes[j]){
if((nodeName=='#text') || (dragConts[i].childNodes[j]==curTarget))
continue;

var pos = getPosition(dragConts[i].childNodes[j]);

// save the width, height and position of each element
setAttribute('startWidth', parseInt(offsetWidth));
setAttribute('startHeight', parseInt(offsetHeight));
setAttribute('startLeft', pos.x);
setAttribute('startTop', pos.y);
}
}
}
}
}

// If we get in here we are dragging something
if(curTarget){
// move our helper div to wherever the mouse is (adjusted by mouseOffset)
dragHelper.style.top = mousePos.y - mouseOffset.y;
dragHelper.style.left = mousePos.x - mouseOffset.x;

var activeCont = null;

var dragConts = DragDrops[curTarget.getAttribute('DragObj')];


var xPos = mousePos.x - mouseOffset.x +
(parseInt(curTarget.getAttribute('startWidth')) /2);
var yPos = mousePos.y - mouseOffset.y +
(parseInt(curTarget.getAttribute('startHeight'))/2);

// check each drop container to see if our target object is "inside" the
container
for(var i=0; i<dragConts.length; i++){
with(dragConts[i]){
if((parseInt(getAttribute('startLeft'))
< xPos) &&
(parseInt(getAttribute('startTop'))
< yPos) &&
((parseInt(getAttribute('startLeft')) +
parseInt(getAttribute('startWidth'))) > xPos) &&
((parseInt(getAttribute('startTop')) +
parseInt(getAttribute('startHeight'))) > yPos)){

/*
our target is inside of our container so save the container into
the activeCont variable and then exit the loop since we no longer
need to check the rest of the containers
*/
activeCont = dragConts[i];

// exit the for loop
break;
}
}
}

// Our target object is in one of our containers. Check to see where our
div belongs
if(activeCont){
if(activeCont != curTarget.parentNode){
writeHistory(curTarget, 'Moved into '+activeCont.id);

var oContClass = activeCont.getAttribute('overClass');
if(oContClass){
activeCont.setAttribute('origClass', activeCont.className);
activeCont.className = oContClass;
}

if (lastCont) {
// reset the classname for the container element
var origContClass = lastCont.getAttribute('origClass');
if(origContClass)
lastCont.className = origContClass;
}
lastCont = activeCont;
}

// beforeNode will hold the first node AFTER where our div belongs
var beforeNode = null;

// loop through each child node (skipping text nodes).
for(var i=activeCont.childNodes.length-1; i>=0; i--){
with(activeCont.childNodes[i]){
if(nodeName=='#text') continue;

// if the current item is "After" the item being dragged
if(curTarget != activeCont.childNodes[i]
&&
((parseInt(getAttribute('startLeft')) +
parseInt(getAttribute('startWidth'))) > xPos) &&
((parseInt(getAttribute('startTop')) +
parseInt(getAttribute('startHeight'))) > yPos)){
beforeNode = activeCont.childNodes[i];
}
}
}

// the item being dragged belongs before another item
if(beforeNode){
if(beforeNode!=curTarget.nextSibling){
writeHistory(curTarget, 'Inserted Before '+beforeNode.id);

activeCont.insertBefore(curTarget, beforeNode);
}

// the item being dragged belongs at the end of the current container
} else {
if((curTarget.nextSibling) || (curTarget.parentNode!=activeCont)){
writeHistory(curTarget, 'Inserted at end of '+activeCont.id);

activeCont.appendChild(curTarget);
}
}

// the timeout is here because the container doesn't "immediately" resize
setTimeout(function(){
var contPos = getPosition(activeCont);
activeCont.setAttribute('startWidth', parseInt(activeCont.offsetWidth));
activeCont.setAttribute('startHeight',
parseInt(activeCont.offsetHeight));
activeCont.setAttribute('startLeft', contPos.x);
activeCont.setAttribute('startTop', contPos.y);}, 5);

// make our drag item visible
if(curTarget.style.display!=''){
writeHistory(curTarget, 'Made Visible');
curTarget.style.display = '';
curTarget.style.visibility = 'hidden';
}
} else {

// our drag item is not in a container, so hide it.
if(curTarget.style.display!='none'){
writeHistory(curTarget, 'Hidden');
curTarget.style.display = 'none';
}
}
}

// track the current mouse state so we can compare against it next time
lMouseState = iMouseDown;

// mouseMove target
lastTarget = target;


if(dragObject){
dragObject.style.position = 'absolute';
dragObject.style.top = mousePos.y - mouseOffset.y;
dragObject.style.left = mousePos.x - mouseOffset.x;
}

// track the current mouse state so we can compare against it next time
lMouseState = iMouseDown;

// this prevents items on the page from being highlighted while dragging
if(curTarget || dragObject) return false;
}

function mouseUp(ev){

if(curTarget){
writeHistory(curTarget, 'Mouse Up Fired');

dragHelper.style.display = 'none';
if(curTarget.style.display == 'none'){
if(rootSibling){
rootParent.insertBefore(curTarget, rootSibling);
} else {
rootParent.appendChild(curTarget);
}
}
curTarget.style.display = '';
curTarget.style.visibility = 'visible';

if (lastCont) {
var origContClass = lastCont.getAttribute('origClass');
if(origContClass)
lastCont.className = origContClass;

if (lastCont.id != "DragContainer1" && curTarget.filters[0])
curTarget.filters[0].opacity = 100;
else if (lastCont.id == "DragContainer1" && curTarget.filters[0])
curTarget.filters[0].opacity = 50;

lastCont = null;
}
}
curTarget = null;

dragObject = null;

iMouseDown = false;
}

function mouseDown(ev){
ev = ev || window.event;
var target = ev.target || ev.srcElement;

iMouseDown = true;
if(lastTarget){
writeHistory(lastTarget, 'Mouse Down Fired');
}

if(target.onmousedown || target.getAttribute('DragObj')){
return false;
}
}

document.onmousemove = mouseMove;
document.onmousedown = mouseDown;
document.onmouseup = mouseUp;

window.onload = function(){
CreateDragContainer(document.getElementById('DragContainer1'),
document.getElementById('DragContainer2'),
document.getElementById('DragContainer3'),
document.getElementById('DragContainer4'));

dragHelper = document.createElement('DIV');
dragHelper.style.cssText = 'position:absolute;display:none;';

document.body.appendChild(dragHelper);
}

10 réponses

Avatar
SAM
Bonjour,

Ce script marche bien sous IE , mais "freeze" sous Firefox. Quelqu'un aurait
une petite modif à faire pour le faire fonctionner sous Firefox svp? Merci.


Pourquoi ré-inventer la roue ?
<http://www.walterzorn.com/dragdrop/dragdrop_e.htm>

--
sm

Avatar
czar
quelqu'un peut me passer un exemple? merci infiniment
Avatar
SAM
czar a écrit :
quelqu'un peut me passer un exemple? merci infiniment



plus d'accès au fil ...

<http://www.walterzorn.com/dragdrop/dragdrop_e.htm>

--
sm
Avatar
czar
mais ça ne fonctionne pas avec un <div> normale du genre tableau... que des images...
Avatar
SAM
czar a écrit :
mais ça ne fonctionne pas avec un <div> normale du genre tableau... que des
images...



D'abord je n'appelle pas un 'table' un div "normal"

ensuite : Tu es sûr et certain ?

Je vois écrit : Drag & Drop for Images and *Layers*
^^^^^^

Aurais-je rêvé quand j'ai bougé les pavés de texte ?


--
sm
Avatar
czar
^^ oui tu as raison, mais ( je te gene par mes questions ) j'ai besoin de faire un drag & drop & copy ( clonage ) est ce possible? ( le cas d'un table bien sure )
Avatar
SAM
czar a écrit :
^^ oui tu as raison, mais ( je te gene par mes questions )



Non, juste le D&D ne m'intéresse pas
Je sais qu'il y a cette bibli pour le jour où ça m'intéressera.
Et que ce n'est pas la peine de ré-inventer la roue.

j'ai besoin de faire
un drag & drop & copy ( clonage ) est ce possible? ( le cas d'un table bien sure
)



Je ne vois pas pourquoi ce ne serait pas possible.

Sauf que ... je ne comprends pas ce que tu dis (à quoi ça sert?).
en fait, on clique sur l'originel (mousedown) ça en fait un clone
qu'on promène jusque là ou on veut le lâcher, c'est bien ça ?

Yaka inclure le clonage dans la fonction de glissé-déposé au moment du
cliquage (onmousedown) sur le table originel, me semble-ce
puis lâcher le clic (onmouseup) à la position où on veut le clone
ou qque chose du genre, non ?
(à la limite, même pas besoin de bibli, la seule grosse difficulté si on
accepte de ne pas faire de glissé est de savoir où est le pointeur du
mulot quand on lâche le clic)
<http://www.quirksmode.org/js/findpos.html>

Finalement ... à l'URL proposée le clic-copy-drag-drop est bien prévu.
L'exemple est pour une image, il n'y a pas de raison que ça ne
fonctionne pas pour un div.
voir :
<http://www.walterzorn.com/dragdrop/commands_e.htm>
dont le 1er : clone
le 2ième : copy (combien de clones ?)
etc ...
et :
<http://www.walterzorn.com/dragdrop/api_e.htm>


Désolé, tous ces trucs sont en pas français.

Et pas mal de français qui font des tutos le font en anglais :-(
(histoire d'être vu par le + grand nombre ? et tant pis pour la
francophonie)

--
sm
Avatar
czar
merci en tt cas, j'ai deja essayé ca avant mais ... bref chiui entrain d'implementer un editeur d'UML par javascript, par dhtml disant... donc le truc de tab sert a simuler les classes... g deja fé tt ça, clonage et tt, mais le probleme c que les attribus doive etre des lien hypertexte... donc je dois changer de principe de travail :/ tu vois ce que je veux dire?
Avatar
SAM
czar a écrit :
merci en tt cas, j'ai deja essayé ca avant mais ... bref chiui entrain
d'implementer un editeur d'UML par javascript, par dhtml disant... donc le truc
de tab sert a simuler les classes... g deja fé tt ça, clonage et tt, mais le
probleme c que les attribus doive etre des lien hypertexte... donc je dois
changer de principe de travail :/ tu vois ce que je veux dire?




avec un peu de chance, en style normal, ou tout au moins avec les mots
importants entiers peut-être ta prose pourrait être comprise ?

tu parl m1tenan 2 "tab" ... c koi ?


--
sm
Avatar
czar
tableau... ( ca te dérange de parlé en priver? puis je posterai la solution! )