English 中文(简体)
script.aculo.us - Drag & Drop
  • 时间:2024-12-24

script.aculo.us - Drag & Drop


The most popular feature of Web 2.0 interface is the drag and drop facipty. Fortunately script.aculo.us comes with an inherent capabipty to support drag and drop.

To use the dragging capabipties of script.aculo.us, you ll need to load the dragdrop module, which also requires the effects module. So your minimum loading for script.aculo.us will look pke this:

<script type = "text/javascript" src = "/javascript/prototype.js"></script>
<script type = "text/javascript"  src = "/javascript/scriptaculous.js?load = effects,dragdrop"></script>

Dragging Things Around

It is very simple to make an item draggable using script.aculo.us. It requires creating an instance of the Draggable class, and identifying the element to be made draggable.

Draggable Syntax

new Draggable( element, options );

The first parameter to the constructor identifies the element to be made draggable either as the id of the element, or a reference to the element. The second parameter specifies optional information on how the draggable element is to behave.

Draggable Options

You can use one or more of the following options while creating your draggable object.

Option Description Examples
revert If set to true, the element returns to its original position when the drag ends. Also specifies whether the reverteffect callback will be invoked when the drag operation stops. Defaults to false.

Example

snap Used to cause a draggable to snap to a grid or to constrain its movement. If false (default), no snapping or constraining occurs.

    If it is assigned an integer x, the draggable will snap to a grid of x pixels.

    If an array [x, y], the horizontal dragging will snap to a grid of x pixels and the vertical will snap to y pixels.

    It can also be a function conforming to Function( x , y , draggable ) that returns an array [x, y].

Example

zindex Specifies the CSS z-index to be appped to the element during a drag operation. By default, the element s z-index is set to 1000 while dragging.

Example

ghosting Boolean determining whether the draggable should be cloned for dragging, leaving the original in place until the clone is dropped. Defaults to false.

Example

constraint A string used to pmit the draggable directions, either horizontal or vertical. Defaults to null which means free movement.

Example

handle Specifies an element to be used as the handle to start the drag operation. By default, an element is its own handle.

Example

starteffect An effect called on element when dragging starts. By default, it changes the element s opacity to 0.2 in 0.2 seconds.

Example

reverteffect An effect called on element when the drag is reverted. Defaults to a smooth spde to element s original position.Called only if revert is true.

Example

endeffect An effect called on element when dragging ends. By default, it changes the element s opacity to 1.0 in 0.2 seconds.

Example

Callback Options

Additionally, you can use any of the following callback functions in the options parameter −

Function Description Examples
onStart Called when a drag is initiated.

Example

onDrag Called repeatedly when a mouse moves, if mouse position changes from previous call.

Example

change Called just as onDrag (which is the preferred callback).

Example

onEnd Called when a drag is ended.

Example

Except for the "change" callback, each of these callbacks accepts two parameters: the Draggable object, and the mouse event object.

Draggable Example

Here, we define 5 elements that are made draggable: three <span> elements, an <img> element, and a <span> element. The purpose of the three different <span> elements is to demonstrate that regardless of whether an element starts off with a positioning rule of static (the default), relative, or absolute, the drag behavior is unaffected.

<html>
   <head>
      <title>Draggables Elements</title>
		
      <script type = "text/javascript" src = "/javascript/prototype.js"></script>
      <script type = "text/javascript" src = "/javascript/scriptaculous.js"></script>
      
      <script type = "text/javascript">
         // Take all the elements whatever you want to make Draggable.
         var elements = [ normalspan ,  relativespan ,  absolutespan ,  image ,  span ];
			
         // Make all the items drag able by creating Draggable objects
         window.onload = function() {
            elements.each(function(item) { new Draggable(item, {});});
         }			
      </script>
   </head>

   <body>
      <span id = "normalspan">
         This is a normal span and this is dragable.
      </span>

      <span id = "relativespan" style="position: relative;">
         This is a relative span and this is dragable.
      </span>

      <span id = "absolutespan" style="position: absolute;">
         This is an absolute span and this dragable.
      </span>
      <br />
		
      <img id = "image" src = "/images/scriptaculous.gif"/>

      <p>Let part <span id = "span" style = "color: blue;"> 
         This is middle part</span> Yes, only middle part is dragable.</p>
   </body>
</html>

This will produce following result −