MooTools - 拖放


MooTools 提供了一个强大的功能,可以帮助您向网页元素添加拖放功能。我们可以通过创建自己的新Drag.Move对象来做到这一点。使用此对象,您可以定义选项和事件。Drag 和 Drag.Move 类来自 MooTools More 库。

让我们讨论一下 Drag.Move 对象的选项和事件。

拖动移动

Drag.Move 是一个用于向 html 元素添加拖放功能的对象。Drag.Move扩展了Drag,因此我们可以通过Drag.Move对象使用Drag类的所有选项和事件。查看以下语法并了解如何使用 Drag.Move 对象。

句法

var myDrag = new Drag.Move(dragElement, {
   // Drag.Move Options
   droppables: dropElement,
   container: dragContainer,
   
   // Drag Options
   handle: dragHandle,

   // Drag.Move Events
   // the Drag.Move events pass the dragged element,
   // and the dropped into droppable element
   onDrop: function(el, dr) {
      //will alert the id of the dropped into droppable element
      alert(dr.get('id'));
   },
   // Drag Events
   // Drag events pass the dragged element
   onComplete: function(el) {
      alert(el.get('id'));
   }
});

拖移选项

Drag.Move 提供以下选项来通过拖放功能维护 html 元素 -

  • droppable - 这可以帮助您设置可放置元素(在与放置相关的事件上注册的元素)的选择器。

  • 容器- 这可以帮助您设置拖动元素的容器(将元素保留在内部)。

  • snap - 这可以帮助您设置在可拖动元素开始拖动之前用户必须拖动光标的像素数。默认值为 6,您可以将其设置为代表数字的任意数量的变量。

  • handle - 这可以帮助您向可拖动元素添加句柄。句柄成为唯一接受抓取的元素。

查看以下语法,了解如何以及在何处定义可放置元素、容器元素、捕捉元素和句柄元素。

句法

//here we define a single element by id
var dragElement = $('drag_element');

//here we define an array of elements by class
var dropElements = $$('.drag_element');
var dragContainer = $('drag_container');
var dragHandle = $('drag_handle');

//now we set up our Drag.Move object
var myDrag = new Drag.Move(dragElement , {
   // Drag.Move Options
   // set up our droppables element with the droppables var we defined above
   droppables: dropElements ,
   
   // set up our container element with the container element var
   container: dragContainer
   
   // set up pixels the user must drag.
   Snap: 10
   
   // Adds a handle to your draggable element
   handle: dragHandle
});

拖动.移动事件

Drag.Move 事件提供了可在不同级别的操作中使用的不同功能。例如,当您开始拖放对象时,每个 Drag.Move 事件都会将拖动的元素或放下的元素作为参数传递。

以下是支持的事件 -

onStart()

这会在拖动开始时引发一个事件。如果您设置了长捕捉,则直到鼠标处于一定距离时才会引发此事件。看看下面的语法。

句法

var myDrag = new Drag.Move(dragElement , {
   // Drag options will pass the dragged element as a parameter
   onStart: function(el) {
      // put whatever you want to happen on start in here
   }
});

onDrag()

当您拖动元素时,这会连续引发事件。看看下面的语法。

句法

var myDrag = new Drag.Move(dragElement , {
   // Drag options will pass the dragged element as a parameter
   onDrag: function(el) {
      // put whatever you want to happen on drag in here
   }
});

onDrop()

当您将可拖动元素放入可放置元素时,这会引发一个事件。看看下面的语法。

句法

var myDrag = new Drag.Move(dragElement , {
   // It will pass the draggable element ('el' in this case)
   // and the droppable element the draggable is interacting with ('dr' here)
   onDrop: function(el, dr) {
      // put whatever you want to happen on drop in here
   }
});

休假()

当可拖动元素离开可放置元素的边界时,这会引发一个事件。看看下面的语法。

句法

var myDrag = new Drag.Move(dragElement , {
   // It will pass the draggable element ('el' in this case)
   // and the droppable element the draggable is interacting with ('dr' here)
   onLeave: function(el, dr) {
      // put whatever you want to happen on Leave from droppable area in here
   }
});

onEnter()

当可拖动元素进入可放置元素区域时会引发此问题。看看下面的语法。

句法

var myDrag = new Drag.Move(dragElement , {
   // It will pass the draggable element ('el' in this case)
   // and the droppable element the draggable is interacting with ('dr' here)
   onEnter: function(el, dr) {
      // this will fire when a draggable enters a droppable element
   }
});

onComplete()

这引发了一个事件。onComplete 指的是当你放下一个 droppable 时,无论你是否落在一个 droppable 中,它都会引发。看看下面的语法。

句法

var myDrag = new Drag.Move(dragElement , {
   // Drag Options
   // Drag options will pass the dragged element as a parameter
   onComplete: function(el) {
      // put whatever you want to happen on complete
   }
});

让我们举一个例子来探讨本章中解释的所有功能。这些功能包括:Drag、Drag.Move、onEnter、onLeave、onDrop、onStart、onDrag 和 onComplete。在此示例中,我们提供了一个句柄,使用它您可以将可拖动对象拖动到容器中的任何位置。对于每个操作,左侧都会有一个通知(以蓝色表示)。容器中有一个可放置区域。如果 Draggable 对象进入 Droppable 区域,则最后三个指示器被激活。看看下面的代码。

例子

<!DOCTYPE html>
<html>

   <head>
      <style>
         /* this is generally a good idea */
         body {
            margin: 0;
            padding: 0;
         }
         
         /* make sure the draggable element has "position: absolute"
         and then top and left are set for the start position */
         #drag_me {
            width: 100px;
            height: 100px;
            background-color: #333;
            position: absolute;
            top: 0;
            left: 0;
         }
         #drop_here {
            width: 80%;
            height: 200px;
            background-color: #eee;
            margin-left: 100px;
            margin-top: -200px !important;
         }
         /* make sure the drag container is set with position relative */
         #drag_cont {
            background-color: #ccc;
            height: auto;
            width: 500px;
            position:relative;
            margin-top: 20px;
            margin-left: 20px;
            margin-bottom: auto;
         }
         #drag_me_handle {
            width: 100%;
            height: auto;
            background-color: #F5B041;
         }
         #drag_me_handle span {
            display: block;
            padding: 20px;
         }
         .indicator {
            width: 100px;
            height: auto;
            background-color: #0066FF;
            border-bottom: 1px solid #eee;
         }
         .indicator span {
            padding: 10px;
            display: block;
         }
         .draggable {
            width: 200px;
            height: 200px;
            background-color: blue;
         }
      </style>
      
      <script type = "text/javascript" src = "MooTools-Core-1.6.0.js"></script>
      <script type = "text/javascript" src = "MooTools-More-1.6.0.js"></script>
      
      <script type = "text/javascript">
         window.addEvent('domready', function() {
            var dragElement = $('drag_me');
            var dragContainer = $('drag_cont');
            var dragHandle = $('drag_me_handle');
            var dropElement = $$('.draggable');
            var startEl = $('start');
            var completeEl = $('complete');
            var dragIndicatorEl = $('drag_ind');
            var enterDrop = $('enter');
            var leaveDrop = $('leave');
            var dropDrop = $('drop_in_droppable');
            
            var myDrag = new Drag.Move(dragElement, {
               // Drag.Move options
               droppables: dropElement,
               container: dragContainer,
               
               // Drag options
               handle: dragHandle,
               
               // Drag.Move Events
               onDrop: function(el, dr) {
                  if (!dr) { }else {
                     dropDrop.highlight('#FB911C'); //flashes orange
                     el.highlight('#fff'); //flashes white
                     dr.highlight('#667C4A'); //flashes green
                  };
               },
               onLeave: function(el, dr) {
                  leaveDrop.highlight('#FB911C'); //flashes orange
               },
               onEnter: function(el, dr) {
                  enterDrop.highlight('#FB911C'); //flashes orange
               },
               
               // Drag Events
               onStart: function(el) {
                  startEl.highlight('#FB911C'); //flashes orange
               },
               onDrag: function(el) {
                  dragIndicatorEl.highlight('#FB911C'); //flashes orange
               },
               onComplete: function(el) {
                  completeEl.highlight('#FB911C'); //flashes orange
               }
            });
         });
      </script>
   </head>
   
   <body>
   
      <p align = "center">Drag and Drop Application</p>
      <div id = "drag_cont">
         <div id = "start" class = "indicator"><span>Start</span></div>
         <div id = "drag_ind" class = "indicator"><span>Drag</span></div>
         <div id = "complete" class = "indicator"><span>Complete</span></div>
         <div id = "enter" class = "indicator"><span>Enter Droppable Element</span></div>
         <div id = "leave" class = "indicator"><span>Leave Droppable Element</span></div>
         
         <div id = "drop_in_droppable" class = "indicator">
            <span>Dropped in Droppable Element</span>
         </div>
         
         <div id = "drag_me">
            <div id = "drag_me_handle"><span>HANDLE</span></div>
         </div>
         
         <div id = "drop_here" class = "draggable">
            <p align = "center">Droppable Area</p>
         </div>
         
      </div>
   </body>
   
</html>

您将收到以下输出,其中您必须单击“处理”并拖动它。您现在可以在左侧找到通知指示。

输出