Suggestion app web

Par Supa polo, il y a 10 ans


Bonjour à tous,

Je suis actuellement entrain de développer une petite application web afin de m'entrainer au développement web (HTML/CSS/JAVASCRIPT).

Je viens ici pour vous demander de l'aide et des idées pour la réalisation d'une partie.
Je vais mettre un systeme de drap and drop en place et je voudrais que l'utilisateur déplace des logo dans des petites cases séparées comme ceci :
logo | logo | logo | ...
Seulement je ne sais pas trop comment materialiser ces petites cases, au départ j'étais parti sur une succession de balise <hr> verticale, seulement je ne trouve pas ca très jolie et un peu chiant à réaliser, cependant je ferais cette technique si je ne trouve pas d'autre solutions.

C'est pour cela que je viens vers vous, vous demandez des idées de comment je pourrais realiser cela.

Merci d'avance.

4 réponses

miniapple, il y a 10 ans

Bonjour,
J'ai trouvé un site qui pourra t'aider pour ton application en matière de drag 'n drop ----> How to Drag and Drop in Javascript
Voici une retranscription du code complet:

// iMouseDown represents the current mouse button state: up or down 2 /* 3 lMouseState represents the previous mouse button state so that we can 4 check for button clicks and button releases: 5 if(iMouseDown && !lMouseState) // button just clicked! 6 if(!iMouseDown && lMouseState) // button just released! 7 */ 8 var mouseOffset = null; 9 var iMouseDown = false; 10 var lMouseState = false; 11 var dragObject = null; 12 // Demo 0 variables 13 var DragDrops = []; 14 var curTarget = null; 15 var lastTarget = null; 16 var dragHelper = null; 17 var tempDiv = null; 18 var rootParent = null; 19 var rootSibling = null; 20 Number.prototype.NaN0=function(){return isNaN(this)?0:this;} 21 function CreateDragContainer(){ 22 /* 23 Create a new "Container Instance" so that items from one "Set" can not 24 be dragged into items from another "Set" 25 */ 26 var cDrag = DragDrops.length; 27 DragDrops[cDrag] = []; 28 /* 29 Each item passed to this function should be a "container". Store each 30 of these items in our current container 31 */ 32 for(var i=0; i<arguments.length; i++){ 33 var cObj = arguments[i]; 34 DragDrops[cDrag].push(cObj); 35 cObj.setAttribute('DropObj', cDrag); 36 /* 37 Every top level item in these containers should be draggable. Do this 38 by setting the DragObj attribute on each item and then later checking 39 this attribute in the mouseMove function 40 */ 41 for(var j=0; j<cObj.childNodes.length; j++){ 42 // Firefox puts in lots of #text nodes...skip these 43 if(cObj.childNodes[j].nodeName=='#text') continue; 44 cObj.childNodes[j].setAttribute('DragObj', cDrag); 45 } 46 } 47 } 48 function mouseMove(ev){ 49 ev = ev || window.event; 50 /* 51 We are setting target to whatever item the mouse is currently on 52 Firefox uses event.target here, MSIE uses event.srcElement 53 */ 54 var target = ev.target || ev.srcElement; 55 var mousePos = mouseCoords(ev); 56 // mouseOut event - fires if the item the mouse is on has changed 57 if(lastTarget && (target!==lastTarget)){ 58 // reset the classname for the target element 59 var origClass = lastTarget.getAttribute('origClass'); 60 if(origClass) lastTarget.className = origClass; 61 } 62 /* 63 dragObj is the grouping our item is in (set from the createDragContainer function). 64 if the item is not in a grouping we ignore it since it can't be dragged with this 65 script. 66 */ 67 var dragObj = target.getAttribute('DragObj'); 68 // if the mouse was moved over an element that is draggable 69 if(dragObj!=null){ 70 // mouseOver event - Change the item's class if necessary 71 if(target!=lastTarget){ 72 var oClass = target.getAttribute('overClass'); 73 if(oClass){ 74 target.setAttribute('origClass', target.className); 75 target.className = oClass; 76 } 77 } 78 // if the user is just starting to drag the element 79 if(iMouseDown && !lMouseState){ 80 // mouseDown target 81 curTarget = target; 82 // Record the mouse x and y offset for the element 83 rootParent = curTarget.parentNode; 84 rootSibling = curTarget.nextSibling; 85 mouseOffset = getMouseOffset(target, ev); 86 // We remove anything that is in our dragHelper DIV so we can put a new item in it. 87 for(var i=0; i<dragHelper.childNodes.length; i++) dragHelper.removeChild(dragHelper.childNodes[i]); 88 // Make a copy of the current item and put it in our drag helper. 89 dragHelper.appendChild(curTarget.cloneNode(true)); 90 dragHelper.style.display = 'block'; 91 // set the class on our helper DIV if necessary 92 var dragClass = curTarget.getAttribute('dragClass'); 93 if(dragClass){ 94 dragHelper.firstChild.className = dragClass; 95 } 96 // disable dragging from our helper DIV (it's already being dragged) 97 dragHelper.firstChild.removeAttribute('DragObj'); 98 /* 99 Record the current position of all drag/drop targets related 100 to the element. We do this here so that we do not have to do 101 it on the general mouse move event which fires when the mouse 102 moves even 1 pixel. If we don't do this here the script 103 would run much slower. 104 */ 105 var dragConts = DragDrops[dragObj]; 106 /* 107 first record the width/height of our drag item. Then hide it since 108 it is going to (potentially) be moved out of its parent. 109 */ 110 curTarget.setAttribute('startWidth', parseInt(curTarget.offsetWidth)); 111 curTarget.setAttribute('startHeight', parseInt(curTarget.offsetHeight)); 112 curTarget.style.display = 'none'; 113 // loop through each possible drop container 114 for(var i=0; i<dragConts.length; i++){ 115 with(dragConts[i]){ 116 var pos = getPosition(dragConts[i]); 117 /* 118 save the width, height and position of each container. 119 Even though we are saving the width and height of each 120 container back to the container this is much faster because 121 we are saving the number and do not have to run through 122 any calculations again. Also, offsetHeight and offsetWidth 123 are both fairly slow. You would never normally notice any 124 performance hit from these two functions but our code is 125 going to be running hundreds of times each second so every 126 little bit helps! 127 Note that the biggest performance gain here, by far, comes 128 from not having to run through the getPosition function 129 hundreds of times. 130 */ 131 setAttribute('startWidth', parseInt(offsetWidth)); 132 setAttribute('startHeight', parseInt(offsetHeight)); 133 setAttribute('startLeft', pos.x); 134 setAttribute('startTop', pos.y); 135 } 136 // loop through each child element of each container 137 for(var j=0; j<dragConts[i].childNodes.length; j++){ 138 with(dragConts[i].childNodes[j]){ 139 if((nodeName=='#text') || (dragConts[i].childNodes[j]==curTarget)) continue; 140 var pos = getPosition(dragConts[i].childNodes[j]); 141 // save the width, height and position of each element 142 setAttribute('startWidth', parseInt(offsetWidth)); 143 setAttribute('startHeight', parseInt(offsetHeight)); 144 setAttribute('startLeft', pos.x); 145 setAttribute('startTop', pos.y); 146 } 147 } 148 } 149 } 150 } 151 // If we get in here we are dragging something 152 if(curTarget){ 153 // move our helper div to wherever the mouse is (adjusted by mouseOffset) 154 dragHelper.style.top = mousePos.y - mouseOffset.y; 155 dragHelper.style.left = mousePos.x - mouseOffset.x; 156 var dragConts = DragDrops[curTarget.getAttribute('DragObj')]; 157 var activeCont = null; 158 var xPos = mousePos.x - mouseOffset.x + (parseInt(curTarget.getAttribute('startWidth')) /2); 159 var yPos = mousePos.y - mouseOffset.y + (parseInt(curTarget.getAttribute('startHeight'))/2); 160 // check each drop container to see if our target object is "inside" the container 161 for(var i=0; i<dragConts.length; i++){ 162 with(dragConts[i]){ 163 if(((getAttribute('startLeft')) < xPos) && 164 ((getAttribute('startTop')) < yPos) && 165 ((getAttribute('startLeft') + getAttribute('startWidth')) > xPos) && 166 ((getAttribute('startTop') + getAttribute('startHeight')) > yPos)){ 167 /* 168 our target is inside of our container so save the container into 169 the activeCont variable and then exit the loop since we no longer 170 need to check the rest of the containers 171 */ 172 activeCont = dragConts[i]; 173 // exit the for loop 174 break; 175 } 176 } 177 } 178 // Our target object is in one of our containers. Check to see where our div belongs 179 if(activeCont){ 180 // beforeNode will hold the first node AFTER where our div belongs 181 var beforeNode = null; 182 // loop through each child node (skipping text nodes). 183 for(var i=activeCont.childNodes.length-1; i>=0; i--){ 184 with(activeCont.childNodes[i]){ 185 if(nodeName=='#text') continue; 186 // if the current item is "After" the item being dragged 187 if( 188 curTarget != activeCont.childNodes[i] && 189 ((getAttribute('startLeft') + getAttribute('startWidth')) > xPos) && 190 ((getAttribute('startTop') + getAttribute('startHeight')) > yPos)){ 191 beforeNode = activeCont.childNodes[i]; 192 } 193 } 194 } 195 // the item being dragged belongs before another item 196 if(beforeNode){ 197 if(beforeNode!=curTarget.nextSibling){ 198 activeCont.insertBefore(curTarget, beforeNode); 199 } 200 // the item being dragged belongs at the end of the current container 201 } else { 202 if((curTarget.nextSibling) || (curTarget.parentNode!=activeCont)){ 203 activeCont.appendChild(curTarget); 204 } 205 } 206 // make our drag item visible 207 if(curTarget.style.display!=''){ 208 curTarget.style.display = ''; 209 } 210 } else { 211 // our drag item is not in a container, so hide it. 212 if(curTarget.style.display!='none'){ 213 curTarget.style.display = 'none'; 214 } 215 } 216 } 217 // track the current mouse state so we can compare against it next time 218 lMouseState = iMouseDown; 219 // mouseMove target 220 lastTarget = target; 221 // track the current mouse state so we can compare against it next time 222 lMouseState = iMouseDown; 223 // this helps prevent items on the page from being highlighted while dragging 224 return false; 225 } 226 function mouseUp(ev){ 227 if(curTarget){ 228 // hide our helper object - it is no longer needed 229 dragHelper.style.display = 'none'; 230 // if the drag item is invisible put it back where it was before moving it 231 if(curTarget.style.display == 'none'){ 232 if(rootSibling){ 233 rootParent.insertBefore(curTarget, rootSibling); 234 } else { 235 rootParent.appendChild(curTarget); 236 } 237 } 238 // make sure the drag item is visible 239 curTarget.style.display = ''; 240 } 241 curTarget = null; 242 iMouseDown = false; 243 } 244 function mouseDown(){ 245 iMouseDown = true; 246 if(lastTarget){ 247 return false; 248 } 249 } 250 document.onmousemove = mouseMove; 251 document.onmousedown = mouseDown; 252 document.onmouseup = mouseUp; 253 window.onload = function(){ 254 // Create our helper object that will show the item while dragging 255 dragHelper = document.createElement('DIV'); 256 dragHelper.style.cssText = 'position:absolute;display:none;'; 257 CreateDragContainer( 258 document.getElementById('DragContainer1'), 259 document.getElementById('DragContainer2'), 260 document.getElementById('DragContainer3') 261 ); 262 document.body.appendChild(dragHelper); 263 } <!--the mouse over and dragging class are defined on each item--> 2 <div class="DragContainer" id="DragContainer1"> 3 <div class="DragBox" id="Item1" overClass="OverDragBox" dragClass="DragDragBox">Item #1</div> 4 <div class="DragBox" id="Item2" overClass="OverDragBox" dragClass="DragDragBox">Item #2</div> 5 <div class="DragBox" id="Item3" overClass="OverDragBox" dragClass="DragDragBox">Item #3</div> 6 <div class="DragBox" id="Item4" overClass="OverDragBox" dragClass="DragDragBox">Item #4</div> 7 </div> 8 <div class="DragContainer" id="DragContainer2"> 9 <div class="DragBox" id="Item5" overClass="OverDragBox" dragClass="DragDragBox">Item #5</div> 10 <div class="DragBox" id="Item6" overClass="OverDragBox" dragClass="DragDragBox">Item #6</div> 11 <div class="DragBox" id="Item7" overClass="OverDragBox" dragClass="DragDragBox">Item #7</div> 12 <div class="DragBox" id="Item8" overClass="OverDragBox" dragClass="DragDragBox">Item #8</div> 13 </div> 14 <div class="DragContainer" id="DragContainer3"> 15 <div class="DragBox" id="Item9" overClass="OverDragBox" dragClass="DragDragBox">Item #9</div> 16 <div class="DragBox" id="Item10" overClass="OverDragBox" dragClass="DragDragBox">Item #10</div> 17 <div class="DragBox" id="Item11" overClass="OverDragBox" dragClass="DragDragBox">Item #11</div> 18 <div class="DragBox" id="Item12" overClass="OverDragBox" dragClass="DragDragBox">Item #12</div> 19 </div>

Ou sinon je te suggère d'aller voir la superbe tuto de Grafikart.

Supa polo, il y a 10 ans

Merci de ta réponse , mais ce n'est pas exactement cela que je recherche.

Supa polo, il y a 10 ans

J'ai également une autre question, comment sont fait les chargements en web par exemple pour passer d'une page à l'autre j'aimerais mettre un chargement réel ou fictif est il possible de le faire et si oui comment ?

Merci d'avance ^^

Genki, il y a 10 ans

Pour le drag & drop, Graf à fait un tuto il y a un mois dessus, regarde un peu : Drag'n'Drop

Pour le côté chargement, je suppose que tu parles d'un changement de page sans rechargement ? Si c'est le cas c'est en Ajax qu'il faut s'adresser, là aussi Graf à toutes les réponses : Navigation Ajax