[英]set position, top and left for a draggable object
I have a draggable object set to position:relative but I need to change it from relative to absolute only when I drag it and I want the object to stay in its initial space. 我将一个可拖动对象设置为position:relative,但是仅当我拖动它并且希望该对象保持在其初始空间时,才需要将其从相对更改为绝对。 I tried to set "drag" in this way:
我试图通过这种方式设置“拖动”:
$('obj').draggable({
cursor: 'move',
revert: true,
drag: function () {
var posizione = $(this).position();
var posizione_x = posizione.left;
var posizione_y = posizione.top;
$(this).css({
"left": posizione_x,
"top": posizione_y,
"position": "absolute"
});
},
stop: function () {
$(this).css("position", "relative");
}
})
or in this way: 或以这种方式:
$('obj').draggable({
cursor: 'move',
revert: true,
drag: function () {
var posizione_x = $(this).offsetLeft;
var posizione_y = $(this).offsetTop;
$(this).css({
"left": posizione_x,
"top": posizione_y,
"position": "absolute"
});
},
stop: function () {
$(this).css("position", "relative");
}
})
but it doesn't work. 但这不起作用。
I think you need .offset().left
and .offset().top
我认为您需要
.offset().left
和.offset().top
Could you detail "does not work" (error in the console? what happend? how is positionned the element?) 您能否详细说明“不起作用”(控制台中的错误?发生了什么?该元素的位置如何?)
And $('obj')
target a <obj>
tag, are you certain you element is well targeted? 并且
$('obj')
以<obj>
标记为目标,您确定您的元素有针对性吗?
Maybe you try to do something like that: http://jsfiddle.net/KyleKatarn/awjh79uw/1/ 也许您尝试做这样的事情: http : //jsfiddle.net/KyleKatarn/awjh79uw/1/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.