2009-05-13 4 views
2

Lassen Sie sich nicht unten Code verscheuchen Sie weg. Die Frage ist wirklich einfach, nur zwei Zeilen machen Probleme:jquery: Variables Rückkehr NaN

Warum generiert mein Code einen NaN-Fehlercode? Ich versuche, einen Variablenwert von einem anderen zu subtrahieren, damit die Position der Elemente korrekt ist.

Die Variablen haben ihren Wert von jQuery position(), die sowieso integer sein soll.

Überprüfen Sie die Linien diese Zeilen:

// For some reason NaN error code gets generated when line below gets executed. 
var posTop = Startpos.top - Stoppos.top; 
var posLeft = Startpos.left - Stoppos.left; 

komplette Code:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en"> 
<head> 
    <meta http-equiv="Content-type" content="text/html; charset=iso-8859-1" /> 
    <title>Drag drop 1</title> 
    <script type="text/javascript" src="js/jquery-1.3.2.min.js"></script> 
    <script type="text/javascript" src="js/jquery-ui-1.7.1.custom.min.js"></script> 
    <script type="text/javascript"> 
    $(document).ready(function() { 
     var Startpos = new Array; 
     var Stoppos = new Array; 

     // Make images draggable. 
     $(".item").draggable({ 

      // Elements cannot go outside #container 
      containment: 'parent', 

      // Make sure the element can only be dropped in a grid. 
      grid: [150,150], 

      // Find original position of dragged image. 
      start: function(event, ui) { 

       // Make sure picture always are on top when dragged (z-index). 
       $(this).css({'z-index' : '100'}); 

       // Show start dragged position of image. 
       Startpos = $(this).position(); 
       $("div#start").text("START: \nLeft: "+ Startpos.left + "\nTop: " + Startpos.top); 
      }, 

      // Find position where image is dropped. 
      stop: function(event, ui) { 

       // Revert to default layer position when dropped (z-index). 
       $(this).css({'z-index' : '10'}); 

       // Show dropped position. 
       Stoppos = $(this).position(); 
       $("div#stop").text("STOP: \nLeft: "+ Stoppos.left + "\nTop: " + Stoppos.top); 
      } 
     }); 
     $(".item").droppable({ 
      drop: function(event, ui) { 

       // Dragged image gets swapped with dropped on image. 
       var prev_position = "#" + $(this).attr('id'); 
       // For some reason NaN error code gets generated when line below gets executed. 
       var posTop = Startpos.top - Stoppos.top; 
       var posLeft = Startpos.left - Stoppos.left; 

       // Below variables will work. But unfortunately they 
       // doesn't give the correct numbers for the purpose. 
       // var posTop = Startpos.top; 
       // var posLeft = Startpos.left; 

       $(prev_position).css({'top' : posTop, 'left' : posLeft}); 

       $("div#test").text("Passed variables. Top: " + posTop + " left: " + posLeft); 
      } 
     }); 
    }); 
    </script> 
    <style> 
    body { 

    } 
    #container { 
     position:relative; 
     width:480px; 
     border:1px solid #000; 
    } 
    .item { 
     position:relative; 
     width:150px; 
     height:150px; 
     z-index:10; 
    } 
    </style> 
</head> 
<body> 
    <div id="container"> 
     <img id="productid_1" src="images/pic1.jpg" class="item" alt="" title="" /><img id="productid_2" src="images/pic2.jpg" class="item" alt="" title="" /><img id="productid_3" src="images/pic3.jpg" class="item" alt="" title="" /><img id="productid_4" src="images/pic4.jpg" class="item" alt="" title="" /><img id="productid_5" src="images/pic5.jpg" class="item" alt="" title="" /><img id="productid_6" src="images/pic6.jpg" class="item" alt="" title="" /><img id="productid_7" src="images/pic7.jpg" class="item" alt="" title="" /><img id="productid_8" src="images/pic8.jpg" class="item" alt="" title="" /><img id="productid_9" src="images/pic9.jpg" class="item" alt="" title="" /> 
    </div> 
    <div style="clear:both;"></div> 
    <div id="start">Waiting...</div> 
    <div id="stop">Waiting...</div> 
    <div id="hover">Waiting...</div> 
    <div id="stop2">Waiting...</div> 
    <div id="test">Waiting...</div> 
</body> 
</html> 

Antwort

4

Das Problem ist, dass droppable.drop() vor draggable.stop() heißt. Also ist Ihr Stoppos noch nicht berechnet.

Eine Möglichkeit, dies zu tun, wäre, einfach zu verfolgen, welches Element gezogen wird, und die Position dafür in droppable.drop() zu berechnen. z.B. (Teilmenge Ihres Codes), beachten Sie das Objekt "Ziehen".

$(document).ready(function() { 
     var Startpos = new Array; 
     var Stoppos = new Array; 
     var Dragging = null; 

     // Make images draggable. 
     $(".item").draggable({ 

       // Elements cannot go outside #container 
       containment: 'parent', 

       // Make sure the element can only be dropped in a grid. 
       grid: [150,150], 

       // Find original position of dragged image. 
       start: function(event, ui) { 
         Dragging=this; 
         // Make sure picture always are on top when dragged (z-index). 
         $(this).css({'z-index' : '100'}); 

         // Show start dragged position of image. 
         Startpos = $(this).position(); 
         $("div#start").text("START: \nLeft: "+ Startpos.left + "\nTop: " + Startpos.top); 
       }, 

       // Find position where image is dropped. 
       stop: function(event, ui) { 
         // Revert to default layer position when dropped (z-index). 
         $(this).css({'z-index' : '10'}); 

         // Show dropped position. 
         Stoppos = $(this).position(); 
         $("div#stop").text("STOP: \nLeft: "+ Stoppos.left + "\nTop: " + Stoppos.top); 
         Dragging=null; 
       } 
     }); 

Allerdings gibt es wahrscheinlich mehrere andere legitime Möglichkeiten um dies.

+0

Ich frage mich auch-- was ist der Punkt der Initialisierung Stoppos und Startpos zu einem Array? Warum lassen Sie sie nicht nicht initialisiert oder setzen = null? – jlarson

+0

Hmmm ... Was mich verwirrt ist, dass ersetzt: var posTop = Startpos.top - Stoppos.top; var posLeft = Startpos.left - Stoppos.left; mit: var postop = Startpos.top; var posLeft = Startpos.left; Wird funktionieren, obwohl die Zahlen nicht das sind, was ich wollte. – Cudos

+0

Initialisierung Stoppos und Startpos stellt sie im globalen Bereich zur Verfügung. Aber ich bin neu bei globalen Variablen in Javascript. Habe gerade etwas versucht, das zu funktionieren schien. – Cudos

1

Soweit ich weiß, Position() ist kein jQuery-Funktion.

Versuchen Sie stattdessen:

Startpos = $(this).offset(); 

Sie sollten dann in der Lage sein, die oberen und linken Eigenschaften zuzugreifen.

+0

Eigentlich kann man Position() verwendet Siehe Link: http://docs.jquery.com/CSS/position zurückkehren die Position eines Elements – Cudos

0

Sind Sie sicher, dass dies die Linien sind, die das Problem verursachen? -

Sie bitte bestätigen, dass "var Postop = Startpos.top - Stoppos.top;"

eine gültige Integer/Double-Wert zu Postop zurückkehrt? Ich habe letzte Woche an einem meiner Projekte programmiert und hatte das gleiche NaN-Problem, ich habe versucht, den Wert zu Integer zu parsen und es hat funktioniert.

Weil Sie eine mathematische Berechnung zu machen, versuchen, die Werte in Double/Int Parsen, bevor Sie sie subtrahieren,

Manchmal ist die Lösung ziemlich einfach ist und wir neigen dazu, über sie erschweren.

Wenn die Analyse nicht funktioniert, könnte es sein, dass Sie nicht den richtigen Wert aus dem Array übergeben. Versuchen Sie etwas wie Startpos [0] .Top

Hoffe, das hilft, Viel Glück!

+0

Ich versuchte die parseInt() ohne Glück: var posTop = parseInt (Startpos.top - Stoppos.top); oder var posTop = parseInt (Startpos.top) - parseInt (Stoppos.top); – Cudos

0

Entweder Startpos oder Stoppos sind immer noch nur ein leeres Array, wenn Sie diese Berechnung durchführen.

Versuchen Sie, sie stattdessen auf einen geeigneten Standardwert (0, 0) zu setzen, da dies das Auffinden von Fehlern erleichtern kann.

0

Was mir ein Rätsel ist, dass dies funktioniert:

ersetzen:

var posTop = Startpos.top - Stoppos.top; 
var posLeft = Startpos.left - Stoppos.left; 

mit:

var posTop = Startpos.top; 
var posLeft = Startpos.left; 

funktioniert sogar, wenn die Zahlen nicht, was ich will. Es scheint, dass eine einfache Subtraktion aus irgendeinem Grund ungültig ist.

+0

Wird Stoppos.top & Stoppos.left immer als Zahlen initialisiert? –

+0

Ja, das sind sie. Das Problem ist woanders. – Cudos

0

Danke für die Hilfe Jungs :)

Das Problem gefunden.

Stoppos = $(this).position(); 

in sein sollte nicht:

$(".item").draggable({ 
stop: function(event, ui) { 

Doch statt in:

$(".item").droppable({ 
drop: function(event, ui) { 

komplette Code:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en"> 
<head> 
    <meta http-equiv="Content-type" content="text/html; charset=iso-8859-1" /> 
    <title>Drag drop 1</title> 
    <script type="text/javascript" src="js/jquery-1.3.2.min.js"></script> 
    <script type="text/javascript" src="js/jquery-ui-1.7.1.custom.min.js"></script> 
    <script type="text/javascript"> 
    $(document).ready(function() { 
     var Startpos = null; 
     var Stoppos = null; 

     // Make images draggable. 
     $(".item").draggable({ 

      // Elements cannot go outside #container 
      containment: 'parent', 

      // Make sure the element can only be dropped in a grid. 
      grid: [150,150], 

      // Find original position of dragged image. 
      start: function(event, ui) { 

       // Make sure picture always are on top when dragged (z-index). 
       $(this).css({'z-index' : '100'}); 

       // Show start dragged position of image. 
       Startpos = $(this).position(); 
       $("div#start").text("START: \nLeft: "+ Startpos.left + "\nTop: " + Startpos.top); 
      }, 

      // Find position where image is dropped. 
      stop: function(event, ui) { 

       // Revert to default layer position when dropped (z-index). 
       $(this).css({'z-index' : '10'}); 
      } 
     }); 
     $(".item").droppable({ 
      drop: function(event, ui) { 

       // Dragged image gets swapped with dropped on image. 
       var prev_position = "#" + $(this).attr('id'); 
       Stoppos = $(this).position(); 
       var posTop = Startpos.top - Stoppos.top; 
       var posLeft = Startpos.left - Stoppos.left; 
       $(prev_position).css({'top' : posTop, 'left' : posLeft}); 

       // Test window 
       $("div#stop").text("STOP: \nLeft: "+ Stoppos.left + "\nTop: " + Stoppos.top); 
       $("div#test").text("Passed variables. Top: " + posTop + " left: " + posLeft); 
      } 
     }); 
    }); 
    </script> 
    <style> 
    body { 

    } 
    #container { 
     position:relative; 
     width:480px; 
     border:1px solid #000; 
    } 
    .item { 
     position:relative; 
     width:150px; 
     height:150px; 
     z-index:10; 
    } 
    </style> 
</head> 
<body> 
    <div id="container"> 
     <img id="productid_1" src="images/pic1.jpg" class="item" alt="" title="" /><img id="productid_2" src="images/pic2.jpg" class="item" alt="" title="" /><img id="productid_3" src="images/pic3.jpg" class="item" alt="" title="" /><img id="productid_4" src="images/pic4.jpg" class="item" alt="" title="" /><img id="productid_5" src="images/pic5.jpg" class="item" alt="" title="" /><img id="productid_6" src="images/pic6.jpg" class="item" alt="" title="" /><img id="productid_7" src="images/pic7.jpg" class="item" alt="" title="" /><img id="productid_8" src="images/pic8.jpg" class="item" alt="" title="" /><img id="productid_9" src="images/pic9.jpg" class="item" alt="" title="" /> 
    </div> 
    <div style="clear:both;"></div> 
    <div id="start">Waiting...</div> 
    <div id="stop">Waiting...</div> 
    <div id="hover">Waiting...</div> 
    <div id="stop2">Waiting...</div> 
    <div id="test">Waiting...</div> 
</body> 
</html> 

Jetzt muss ich nur noch herausfinden, wie bekommen die Platzierung der Bilder, wenn sie mehr als einmal bewegt wurden. Es scheint, dass position: relative wird draggables automatisch hinzugefügt und Mühe macht in den Positionen der Berechnung :(