Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/364.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 更改拖动对象并在拖放对象后将其还原_Javascript_Jquery_Jquery Ui_Draggable_Droppable - Fatal编程技术网

Javascript 更改拖动对象并在拖放对象后将其还原

Javascript 更改拖动对象并在拖放对象后将其还原,javascript,jquery,jquery-ui,draggable,droppable,Javascript,Jquery,Jquery Ui,Draggable,Droppable,请帮助我,我一般知道如何使用Dragable和Dropable类,但我找不到实现这一点的方法: 我有一个大尺寸的图像,我需要拖放到一个div中 1) 拖动时,我想使用一个小尺寸的图像,而不是在大尺寸的图像上移动(我已经有了它,只需要更改src) 2) 一旦它到达目标div,我想隐藏拖动的图像,并在其原始位置再次显示大尺寸图像 唯一的限制是:“恢复:无效”必须适用 这是我的代码: $("#big_img").draggable({ revert: 'invalid', drag

请帮助我,我一般知道如何使用Dragable和Dropable类,但我找不到实现这一点的方法:

我有一个大尺寸的图像,我需要拖放到一个div中

1) 拖动时,我想使用一个小尺寸的图像,而不是在大尺寸的图像上移动(我已经有了它,只需要更改src)

2) 一旦它到达目标div,我想隐藏拖动的图像,并在其原始位置再次显示大尺寸图像

唯一的限制是:“恢复:无效”必须适用

这是我的代码:

$("#big_img").draggable({ 
    revert: 'invalid', 
    drag : function(e, ui){
        //Change big image with a small version of it
        $(this).attr("src").replace("/Large/","/Small/"); //<--this do nothing
    }
});
$("#target").droppable({
    drop: function(e, ui) {
        alert("was added"); //<-- no problem here.
        //Restore the big_img
    }
});
$(“#big_img”).draggable({
还原:“无效”,
拖动:函数(e、ui){
//用它的小版本更改大图像
$(this.attr(“src”).replace(“/Large/”,“/Small/”);//我想我解决了这个问题:

使用“帮助器”,我可以实现使用其他图像,如下所示:

$("#big_img").draggable({ 
    helper: return $("<img src='"+$(this).attr("src").replace("/Large/","/Small/")+"' />");
});
$(“#big_img”).draggable({
助手:返回$(“”);
});
我只需要将它放在鼠标光标的中心,但我认为这不会有问题。然后, 删除删除的元素也不会有问题。因此,我不需要恢复图像,因为它不是真正移动的。:)

如果您还有其他选择,我很乐意阅读。

我想我已经解决了:

使用“帮助器”,我可以实现使用其他图像,如下所示:

$("#big_img").draggable({ 
    helper: return $("<img src='"+$(this).attr("src").replace("/Large/","/Small/")+"' />");
});
$(“#big_img”).draggable({
助手:返回$(“”);
});
我只需要将它放在鼠标光标的中心,但我认为这不会有问题。然后, 删除删除的元素也不会有问题。因此,我不需要恢复图像,因为它不是真正移动的。:)


如果您还有其他选择,我很乐意阅读。

String.prototype.replace()
不会修改源字符串,但会返回一个新的修改过的字符串。请尝试以下操作:

$("#big_img").draggable({ 
    revert: 'invalid', 
    drag : function(e, ui) {
        $this = $(this);
        $this.attr("src", $this.attr("src").replace("/Large/","/Small/"));
    }
});

String.prototype.replace()
不修改源字符串,但返回一个新的修改过的字符串。请尝试以下操作:

$("#big_img").draggable({ 
    revert: 'invalid', 
    drag : function(e, ui) {
        $this = $(this);
        $this.attr("src", $this.attr("src").replace("/Large/","/Small/"));
    }
});

要使鼠标光标居中,请使用:$(“#big_img”).draggable({cursorAt:{left:20,top:15},helper:…});在我的例子中,小图像是40x30,所以我想将它们居中于x=20,y=15。我希望这能对某人有所帮助。:)要使鼠标光标居中,请使用:$(“#big#img”).draggable({cursorAt:{left:20,top:15},helper:…);在我的例子中,小图像是40x30,所以我想将它们放在x=20,y=15的中心。我希望这能对某人有所帮助。:)谢谢你的评论…我稍后会测试它。但是对于上述问题,我认为使用助手比我最初更改图像的方法要好得多。主要是因为它也解决了我的其他问题。谢谢你的评论…我稍后会测试它。但是对于上面描述的问题,我认为使用助手比我最初更改图像的方法要好得多。主要是因为它也解决了我的其他问题。