如何获取div的坐标?
我使用下面的代码来获取删除元素的位置。我将位置保存到数据库中,以便当用户下次打开页面时,元素位于相同的位置。到目前为止它工作正常,但是当屏幕尺寸改变时,位置也会改变。例如,如果我使用大桌面屏幕,捕获图像位置,将其保存到数据库并在笔记本电脑[小屏幕硬本]中打开同一页面,则立场各有不同。
我想这是因为 JQuery 获取元素相对于页面的位置,而不是相对于我的容器“div”。那么如何获取div中元素的位置呢?
$(document).ready(function() {
//Counter
counter = 0;
//Make element draggable
$("#fd img").draggable({
helper: 'clone',
containment: '#frame',
//When first dragged
stop: function(ev, ui) {
var pos = $(ui.helper).offset();
objName = "#clonediv" + counter
$(objName).css({ "left": pos.left, "top": pos.top });
$(objName).removeClass("drag");
//When an existiung object is dragged
var container = $('#frame');
$(objName).draggable({
containment: container,
stop: function(ev, ui) {
var pos = $(ui.helper).offset();
//console.log($(this).attr("id"));
//console.log(pos.left)
//console.log(pos.top)
}
});
}
});
//Make element droppable
$("#frame").droppable({
drop: function(ev, ui) {
var angle;
if (document.getElementById('angle').value != "") {
angle = document.getElementById('angle').value;
document.getElementById('angle').value = 0;
}
else {
angle = 0;
}
调试器 if (ui.helper.attr('id').search(/drag[0-9]/) != -1) {
if (ui.helper.attr('id').indexOf("clonediv") != -1) {
var deleteimage = 0;
var id = ui.helper.attr('id').substring(8);
saveCoords(ui.absolutePosition.left, ui.absolutePosition.top, angle, id, document.getElementById("trafficID").value, deleteimage);
}
else {
var pos = $(ui.helper).offset();
counter++;
var element = $(ui.helper).clone();
//var element = element1.resizable();
element.addClass("tempclass");
$(this).append(element);
var deleteimage = 0;
element.attr("id", ui.helper.attr('id') + counter);
**saveCoords(ui.absolutePosition.left, ui.absolutePosition.top, angle, ui.helper.attr('id') + counter, document.getElementById("trafficID").value, deleteimage);**
$(".tempclass").attr("id", "clonediv" + ui.helper.attr('id') + counter);
$("#clonediv" + ui.helper.attr('id') + counter).removeClass("tempclass");
//$(".tempclass").attr("onclick",function(){ $(this).remove(););
//$("#clonediv" + counter).removeClass("tempclass");
//Get the dynamically item id
draggedNumber = ui.helper.attr('id').search(/drag([0-9])/);
itemDragged = "dragged" + RegExp.$1;
//console.log(itemDragged)
//alert('left' + pos.left + ',top' + pos.top + 'of item' + itemDragged);
$("#clonediv" + ui.helper.attr('id') + counter).addClass(itemDragged);
}
}
}
});
//Make the element resizable
});
谢谢,
I am using the below code to get the position of dropped elements. I am saving the position to database,so that when the user opens the page next time, the elements are in the same position. It works fine so far, but when the screen size changes the position are changed.for example if i am using big desktop screen ,capture the image position ,save it to database and open the same page in laptop[small screen toughbook] ,the position are varied.
I guess this is bacause the JQuery gets the position of elements relative to page ,not to my container "div ". So how to get position of elements with in a div.
$(document).ready(function() {
//Counter
counter = 0;
//Make element draggable
$("#fd img").draggable({
helper: 'clone',
containment: '#frame',
//When first dragged
stop: function(ev, ui) {
var pos = $(ui.helper).offset();
objName = "#clonediv" + counter
$(objName).css({ "left": pos.left, "top": pos.top });
$(objName).removeClass("drag");
//When an existiung object is dragged
var container = $('#frame');
$(objName).draggable({
containment: container,
stop: function(ev, ui) {
var pos = $(ui.helper).offset();
//console.log($(this).attr("id"));
//console.log(pos.left)
//console.log(pos.top)
}
});
}
});
//Make element droppable
$("#frame").droppable({
drop: function(ev, ui) {
var angle;
if (document.getElementById('angle').value != "") {
angle = document.getElementById('angle').value;
document.getElementById('angle').value = 0;
}
else {
angle = 0;
}
debugger
if (ui.helper.attr('id').search(/drag[0-9]/) != -1) {
if (ui.helper.attr('id').indexOf("clonediv") != -1) {
var deleteimage = 0;
var id = ui.helper.attr('id').substring(8);
saveCoords(ui.absolutePosition.left, ui.absolutePosition.top, angle, id, document.getElementById("trafficID").value, deleteimage);
}
else {
var pos = $(ui.helper).offset();
counter++;
var element = $(ui.helper).clone();
//var element = element1.resizable();
element.addClass("tempclass");
$(this).append(element);
var deleteimage = 0;
element.attr("id", ui.helper.attr('id') + counter);
**saveCoords(ui.absolutePosition.left, ui.absolutePosition.top, angle, ui.helper.attr('id') + counter, document.getElementById("trafficID").value, deleteimage);**
$(".tempclass").attr("id", "clonediv" + ui.helper.attr('id') + counter);
$("#clonediv" + ui.helper.attr('id') + counter).removeClass("tempclass");
//$(".tempclass").attr("onclick",function(){ $(this).remove(););
//$("#clonediv" + counter).removeClass("tempclass");
//Get the dynamically item id
draggedNumber = ui.helper.attr('id').search(/drag([0-9])/);
itemDragged = "dragged" + RegExp.$1;
//console.log(itemDragged)
//alert('left' + pos.left + ',top' + pos.top + 'of item' + itemDragged);
$("#clonediv" + ui.helper.attr('id') + counter).addClass(itemDragged);
}
}
}
});
//Make the element resizable
});
Thanks,
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
offset
方法为您提供相对于文档的位置,因此您可能需要position
方法来代替,它为您提供相对于偏移量父级的位置:在您的代码中,您根本没有使用 pos 变量,这也是它不起作用的原因。
The
offset
method gets you the position relative to the document, so you probably want theposition
method instead, that gives you the position relative to the offset parent:In your code you are not using the
pos
variable at all, which also is a reason that it doesn't work.不知道你的代码到底是如何工作的,但我认为这可以解决问题:
Don't know exactly how your code works, but I think this will do the trick: