jQuery拖拽&删除模拟插件

jQuery Drag & Drop Simulate Plugin

本文关键字:模拟 插件 删除 拖拽 jQuery      更新时间:2023-09-26

提前感谢你看了这个!我在模拟阻力时遇到了麻烦。jQuery UI通过扩展插件提供的drop特性:

https://github.com/j-ulrich/jquery-simulate-ext/blob/master/doc/drag-n-drop.md

文档使用了以下内容,我对其进行了修改以满足我的需要:

$('#draggableDiv').simulate("drag", {dragTarget: otherDiv});
Changed to this:
$('#placeme').simulate("drag", {dragTarget: $("#page1")});

我的警报消息从未弹出,因此很明显,drop功能从未被触发,我不知道为什么。手动拖动对象到page1的行为正常。

JSFiddle:http://jsfiddle.net/d2fdvxhz/8/

HTML:

<div class="container">
    <div id="view-port">
        <div id="placeme" class="droppableShape">
            <img src="https://upload.wikimedia.org/wikipedia/en/6/6f/Smiley_Face.png" width="25" height="25" />
        </div>
        <button id="testme">Simulate Drop</button>
        <div class="page" id="page1" style="background-image: url(http://images.huffingtonpost.com/2012-08-15-1CanvasPanelInstall5psd.jpg);"></div>
        <!-- Pages Here -->
    </div>
</div>

JS:

$(document).ready(function(){
     $(".droppableShape").draggable({
            helper:'clone'
     });
    $(".page").droppable({
        accept: ".droppableShape",
        tolerance: 'fit',
        drop: function(event,ui){
            alert("Drop Detected");
            var new_field = $(ui.helper).clone().removeClass('droppableShape');
            var droppable_page = $(this);
            var droppableOffset = $(this).offset();
            new_field.css('top', ui.position.top - droppableOffset.top);
            new_field.css('left', ui.position.left - droppableOffset.left);
            // Set Draggable Options
            new_field.draggable({
                containment: droppable_page,
                stop: function(event, ui) {
                    // Save position after dragging stops
                    $(this).data("x_cord", ui.position.left);
                    $(this).data("y_cord", ui.position.top);
                    $(this).draggable( "disable" );
                }
            });
            // Add to drop area
            $(this).append(new_field);
        }
    });
    $( "#testme" ).click(function() {
        $('#placeme').simulate("drag", {
            dragTarget: $("#page1")
        });
        console.log("Simulate Attempted");
    });
});

你应该使用

$('#placeme').simulate("drag-n-drop", {

如这里所示

工作小提琴

如果你只模拟一个拖放,则永远不会发生拖放