简体   繁体   English

如何根据选中的“复选框”禁止“var drag”?

[英]How to Disallow `var drag` depending on `checkbox` checked?

Mootools : How to Allow and Disallow var drag depending on checkbox checked or not? MooTools的:如何允许和禁止var drag根据checkbox选中或不?

window.addEvent('domready',function() {
  var z = 2;
  $$('#dragable').each(function(e) {
    var drag = new Drag.Move(e,{
      grid: false,
      preventDefault: true,
      onStart: function() {
        e.setStyle('z-index',z++);
      }
    });
  });
});
function check(tag){
  if(tag.checked){
    //checkbox checked
    //How to Disallow Drag.Move for #dragable ?
    //Unfortunately so it does not work - drag.destroy(); drag.removeEvents();
  }else{
    //How to Allow Drag.Move for #dragable ?
  }
}
<input type="checkbox" onClick="check(this);">
<div id="dragable">Drag-able DIV</div>

Store the instance of Drag in MooTools Element Store so when the checkbox is clicked, we can retrieve this instance and manipulate it. Drag的实例存储在 MooTools Element Store 中,因此当单击复选框时,我们可以检索此实例并对其进行操作。

Drag.Move is an extension to the base Drag class, and if you see the docs, you will notice it has two methods for this situation: Drag.Move是对基本Drag类的扩展,如果您查看文档,您会注意到它有两种用于这种情况的方法:

You need to call these methods on the drag object that gets created when you call new Drag.Move(..) to enable or disable dragging.您需要在调用new Drag.Move(..)以启用或禁用拖动时创建的拖动对象上调用这些方法。

So first create the drag object as you are already doing:所以首先创建拖动对象,因为你已经在做:

var drag = new Drag.Move(e, {
    ...
});

And then store a reference of this drag object inside the Element Store for later retrieval.然后将这个drag对象的引用存储在 Element Store 中以供以后检索。

e.store('Drag', drag);

You can use any key you want here - I've used "Drag" .你可以在这里使用任何你想要的键 - 我用过"Drag"

Then later in the check function, retrieve the drag object, and call attach or detach on it depending on the state of the checkbox.然后在 check 函数中,检索拖动对象,并根据复选框的状态对其调用attachdetach

function check(elem) {
    var drag = elem.retrieve('Drag'); // retrieve the instance we stored before

    if(elem.checked) {
        drag.detach(); // disable dragging
    }
    else {
        drag.attach(); // enable dragging
    }
}

See your example modified to work this the checkbox.请参阅修改后的示例以使用此复选框。

On a side note, if you are retrieving an element by id, you don't need to use $$ as ideally there should only be only element with that id.附带说明一下,如果您通过 id 检索元素,则不需要使用$$因为理想情况下应该只有具有该 id 的元素。 $$("#dragable") is just too redundant and less performant. $$("#dragable")过于冗余且性能较低。 Use document.id('dragable') or $("dragable") instead.改用document.id('dragable')$("dragable")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM