Monday 15 February 2010

javascript - compare position of multiple divs -


I have a dragable object when the status of objects is omitted, then it is similar to another device, a warning message Should be visible. Example:

  & lt; Div id = "ha" & gt; 1 & lt; / Div & gt; & Lt; Div id = "ha2" square = "box" & gt; 2 & lt; / Div & gt; & Lt; Div id = "ha3" square = "box" & gt; 3 & lt; / Div & gt; & Lt; Div id = "ha4" square = "box" & gt; 4 & lt; / Div & gt;   

JS:

  var posbox = $ (". Box"). Event (); Var boxbound = posbox.left; $ ("#h"). Draggable ({drag: function () {var pos = $ (this) .position (); var left = pos.left; if (left == boxleft) {warning ("similar");}}});   

The problem is that it only compares the first box with the class box. The number of divisions can change from time to time, so I can not put each status in a string and then compare the dragable to each of them. Is there any way to do this?

You can do this -

  var posbox = $ ( ". Box"). Map (function () {return $ (this). Position () .left;}) Get (); $ ("#h"). Draggable ({drag: function () {var pos = $ (this) .position (); var left = pos.left; var p = $ .inArray (left, posbox); if (P & gt; -1) { Console.log (":" + $ ('. Box'). Eq (p) .text ());}}});   

Demo --- & gt;

No comments:

Post a Comment