jQuery:停止拖动元素的绝对定位?

发布于 2024-12-11 15:06:36 字数 1234 浏览 0 评论 0原文

我是一个完全的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

来世叙缘 2024-12-18 15:06:36

您可以尝试使用 $(x).css() 来操作 drop 后的 CSS......

You can try using $(x).css() to manipulate CSS after the drop.....

涙—继续流 2024-12-18 15:06:36

没有必要让它变得比现在更复杂。
删除 CSS 似乎是个坏主意。

请参阅:http://jsfiddle.net/MPy9n/6/

draggableOptions = { appendTo: "body", zIndex: 99};

$( ".favrecentbox" ).draggable(draggableOptions);

$( ".favrecentboxessection" ).droppable({
    activeClass: "ui-state-default",
    hoverClass: "ui-state-hover",
    accept: ":not(.ui-sortable-helper)",
    drop: function( event, ui ) {
        console.log(ui);
        $( ui.draggable ).remove();
        $( '<div class="favrecentbox"></div>' ).html( ui.draggable.html() ).draggable(draggableOptions).appendTo( this );
    }});

No need to make it more complicated than it already is.
Removing CSS seems like a bad idea.

See: http://jsfiddle.net/MPy9n/6/

draggableOptions = { appendTo: "body", zIndex: 99};

$( ".favrecentbox" ).draggable(draggableOptions);

$( ".favrecentboxessection" ).droppable({
    activeClass: "ui-state-default",
    hoverClass: "ui-state-hover",
    accept: ":not(.ui-sortable-helper)",
    drop: function( event, ui ) {
        console.log(ui);
        $( ui.draggable ).remove();
        $( '<div class="favrecentbox"></div>' ).html( ui.draggable.html() ).draggable(draggableOptions).appendTo( this );
    }});
软甜啾 2024-12-18 15:06:36

您可以在放置时将其作为子项添加到可放置容器中。

$('.favrecentboxessection').droppable( {
        accept: ".favrecentbox",
        drop: function(ev, ui) {
            $(this).append($(ui.draggable));
            $(ui.draggable).css({position:'relative'});
        },
        activeClass: 'ui-state-highlight',
            drop: function (event, ui) {
            handleDropEvent (ui.draggable);
            }
    } );

you could add it as a child to the droppable container when dropped.

$('.favrecentboxessection').droppable( {
        accept: ".favrecentbox",
        drop: function(ev, ui) {
            $(this).append($(ui.draggable));
            $(ui.draggable).css({position:'relative'});
        },
        activeClass: 'ui-state-highlight',
            drop: function (event, ui) {
            handleDropEvent (ui.draggable);
            }
    } );
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文