MooTools - 拖放



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

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

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 选项

Drag.Move 提供以下选项来维护具有拖放功能的 html 元素:

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

  • **container** - 这可以帮助您设置拖动元素的容器(保持元素在容器内)。

  • **snap** - 这可以帮助您设置用户必须拖动光标多少像素才能开始拖动可拖动元素。默认值为 6,您可以将其设置为任何表示数字的变量。

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

请查看以下语法,了解如何在何处定义可放置元素和容器、snap 和 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 事件提供不同的函数,这些函数可以在操作的不同级别使用。例如,当您开始拖动或放置对象时,每个 Drag.Move 事件都将拖动的元素或放置的元素作为参数传递。

以下是支持的事件:

onStart()

在拖动开始时触发此事件。如果您设置了较长的 snap 值,则此事件直到鼠标达到一定距离才会触发。请查看以下语法。

语法

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
   }
});

onLeave()

当可拖动元素离开可放置元素的边界时触发此事件。请查看以下语法。

语法

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 指的是当您放置一个可放置元素时,无论您是否落在可放置元素中,它都会触发。请查看以下语法。

语法

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。在这个例子中,我们提供了一个句柄,您可以使用它将可拖动对象拖动到容器中的任何位置。对于每个操作,左侧都会有通知(以蓝色表示)。容器中有一个可放置区域。如果可拖动对象进入可放置区域,则最后三个指示器将被激活。请查看以下代码。

示例

<!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>

您将收到以下输出,您必须点击句柄并拖动它。您现在可以在左侧找到通知指示。

输出

广告