DHTML Jigsaw Puzzle: IE4; Grabbing Pieces | WebReference

DHTML Jigsaw Puzzle: IE4; Grabbing Pieces


The DHTML Lab Jigsaw Puzzle, Part II: IE4 cont'd
grabbing the puzzle pieces

Drag & Drop

We have discussed drag and drop concepts extensively in columns 6 and 7. Complete drag and drop code for Explorer 4 was developed in column 7. Here we will discuss only the modifications necessary for the puzzle.

As you will recall, the drag and drop code for Explorer took this form:

    currentX = currentY = 0; // vars for tracking mouse position whichEl = null; // var for tracking dragged element function grabEl() { // statements to execute when user grabs an element (mousedown) // go here } function moveEl() { // statements to execute when user drags an element (mousemove) // go here } function checkEl() { if (whichEl!=null) { return false } // don't select parts of document // if drag is in progress } function dropEl() { // statements to execute when user drops an element (mousedown) // go here } document.onmousemove = moveEl; // define which functions document.onselectstart = checkEl; // should be called document.onmousedown = grabEl; // for which events document.onmouseup = dropEl;

Grabbing the Puzzle or Puzzle Piece

We will modify the grabEl() function in two places. We gave all draggable elements in the puzzle a draggable property. This applies to the puzzle itself and all puzzle pieces that are created. The value of this property is true or false, depending on whether the element should be dragged or not. In the case of the full puzzle, the value is toggled by the drag toggle button in the controls. Puzzle pieces cannot be dragged if they are placed in their correct position in the puzzle. We will be discussing routines to determine this further down. Since we are concerned with a true value for the draggable property, we modify grabEl() to search it through the element heirarchy that exists under the mousedown:

    function grabEl() { whichEl = event.srcElement; while (!whichEl.draggable) { // identify element to be dragged whichEl = whichEl.parentElement; // by finding true value for draggable if (whichEl == null) { return } } if (whichEl != elPuzzle && whichEl != activeEl) { // exclude full puzzle whichEl.style.zIndex = activeEl.style.zIndex + 1; activeEl = whichEl; } whichEl.style.pixelLeft = whichEl.offsetLeft; whichEl.style.pixelTop = whichEl.offsetTop; currentX = (event.clientX + document.body.scrollLeft); currentY = (event.clientY + document.body.scrollTop); }

We also make a change in the code that places the dragged element on top of other draggable elements. When the full puzzle is dragged, no puzzle pieces are visible, as it can only be dragged in a solved form. Therefore, the full puzzle element is excluded from the z-index incrementation.

The user can now continue to attempt a solve by moving the piece.

Produced by Peter Belesis and

All Rights Reserved. Legal Notices.
Created: Nov. 13, 1997
Revised: Jan. 18, 1998

URL: http://www.webreference.com/dhtml/column9/puzzGrab.html