aiuto jquery: draggable & droppable

surferbloggy

Nuovo Utente
21 Feb 2010
3
0
0
ciao, sto facendo uno script jquery che dovrebbe funzionare cosi:
quando clicco un pulsante clono una riga di elementi grafici (div con bg immagine) e ne creo un'altra riga
poi a destra ho una serie di altri elementi grafici colorati da trascinare verso gli elementi dell'ultima riga generata
in modo che questi elementi cambiano colore al rilascio e memorizzo il numero del colore in un array

solo che funziona male nel senso che per cambiare il colore degli elementi dell'ultima riga generata devo trascinarli sulla prima
insomma dovrei cambiare il colore all'elemento con cui ho la collisione, invece questo non avviene e per cambiare l'ultima riga devo toccare gli elementi della prima riga
è come se il droppable trova il numero di riga non aggiornato e quindi per cambiare la riga 3 devo fare il drag'n'drop sulla 1

questo è il codice della funzione che richiamo al click del pulsante per generare le righe la funzione contirene anche il droppable che ripeto per ogni elemento della riga (che sono 7 per ogni riga) e faccio tornare i 12 colori al loro posto quindi è un po lungo, ancora non ho capito dove' l'errore
mi date un aiuto???

Codice:
$('.newatt').click(function() {
			  
			  
			  			//////////////////
						
					var idparent=$(this).parent().attr("id");
					if(idparent.length==6){
							var bid= idparent.slice(-1);
						}else{
							var bid= idparent.slice(-2);
						}
						nid=Number(bid)+1;
					
					alert(nid);
				
				row++;
				
				var rr=row-1;
				$('#idrow1').clone(true).attr('id', 'idrow'+ row).insertAfter("#idrow"+rr);
				
				
				$('#idrow'+row+" div").each(function(){
    				$(this).css({'background':'transparent url(assets/images/can3Dwhite.png) no-repeat 0 0'});	
					$('.newatt').css({'background':'transparent url(assets/images/can3Dblack.png) no-repeat 0 0'});
				});
				
				
				
				//////////////////
				
				
				var arrATT=new Array(); 
				
				switch(formulasize)
				{
				case 5:
					arrATT[row]=[0,0,0,0,0];
					
  					
  				break;
				case 6:
					arrATT[row]=[0,0,0,0,0,0];
  					
  				break;
				case 7:
					arrATT[row]=[0,0,0,0,0,0,0];
  					
  				break;
				default:
  					arrATT[row]=[0,0,0,0,0];
					
				}
				
				//alert(row);
				
				
				
				/////////////
				for (var i=1;i<=12;i++){
				
				$("#gc"+i).draggable({
   					 cursor: 'move',          // sets the cursor apperance
    				 revert: 'invalid',       // makes the item to return if it isn't placed into droppable
    				 revertDuration: 100,     // duration while the item returns to its place
    				 opacity: 1 ,
					           // opacity while the element is dragged
					drag: function(event, ui) {

					}
  					});
				}
				
				//////////////////////////////////////////
				
					$( nid+" .c01").droppable({
					
   					 drop: function(event, ui) {
						 
						
						
						
						var a= $(ui.draggable).attr("id");
						if(a.length==3){
							var b= a.slice(-1);
						}else{
							var b= a.slice(-2);
						}
						
						
						
						
						$( nid+" .c01").css({'background':'transparent url(assets/images/can3D'+b+'.png) no-repeat 0 0'});
						
						
						
						arrATT[row][1]=b;
						alert(arrATT[row][1]);
						
						
						$("#gc1").animate({'left':'0','top':'0','opacity':'1'}, 100, function(){

    						$(this).css({'left':'0','top':'0', 'opacity':'1'});

						});
						$("#gc2").animate({'left':'45','top':'0','opacity':'1'}, 100, function(){

    						$(this).css({'left':'45','top':'0', 'opacity':'1'});

						});
						$("#gc3").animate({'left':'80','top':'0','opacity':'1'}, 100, function(){

    						$(this).css({'left':'80','top':'0', 'opacity':'1'});

						});
						$("#gc4").animate({'left':'0','top':'130','opacity':'1'}, 100, function(){

    						$(this).css({'left':'0','top':'130', 'opacity':'1'});

						});
						$("#gc5").animate({'left':'45','top':'130','opacity':'1'}, 100, function(){

    						$(this).css({'left':'45','top':'130', 'opacity':'1'});

						});
						$("#gc6").animate({'left':'80','top':'130','opacity':'1'}, 100, function(){

    						$(this).css({'left':'80','top':'130', 'opacity':'1'});

						});
						$("#gc7").animate({'left':'0','top':'260','opacity':'1'}, 100, function(){

    						$(this).css({'left':'0','top':'260', 'opacity':'1'});

						});
						$("#gc8").animate({'left':'45','top':'260','opacity':'1'}, 100, function(){

    						$(this).css({'left':'45','top':'260', 'opacity':'1'});

						});
						$("#gc9").animate({'left':'80','top':'260','opacity':'1'}, 100, function(){

    						$(this).css({'left':'80','top':'260', 'opacity':'1'});

						});
						$("#gc10").animate({'left':'0','top':'395','opacity':'1'}, 100, function(){

    						$(this).css({'left':'0','top':'395', 'opacity':'1'});

						});
						$("#gc11").animate({'left':'45','top':'395','opacity':'1'}, 100, function(){

    						$(this).css({'left':'45','top':'395', 'opacity':'1'});

						});
						$("#gc12").animate({'left':'80','top':'395','opacity':'1'}, 100, function(){

    						$(this).css({'left':'80','top':'395', 'opacity':'1'});

						});
						
						
						
						
						
						
    				}
 					});
				
				
				//////////////////////////////////////////
				
				
				//////////////////////////////////////////
				
				
					
					$( nid+" .c02").droppable({
   					 drop: function(event, ui) {
						 
						
					
						
						var a= $(ui.draggable).attr("id");
						if(a.length==3){
							var b= a.slice(-1);
						}else{
							var b= a.slice(-2);
						}
						
						
						
						$( nid+" .c02").css({'background':'transparent url(assets/images/can3D'+b+'.png) no-repeat 0 0'});
						
						
						arrATT[row][2]=b;
						alert(arrATT[row][2]);
						
						$("#gc1").animate({'left':'0','top':'0','opacity':'1'}, 100, function(){

    						$(this).css({'left':'0','top':'0', 'opacity':'1'});

						});
						$("#gc2").animate({'left':'45','top':'0','opacity':'1'}, 100, function(){

    						$(this).css({'left':'45','top':'0', 'opacity':'1'});

						});
						$("#gc3").animate({'left':'80','top':'0','opacity':'1'}, 100, function(){

    						$(this).css({'left':'80','top':'0', 'opacity':'1'});

						});
						$("#gc4").animate({'left':'0','top':'130','opacity':'1'}, 100, function(){

    						$(this).css({'left':'0','top':'130', 'opacity':'1'});

						});
						$("#gc5").animate({'left':'45','top':'130','opacity':'1'}, 100, function(){

    						$(this).css({'left':'45','top':'130', 'opacity':'1'});

						});
						$("#gc6").animate({'left':'80','top':'130','opacity':'1'}, 100, function(){

    						$(this).css({'left':'80','top':'130', 'opacity':'1'});

						});
						$("#gc7").animate({'left':'0','top':'260','opacity':'1'}, 100, function(){

    						$(this).css({'left':'0','top':'260', 'opacity':'1'});

						});
						$("#gc8").animate({'left':'45','top':'260','opacity':'1'}, 100, function(){

    						$(this).css({'left':'45','top':'260', 'opacity':'1'});

						});
						$("#gc9").animate({'left':'80','top':'260','opacity':'1'}, 100, function(){

    						$(this).css({'left':'80','top':'260', 'opacity':'1'});

						});
						$("#gc10").animate({'left':'0','top':'395','opacity':'1'}, 100, function(){

    						$(this).css({'left':'0','top':'395', 'opacity':'1'});

						});
						$("#gc11").animate({'left':'45','top':'395','opacity':'1'}, 100, function(){

    						$(this).css({'left':'45','top':'395', 'opacity':'1'});

						});
						$("#gc12").animate({'left':'80','top':'395','opacity':'1'}, 100, function(){

    						$(this).css({'left':'80','top':'395', 'opacity':'1'});

						});
						
						
						
						
						
						
    				}
 					});
				
				
				//////////////////////////////////////////
				
				//////////////////////////////////////////
				
					$( nid+" .c03").droppable({
   					 drop: function(event, ui) {
						 
						
					
						
						var a= $(ui.draggable).attr("id");
						if(a.length==3){
							var b= a.slice(-1);
						}else{
							var b= a.slice(-2);
						}
						
						
						
						$( nid+" .c03").css({'background':'transparent url(assets/images/can3D'+b+'.png) no-repeat 0 0'});
						
						
						arrATT[row][3]=b;
						alert(arrATT[row][3]);
						
						$("#gc1").animate({'left':'0','top':'0','opacity':'1'}, 100, function(){

    						$(this).css({'left':'0','top':'0', 'opacity':'1'});

						});
						$("#gc2").animate({'left':'45','top':'0','opacity':'1'}, 100, function(){

    						$(this).css({'left':'45','top':'0', 'opacity':'1'});

						});
						$("#gc3").animate({'left':'80','top':'0','opacity':'1'}, 100, function(){

    						$(this).css({'left':'80','top':'0', 'opacity':'1'});

						});
						$("#gc4").animate({'left':'0','top':'130','opacity':'1'}, 100, function(){

    						$(this).css({'left':'0','top':'130', 'opacity':'1'});

						});
						$("#gc5").animate({'left':'45','top':'130','opacity':'1'}, 100, function(){

    						$(this).css({'left':'45','top':'130', 'opacity':'1'});

						});
						$("#gc6").animate({'left':'80','top':'130','opacity':'1'}, 100, function(){

    						$(this).css({'left':'80','top':'130', 'opacity':'1'});

						});
						$("#gc7").animate({'left':'0','top':'260','opacity':'1'}, 100, function(){

    						$(this).css({'left':'0','top':'260', 'opacity':'1'});

						});
						$("#gc8").animate({'left':'45','top':'260','opacity':'1'}, 100, function(){

    						$(this).css({'left':'45','top':'260', 'opacity':'1'});

						});
						$("#gc9").animate({'left':'80','top':'260','opacity':'1'}, 100, function(){

    						$(this).css({'left':'80','top':'260', 'opacity':'1'});

						});
						$("#gc10").animate({'left':'0','top':'395','opacity':'1'}, 100, function(){

    						$(this).css({'left':'0','top':'395', 'opacity':'1'});

						});
						$("#gc11").animate({'left':'45','top':'395','opacity':'1'}, 100, function(){

    						$(this).css({'left':'45','top':'395', 'opacity':'1'});

						});
						$("#gc12").animate({'left':'80','top':'395','opacity':'1'}, 100, function(){

    						$(this).css({'left':'80','top':'395', 'opacity':'1'});

						});
						
						
						
						
						
						
						
    				}
 					});
				
				
				//////////////////////////////////////////
				
				
				//////////////////////////////////////////
				
					$( nid+" .c04").droppable({
   					 drop: function(event, ui) {
						 
						
					
						
						var a= $(ui.draggable).attr("id");
						if(a.length==3){
							var b= a.slice(-1);
						}else{
							var b= a.slice(-2);
						}
						
						
						
						$( nid+" .c04").css({'background':'transparent url(assets/images/can3D'+b+'.png) no-repeat 0 0'});
						
						
						arrATT[row][4]=b;
						alert(arrATT[row][4]);
						
						$("#gc1").animate({'left':'0','top':'0','opacity':'1'}, 100, function(){

    						$(this).css({'left':'0','top':'0', 'opacity':'1'});

						});
						$("#gc2").animate({'left':'45','top':'0','opacity':'1'}, 100, function(){

    						$(this).css({'left':'45','top':'0', 'opacity':'1'});

						});
						$("#gc3").animate({'left':'80','top':'0','opacity':'1'}, 100, function(){

    						$(this).css({'left':'80','top':'0', 'opacity':'1'});

						});
						$("#gc4").animate({'left':'0','top':'130','opacity':'1'}, 100, function(){

    						$(this).css({'left':'0','top':'130', 'opacity':'1'});

						});
						$("#gc5").animate({'left':'45','top':'130','opacity':'1'}, 100, function(){

    						$(this).css({'left':'45','top':'130', 'opacity':'1'});

						});
						$("#gc6").animate({'left':'80','top':'130','opacity':'1'}, 100, function(){

    						$(this).css({'left':'80','top':'130', 'opacity':'1'});

						});
						$("#gc7").animate({'left':'0','top':'260','opacity':'1'}, 100, function(){

    						$(this).css({'left':'0','top':'260', 'opacity':'1'});

						});
						$("#gc8").animate({'left':'45','top':'260','opacity':'1'}, 100, function(){

    						$(this).css({'left':'45','top':'260', 'opacity':'1'});

						});
						$("#gc9").animate({'left':'80','top':'260','opacity':'1'}, 100, function(){

    						$(this).css({'left':'80','top':'260', 'opacity':'1'});

						});
						$("#gc10").animate({'left':'0','top':'395','opacity':'1'}, 100, function(){

    						$(this).css({'left':'0','top':'395', 'opacity':'1'});

						});
						$("#gc11").animate({'left':'45','top':'395','opacity':'1'}, 100, function(){

    						$(this).css({'left':'45','top':'395', 'opacity':'1'});

						});
						$("#gc12").animate({'left':'80','top':'395','opacity':'1'}, 100, function(){

    						$(this).css({'left':'80','top':'395', 'opacity':'1'});

						});
						
						
						
						
						
						
						
    				}
 					});
				
				
				//////////////////////////////////////////
				
				//////////////////////////////////////////
				
					$( nid+" .c05").droppable({
   					 drop: function(event, ui) {
						 
						
					
						
						var a= $(ui.draggable).attr("id");
						if(a.length==3){
							var b= a.slice(-1);
						}else{
							var b= a.slice(-2);
						}
						
						
						
						$( nid+" .c05").css({'background':'transparent url(assets/images/can3D'+b+'.png) no-repeat 0 0'});
						
						arrATT[row][5]=b;
						alert(arrATT[row][5]);
						
						$("#gc1").animate({'left':'0','top':'0','opacity':'1'}, 100, function(){

    						$(this).css({'left':'0','top':'0', 'opacity':'1'});

						});
						$("#gc2").animate({'left':'45','top':'0','opacity':'1'}, 100, function(){

    						$(this).css({'left':'45','top':'0', 'opacity':'1'});

						});
						$("#gc3").animate({'left':'80','top':'0','opacity':'1'}, 100, function(){

    						$(this).css({'left':'80','top':'0', 'opacity':'1'});

						});
						$("#gc4").animate({'left':'0','top':'130','opacity':'1'}, 100, function(){

    						$(this).css({'left':'0','top':'130', 'opacity':'1'});

						});
						$("#gc5").animate({'left':'45','top':'130','opacity':'1'}, 100, function(){

    						$(this).css({'left':'45','top':'130', 'opacity':'1'});

						});
						$("#gc6").animate({'left':'80','top':'130','opacity':'1'}, 100, function(){

    						$(this).css({'left':'80','top':'130', 'opacity':'1'});

						});
						$("#gc7").animate({'left':'0','top':'260','opacity':'1'}, 100, function(){

    						$(this).css({'left':'0','top':'260', 'opacity':'1'});

						});
						$("#gc8").animate({'left':'45','top':'260','opacity':'1'}, 100, function(){

    						$(this).css({'left':'45','top':'260', 'opacity':'1'});

						});
						$("#gc9").animate({'left':'80','top':'260','opacity':'1'}, 100, function(){

    						$(this).css({'left':'80','top':'260', 'opacity':'1'});

						});
						$("#gc10").animate({'left':'0','top':'395','opacity':'1'}, 100, function(){

    						$(this).css({'left':'0','top':'395', 'opacity':'1'});

						});
						$("#gc11").animate({'left':'45','top':'395','opacity':'1'}, 100, function(){

    						$(this).css({'left':'45','top':'395', 'opacity':'1'});

						});
						$("#gc12").animate({'left':'80','top':'395','opacity':'1'}, 100, function(){

    						$(this).css({'left':'80','top':'395', 'opacity':'1'});

						});
						
						
						
						
						
						
						
						
						
    				}
 					});
				
				
				//////////////////////////////////////////
				
				
				//////////////////////////////////////////
				
					$( nid+" .c06").droppable({
   					 drop: function(event, ui) {
						 
						
					
						
						var a= $(ui.draggable).attr("id");
						if(a.length==3){
							var b= a.slice(-1);
						}else{
							var b= a.slice(-2);
						}
						
						
						
						$( nid+" .c06").css({'background':'transparent url(assets/images/can3D'+b+'.png) no-repeat 0 0'});
						
						
						arrATT[row][6]=b;
						alert(arrATT[row][6]);
						
						$("#gc1").animate({'left':'0','top':'0','opacity':'1'}, 100, function(){

    						$(this).css({'left':'0','top':'0', 'opacity':'1'});

						});
						$("#gc2").animate({'left':'45','top':'0','opacity':'1'}, 100, function(){

    						$(this).css({'left':'45','top':'0', 'opacity':'1'});

						});
						$("#gc3").animate({'left':'80','top':'0','opacity':'1'}, 100, function(){

    						$(this).css({'left':'80','top':'0', 'opacity':'1'});

						});
						$("#gc4").animate({'left':'0','top':'130','opacity':'1'}, 100, function(){

    						$(this).css({'left':'0','top':'130', 'opacity':'1'});

						});
						$("#gc5").animate({'left':'45','top':'130','opacity':'1'}, 100, function(){

    						$(this).css({'left':'45','top':'130', 'opacity':'1'});

						});
						$("#gc6").animate({'left':'80','top':'130','opacity':'1'}, 100, function(){

    						$(this).css({'left':'80','top':'130', 'opacity':'1'});

						});
						$("#gc7").animate({'left':'0','top':'260','opacity':'1'}, 100, function(){

    						$(this).css({'left':'0','top':'260', 'opacity':'1'});

						});
						$("#gc8").animate({'left':'45','top':'260','opacity':'1'}, 100, function(){

    						$(this).css({'left':'45','top':'260', 'opacity':'1'});

						});
						$("#gc9").animate({'left':'80','top':'260','opacity':'1'}, 100, function(){

    						$(this).css({'left':'80','top':'260', 'opacity':'1'});

						});
						$("#gc10").animate({'left':'0','top':'395','opacity':'1'}, 100, function(){

    						$(this).css({'left':'0','top':'395', 'opacity':'1'});

						});
						$("#gc11").animate({'left':'45','top':'395','opacity':'1'}, 100, function(){

    						$(this).css({'left':'45','top':'395', 'opacity':'1'});

						});
						$("#gc12").animate({'left':'80','top':'395','opacity':'1'}, 100, function(){

    						$(this).css({'left':'80','top':'395', 'opacity':'1'});

						});
						
						
						
						
						
						
						
						
    				}
 					});
				
				
				//////////////////////////////////////////
				
				
				//////////////////////////////////////////
				
					$( nid+" .c07").droppable({
   					 drop: function(event, ui) {
						 
						
					
						
						var a= $(ui.draggable).attr("id");
						if(a.length==3){
							var b= a.slice(-1);
						}else{
							var b= a.slice(-2);
						}
						
						
						
						$( nid+" .c07").css({'background':'transparent url(assets/images/can3D'+b+'.png) no-repeat 0 0'});
						
						
						arrATT[row][7]=b;
						alert(arrATT[row][7]);
						
						$("#gc1").animate({'left':'0','top':'0','opacity':'1'}, 100, function(){

    						$(this).css({'left':'0','top':'0', 'opacity':'1'});

						});
						$("#gc2").animate({'left':'45','top':'0','opacity':'1'}, 100, function(){

    						$(this).css({'left':'45','top':'0', 'opacity':'1'});

						});
						$("#gc3").animate({'left':'80','top':'0','opacity':'1'}, 100, function(){

    						$(this).css({'left':'80','top':'0', 'opacity':'1'});

						});
						$("#gc4").animate({'left':'0','top':'130','opacity':'1'}, 100, function(){

    						$(this).css({'left':'0','top':'130', 'opacity':'1'});

						});
						$("#gc5").animate({'left':'45','top':'130','opacity':'1'}, 100, function(){

    						$(this).css({'left':'45','top':'130', 'opacity':'1'});

						});
						$("#gc6").animate({'left':'80','top':'130','opacity':'1'}, 100, function(){

    						$(this).css({'left':'80','top':'130', 'opacity':'1'});

						});
						$("#gc7").animate({'left':'0','top':'260','opacity':'1'}, 100, function(){

    						$(this).css({'left':'0','top':'260', 'opacity':'1'});

						});
						$("#gc8").animate({'left':'45','top':'260','opacity':'1'}, 100, function(){

    						$(this).css({'left':'45','top':'260', 'opacity':'1'});

						});
						$("#gc9").animate({'left':'80','top':'260','opacity':'1'}, 100, function(){

    						$(this).css({'left':'80','top':'260', 'opacity':'1'});

						});
						$("#gc10").animate({'left':'0','top':'395','opacity':'1'}, 100, function(){

    						$(this).css({'left':'0','top':'395', 'opacity':'1'});

						});
						$("#gc11").animate({'left':'45','top':'395','opacity':'1'}, 100, function(){

    						$(this).css({'left':'45','top':'395', 'opacity':'1'});

						});
						$("#gc12").animate({'left':'80','top':'395','opacity':'1'}, 100, function(){

    						$(this).css({'left':'80','top':'395', 'opacity':'1'});

						});
						
						
						
						
						
						
						
						
						
    				}
 					});
				
				
				//////////////////////////////////////////
			 
			  
		   });
 

Discussioni simili