[英]How to get distance in pixels of element inside of parent div
so I'm using Jquery and pure js to write small application, I have a element inside of a div parent.This Draggable element (ui widget) that can be moved around in div parent.I would like to get exact amount of pixels from the top and from the left. 所以我使用Jquery和纯js编写小型应用程序,我在div父级中有一个元素。这个Draggable元素(ui小部件)可以在div父级中移动。我想从中获取确切的像素数量顶部和左侧。
I have following code right now, but with this code I can't get exact ammount of pixels from top and bottom, 我现在有以下代码,但是使用此代码,我无法从顶部和底部获得确切的像素数量,
$('#dragThis').draggable(
{
drag: function(){
var parentOffset =$(this).parent().offset();
var offset = $(this).offset();
var xPos = offset.left - parentOffset.left ;
var yPos = offset.top - parentOffset.top ;
$('#posX').text('x: ' + xPos);
$('#posY').text('y: ' + yPos);
},
containment: "#parentDiv"
});
Working example: http://jsfiddle.net/8fbdf93m/ 工作示例: http : //jsfiddle.net/8fbdf93m/
Please help. 请帮忙。
Try Jquery.Position method. 尝试使用Jquery.Position方法。 It allows you to retrieve the current position of an element relative to the offset parent 它允许您检索元素相对于偏移父级的当前位置
you can also try $(this).css('left')
or $(this).css('top')
您也可以尝试$(this).css('left')
或$(this).css('top')
updated fiddle http://jsfiddle.net/8fbdf93m/4/ 更新小提琴http://jsfiddle.net/8fbdf93m/4/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.