Javascript 设置可拖动div的位置

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7664945/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 03:06:52  来源:igfitidea点击:

Set position of draggable div

javascriptjqueryjquery-uijquery-ui-draggable

提问by Andres

I'm doing a little application using jQuery and JQuery ui.

我正在使用 jQuery 和 JQuery ui 做一个小应用程序。

I've defined a dragable div, and it works perfectly, but I want to set the position of the div when a click a button...

我已经定义了一个可拖动的 div,它工作得很好,但是我想在单击按钮时设置 div 的位置......

I read here in stackOverflow to do this:

我在这里阅读 stackOverflow 来做到这一点:

element.position().top = topUserDefined;
element.position().left = leftUserDefined;

I did an alert of element.position().top before and after this assignment and the new value it's not assigned, it keeps the original one...

我在此分配之前和之后发出了 element.position().top 警报,并且未分配新值,它保留了原始值...

Any Idea??? Thanks!

任何的想法???谢谢!

回答by Mohsen

.position()gets the position. It doesn't set the position. It actually uses element.offsetLeftand element.offsetTop(compared to element.offsetParentposition) those are read only.

.position()得到位置。它不设置位置。它实际上使用element.offsetLeftelement.offsetTop(与element.offsetParent位置相比)那些是只读的。

Change inline CSS value to move your element:

更改内联 CSS 值以移动您的元素:

element.css({'top': 10, 'left' : 20})