2009-05-13 12 views
2

以下のコードではあなたを怖がらせないでください。問題は本当にシンプルですが、2行だけで問題が発生しています。jquery:変数NaNを返します

なぜコードでNaNエラーコードが生成されましたか?私は別の変数の値を1つ引くことを試みているので、要素の位置は正しいでしょう。

変数はjQueryのposition()から値を得ました。これは整数であるはずです。

チェック行これらの行:

// 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; 

完全なコード:

<!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> 

答えて

4

問題は、drappable.drop()が、 draggable.stop()の前にと呼ばれていることです。あなたのStopposはまだ計算されていません。

これを処理する方法の1つは、ドラッグされているアイテムを単純に追跡し、その位置をdroppable.drop()で計算することです。例えば(あなたのコードのサブセット)、 "ドラッグ"オブジェクトに気づいてください。

$(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; 
       } 
     }); 

しかし、おそらくこれを回避するためのいくつかの正当な方法があります。

+0

StopposとStartposを配列に初期化する際のポイントは何ですか?それらを初期化しないままにするか、またはnullに設定するのはなぜですか? – jlarson

+0

Hmmm ...私を困惑させるのは、置き換えます: var posTop = Startpos.top - Stoppos.top; VAR posLeft = Startpos.left - Stoppos.left; 付: VAR術後= Startpos.top; VAR posLeft = Startpos.left; 数字が私のものではないにもかかわらず動作します。 StopposとStartposを初期化する – Cudos

+0

は、それらをグローバルスコープで利用できるようにします。しかし、私はjavascriptでグローバル変数に新しいです。ちょうどうまくいったものを試しました。 – Cudos

1

私の知る限りは()jQueryの関数ではない位置を知っています。

は、代わりにこれを試してみてください:

Startpos = $(this).offset(); 

その後、上部と左のプロパティにアクセスすることができるはずです。

+0

実際にはposition()を使用することができますリンク:http://docs.jquery.com/CSS/position 要素の位置 – Cudos

0

問題の原因となっているのはこれらのラインですか?

が術後に有効な整数/ダブル値を返します。 - - "Stoppos.topするvar術後= Startpos.top"

あなたがいることを確認してくださいすることができますか?私は先週私のプロジェクトの1つをコーディングしていて、同じNaN問題を抱えていました。Integerに値を解析しようとしましたが、うまくいきました。

あなたは数学的な計算を行っているので、あなたがそれらを引く前に、ダブル/ INTに値を解析してみてください、

時には、解決策は非常に単純であり、我々は以上のことを複雑にする傾向があります。

解析が機能しない場合は、配列から正しい値を渡していない可能性があります。 Startpos [0]のようなものを試してください。トップ

これが役に立ったら、幸運を祈る!

+0

私は運がないparseInt()を試しました:var posTop = parseInt(Startpos.top - Stoppos.top);またはvar posTop = parseInt(Startpos.top) - parseInt(Stoppos.top); – Cudos

0

この計算を実行するとき、StartposまたはStopposはまだ空の配列です。

代わりに適切なデフォルト(0、0)に設定してください。これにより、迷惑行為を簡単に追跡できるようになります。

0

何を私にパズルこれが機能することである:

は交換してください:付き

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

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

する数字は私が欲しいものでなくても動作します。シンプルな減算が何らかの理由で無効であるようです。

+0

Stoppos.top&Stoppos.leftは常に数値として初期化されていますか? –

+0

はいそうです。問題は他のところです。 – Cudos

0

助けを借りてくれてありがとう。

問題が見つかりました。

Stoppos = $(this).position(); 

にすべきではない:

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

しかし、その代わりに:

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

完全なコード:

<!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> 

は、今私はちょうどどのように把握する必要があり取得するためそれらが複数回移動されたときの画像のプレースメント位置:相対値がdraggablesに自動的に追加され、位置の計算に問題があるようです:(

関連する問題