jQuery jquery可拖动启用和禁用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13629980/
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-26 12:49:05  来源:igfitidea点击:

jquery draggable enable and disable

jquerydraggable

提问by Gleiemeister 2000

Ive tried anything to do this, but always get the same error

我试过任何方法来做到这一点,但总是得到同样的错误

$(".tooltip").draggable('disable');

Error: cannot call methods on draggable prior to initialization; attempted to call method 'disable'

错误:无法在初始化之前调用可拖动的方法;试图调用方法“禁用”

Prior to initialization? So I cannot remove the option before its actually being dragged around? Ive tried with droppable as well, cant seem to get them disabled enabled without getting this error.

初始化之前?所以我不能在它实际被拖动之前删除该选项?我也尝试过使用 droppable,似乎无法在没有收到此错误的情况下禁用它们。

edit: I found out that I have an element with the class that is without draggable (which makes sense when you look at the error). Now I just have to find a way so it disables all the draggables without throwing the error :)

编辑:我发现我有一个类的元素是不可拖动的(当你查看错误时这是有道理的)。现在我只需要找到一种方法,它就可以禁用所有可拖动对象而不会抛出错误:)

回答by Barmar

Try this:

尝试这个:

$(".tooltip").draggable({ disabled: true });

This initializes the draggable in the disabled state. You can then use

这将在禁用状态下初始化可拖动。然后你可以使用

$(".tooltip").draggable("enable");

later when you want to allow dragging.

稍后当您想要允许拖动时。

回答by Don Boots

I was able to recreate your scenario in this jsfiddle:

我能够在这个 jsfiddle 中重新创建你的场景:

http://jsfiddle.net/dboots/NDZKY/

http://jsfiddle.net/dboots/NDZKY/

This is using

这是使用

$('.draggable').draggable('disable');

and

$('.draggable').draggable('enable');

Is there something different that you're not able to do the same? Looks like you have the same syntax.

有什么不同的东西是你不能做的吗?看起来你有相同的语法。

回答by Luca Davanzo

Take a look at the documentation http://api.jqueryui.com/draggable/

看一下文档http://api.jqueryui.com/draggable/

and a example here:

和一个例子:

http://jqueryui.com/draggable/

Have you "included" jquery-ui.js library?

您是否“包含”了 jquery-ui.js 库?

try on this fiddle! http://jsfiddle.net/3Lrg2/23/

试试这个小提琴!http://jsfiddle.net/3Lrg2/23/

回答by Photon Khan

Use one buttonto enableand disable

使用一个按钮启用禁用

$("#container").draggable({disabled:false});
$("#container").draggable({disabled:true});

Simple Example Given Below:

下面给出的简单示例:

<div id = "container"></div>
<button id = "control"></button>
<script>
  $(document).ready(function()
    {
       $("#control").click(function()
          {
            if($("#control").hasClass("active")
              {
                $("container").draggable({disabled:true});
                $("control").removeAttr("class","active");   
              }
            else
              {
                $("container").draggable({disabled:false});
                $("control").attr("class","active");
              }
          });
    });
</script> 

回答by Alex_B

try using .unbind('ondrag') to remove the event from your element.

尝试使用 .unbind('ondrag') 从元素中删除事件。

sorry I think it should be $("tooltip").unbind('draggable') not 100% sure on event name.

抱歉,我认为它应该是 $("tooltip").unbind('draggable') 不是 100% 确定事件名称。