admin管理员组

文章数量:1394985

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.

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.

Share Improve this question asked Aug 4, 2016 at 9:17 Nayantara JeyarajNayantara Jeyaraj 2,7067 gold badges36 silver badges65 bronze badges 1
  • getBoundingCientRect() – caub Commented Aug 4, 2016 at 9:39
Add a ment  | 

5 Answers 5

Reset to default 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);

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

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;

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

Right Position:

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

Bottom Position:

$(window).height() - $('.partitiondrop').offset().top;

本文标签: jqueryGet Position of Element in JavascriptStack Overflow