Behaviors > Using the behavior actions that come with Dreamweaver > Drag Layer |
The Drag Layer action lets the visitor drag a layer. Use this action to create puzzles, slider controls, and other movable interface elements.
You can specify in which direction the visitor can drag the layer (horizontally, vertically, or in any direction), a target to which the visitor should drag the layer, whether to snap the layer to the target if the layer is within a certain number of pixels of the target, what to do when the layer hits the target, and more.
Because the Drag Layer action must be called before the layer can be dragged by the visitor, make sure the event that triggers the action occurs before the visitor attempts to drag the layer. It's best to attach Drag Layer to the body
object (with the onLoad
event), though you can also attach it to a link that fills the entire layer (such as a link around an image) using the onMouseOver
event.
To use the Drag Layer action:
1 | Choose Insert > Layer or click the Layer button on the Objects panel and draw a layer in the Document window's Design view. |
2 | Select the body tag by clicking <body> in the tag selector at the bottom of the Document window. |
3 | Open the Behaviors panel. |
4 | Click the Plus (+) button and choose Drag Layer from the Actionspop-up menu. |
If Drag Layer is unavailable, you probably have a layer selected. Because layers do not accept events in both 4.0 browsers, you must select a different objectsuch as the body tag or a link (a tag)or change the target browser to IE 4.0 in the Show Events For pop-up menu. |
|
5 | In the Layer pop-up menu, select the layer that you want to make draggable. |
6 | Choose either Constrained or Unconstrained from the Movement pop-up menu. |
Unconstrained movement is appropriate for puzzles and other drag-and-drop games. For slider controls and moveable scenery such as file drawers, curtains, and mini-blinds, choose constrained movement. | |
7 | For constrained movement, enter values (in pixels) in the Up, Down, Left, and Right fields. |
Values are relative to the starting position of the layer. To constrain movement within a rectangular region, enter positive values in all four fields. To allow only vertical movement, enter positive values for Up and Down and 0 for Left and Right. To allow only horizontal movement, enter positive values for Left and Right and 0 for Up and Down. | |
8 | Enter values (in pixels) for the drop target in the Left and Top fields. |
The drop target is the spot to which you want the visitor to drag the layer. A layer is considered to have reached the drop target when its left and top coordinates match the values you enter in the Left and Top fields. Values are relative to the top left corner of the browser window. Click Get Current Position to automatically fill the fields with the current position of the layer. | |
9 | Enter a value (in pixels) in the Snap if Within field to determine how close the visitor must get to the drop target before the layer snaps to the target. |
Larger values make it easier for the visitor to find the drop target. | |
10 | For simple puzzles and scenery manipulation, you can stop here. To define the drag handle for the layer, track the movement of the layer while it is being dragged, and trigger an action when the layer is dropped, click the Advanced tab. |
11 | To specify that the visitor must click a particular area of the layer to drag the layer, choose Area Within Layer from the Drag Handle pop-up menu; then enter the left and top coordinates and the width and height of the drag handle. |
This option is useful when the image inside the layer has an element that suggests dragging, such as a title bar or drawer handle. Do not set this option if you want the visitor to be able to click anywhere in the layer to drag it. | |
12 | Choose any While Dragging options that you want to use: |
Select Bring Layer to Front if the layer should move to the front of the stacking order while it is being dragged. If you select this option, use the pop-up menu to choose whether to leave the layer in front or restore it to its original position in the stacking order. | |
Enter JavaScript code or a function name (for example, monitorLayer() ) in the Call JavaScript field to repeatedly execute the code or function while the layer is being dragged. For example, you could write a function that monitors the coordinates of the layer and displays hints such as "you're getting warmer" or "you're nowhere near the drop target" in a text field. See Gathering information about the draggable layer. |
|
13 | Enter JavaScript code or a function name (for example, evaluateLayerPos() ) in the second Call JavaScript field to execute the code or function when the layer is dropped. Select Only if Snapped if the JavaScript should be executed only if the layer has reached the drop target. |
14 | Click OK. |
15 | Check that the default event is the one you want. |
If it isn't, choose another event from the pop-up menu. If the events you want are not listed, change the target browser in the Show Events For pop-up menu. Remember that layers are not supported by 3.0 browsers. | |
Note: You cannot attach the Drag Layer action to an object with the onMouseDown
or onClick
events.
Gathering information about the draggable layer
When you attach the Drag Layer action to an object, Dreamweaver inserts the MM_dragLayer()
function into the head
section of your document. In addition to registering the layer as draggable, this function defines three properties for each draggable layerMM_LEFTRIGHT
, MM_UPDOWN
, and MM_SNAPPED
that you can use in your own JavaScript functions to determine the relative horizontal position of the layer, the relative vertical position of the layer, and whether the layer has reached the drop target.
Note: The information provided here is intended for the use of experienced JavaScript programmers only.
For example, the following function displays the value of the MM_UPDOWN
property (the current vertical position of the layer) in a form field called curPosField
. (Form fields are useful for displaying continuously updated information because they are dynamicthat is, you can change their contents after the page has finished loadingin both Navigator and Internet Explorer.)
function getPos(layername){ var layerRef = MM_findObj(layername); var curVertPos = layerRef.MM_UPDOWN; document.tracking.curPosField.value = curVertPos; }
Instead of displaying the value of MM_UPDOWN
or MM_LEFTRIGHT
in a form field, you could write a function that displays a message in the form field depending on how close the value is to the drop zone, or you could call another function to show or hide a layer depending on the value. How you react to the value of MM_UPDOWN
or MM_LEFTRIGHT
is limited only by your imagination and your JavaScript skills.
It is especially useful to read the MM_SNAPPED
property when you have several layers on the page, all of which must reach their targets before the visitor can advance to the next page or task. For example, you could write a function to count how many layers have an MM_SNAPPED
value of true
and call it whenever a layer is dropped. When the snapped count reaches the desired number, you could send the visitor to the next page or display a message of congratulations.
If you have used the onMouseOver
event to attach the Drag Layer action to links within several layers, you must make a minor change to the MM_dragLayer()
function to prevent the MM_SNAPPED
property of a snapped layer from being reset to false
if the mouse pointer rolls over the layer. (This can happen if you have used Drag Layer to create a picture puzzle, because the visitor is likely to roll the mouse pointer over snapped pieces while positioning others.) The MM_dragLayer()
function does not prevent this behavior, because it is sometimes desirablefor example, if you want to set multiple drop targets for a single layer.
To prevent re-registration of snapped layers:
1 | Make a backup copy of your document before making any changes to the code. (You can do this in the Site window in Dreamweaver, or in Windows Explorer (Windows) or the Finder (Macintosh).) |
2 | Choose Edit > Find. |
3 | Choose HTML Source from the Find What pop-up menu. |
4 | Type (!curDrag) in the adjacent text field. |
5 | Click Find Next. |
If Dreamweaver asks if you want to continue searching from the beginning of the document, click Yes. Dreamweaver finds a statement that reads: | |
if (!curDrag) return false; |
|
6 | Close the Find dialog box and then modify the statement in the Document window's Code view or in the Code inspector so that it reads: |
if (!curDrag || curDrag.MM_SNAPPED != null) return false; |
|
The two pipes (|| ) mean "or," and curDrag is a variable that represents the layer that is being registered as draggable. In English the statement means "If curDrag is not an object, or if it already has an MM_SNAPPED value, don't bother executing the rest of the function." |
|