2

I am trying to get the position of an element dropped onto the canvas using the following code.

var x = $(".partitiondrop").position();
alert("Top position: " + x.top + "\nLeft position: " + x.left);

The above works fine. I would like to know if I can get the Right and Bottom positions in the same way so that I can have the area bound by the element as I need to check which elements fall inside this element.

Nayantara Jeyaraj
  • 2,406
  • 5
  • 30
  • 55

5 Answers5

3

U can always add width to x.left position and add height to x.top position.

Verri
  • 1,351
  • 13
  • 19
3
var $partitiondrop = $(".partitiondrop");
var position = $partitiondrop.position();
position.bottom = position.top + $partitiondrop.height();
position.right = position.left + $partitiondrop.width();
alert("Top position: " + position.top + "\nLeft position: " + position.left + "\nBottom position: " + position.bottom + "\nRight position: " + position.right);
Verri
  • 1,351
  • 13
  • 19
metal03326
  • 1,189
  • 2
  • 12
  • 15
1

To get the position of any element by its ID you can do the following

var elementID; //Already obtained via attr(id) method or so
var $element = $("#" + elementID);
var position = $element.position();
position.bottom = position.top + $element.height();
position.right = position.left + $element.width();

var top_position=position.top;
var left_position=position.left;
var bottom_position=position.bottom;
var right_position=position.right;
1

In Jquery, it can be done using the following code

var p = $( "elementId" );
var position = p.position();
$( "p:last" ).text( "left: " + position.left + ", top: " + position.top );
0

Right Position:

$(window).width() - ($('.partitiondrop').offset().left + $('.partitiondrop').width());

Bottom Position:

$(window).height() - $('.partitiondrop').offset().top;
titan
  • 656
  • 6
  • 18