jQuery:停止拖动元素的绝对定位?
我是一个完全的 jQuery 和 Javascript 菜鸟,试图让一个非常简单的拖放现场演示工作。我已经很接近了,但是我拖动的元素被赋予了绝对定位,因此它们不能很好地流动在一起。
标记非常简单:
<section class="favrecentboxessection" id="favorites">
little divs with the style set to draggable
</section>
<section class="favrecentboxessection" id="recents">
where they are dragged to
</section>
还有我悲伤的小 jQuery:
$( init );
var $favorites = $( "#favorites" ),
$recent = $( "#recent" );
function init() {
$('.favrecentbox').draggable( {
cursor: 'move',
containment: '#mainsection',
stack: '.favrecentbox',
revert: 'invalid',
revertDuration: 200,
} );
$('.favrecentboxessection').droppable( {
accept: ".favrecentbox",
activeClass: 'ui-state-highlight',
drop: function (event, ui) {
handleDropEvent (ui.draggable);
}
} );
function handleDropEvent( $item ) {
var temp;
temp = $item.detach();
temp.appendTo( $('#favorites'));
我已经可以进行拖动了,但是当拖放时,它们会保留绝对定位(设置了左侧、顶部等)。我该如何关闭它?
I am a complete jQuery and Javascript noob trying to get a very simple drag and drop live demo working. I am getting close but my dragged elements are given absolute positioning, so they don't flow nicely together.
Markup is very simple:
<section class="favrecentboxessection" id="favorites">
little divs with the style set to draggable
</section>
<section class="favrecentboxessection" id="recents">
where they are dragged to
</section>
And my sad little jQuery:
$( init );
var $favorites = $( "#favorites" ),
$recent = $( "#recent" );
function init() {
$('.favrecentbox').draggable( {
cursor: 'move',
containment: '#mainsection',
stack: '.favrecentbox',
revert: 'invalid',
revertDuration: 200,
} );
$('.favrecentboxessection').droppable( {
accept: ".favrecentbox",
activeClass: 'ui-state-highlight',
drop: function (event, ui) {
handleDropEvent (ui.draggable);
}
} );
function handleDropEvent( $item ) {
var temp;
temp = $item.detach();
temp.appendTo( $('#favorites'));
I've got the dragging working, but when dropped they carry over absolute positioning (left, top, etc are set). How do I turn that off?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以尝试使用 $(x).css() 来操作 drop 后的 CSS......
You can try using $(x).css() to manipulate CSS after the drop.....
没有必要让它变得比现在更复杂。
删除 CSS 似乎是个坏主意。
请参阅:http://jsfiddle.net/MPy9n/6/
No need to make it more complicated than it already is.
Removing CSS seems like a bad idea.
See: http://jsfiddle.net/MPy9n/6/
您可以在放置时将其作为子项添加到可放置容器中。
you could add it as a child to the droppable container when dropped.