DHTML Lab: Dynamic Page Segments; Segment Display
Dynamic Page Segments
| |||
Our technique uses this column's front page as a live example. If you wish to follow the tutorial with the front page visible for reference, you may view it: in a new window (all) When you are done, you may: In longer script listings, cross-browser code is blue, Navigator-specific code is red, and Explorer code is green. | Displaying the Page SegmentRecall that showSegment() is called not only by initIt() upon page load, but also by the onClick handlers in the document itself. For example: <A HREF="#Intro" onClick="showSegment('Intro');return false"> Introduction</A> It is therefore written to account for an already visible page segment. The first time it is called, by initIt(), there is no visible segment. The second and third statements of the function are redundant this once, but apply if showSegment() is called from the in-page links later: function showSegment(el){ whichSeg = eval(prefix+el); currentSeg.showIt(false); if (hasFooter) footerEl.showIt(false); whichSeg.showIt(true); currentSeg = whichSeg; if (hasFooter) setTimeout("whichSeg.arrangeIt();",50) } The statements are straight-forward:
Why are we calling arrangeIt() with a setTimeout()? The Explorer 4 Note, below, after arrangeIt() is discussed, explains. Displaying the Footerfunction arrangeIt() { if (NS4) { footerEl.top = // keep with next line this.pageY + this.document.height + footerElSpace; } else { footerEl.style.pixelTop = // keep with next line this.style.pixelTop + this.offsetHeight + footerElSpace; } footerEl.showIt(true); } The arrangeIt() method of all segment elements, simply positions the footer element below the segment, using the usual browser-specific properties and adding the vertical space we assigned to footerElSpace. Once the footer is positioned, it is made visible. The user can now navigate the page segments by clicking the links defined in your page. The visibility-swapping and positioning routines are simply repeated for every segment called for by the user. Explorer 4 Note: Whichever way you look at it, a problem is a problem. We need to place the footer element below the currently visible segment element. We must know where the latter is positioned and what its height is. Even though the segment element is positioned and visible, Explorer returns incorrect values for offsetHeight, scrollHeight and clientHeight, properties that relate to the element's screen presence, if we retrieve them immediately. If we wait, the values are correct. Therefore, we wait by calling arrangeIt(), which needs the offsetHeight property, with the setTimeout() method. Originally, we had set the millisecond value of setTimeout() to 1, and on most attempts, this was enough. A value of 50, however, seemed the safest. This is not a large interval (1/20th of a second), and almost indiscernible, so we did not apply a conditional for Explorer use. It does not adversely affect Navigator in any way. showIt()We mustn't forget our standard element showIt() method, which toggles an element's visibility: function showIt(on) { if (NS4) { this.visibility = (on) ? "show" : "hide" } else { this.style.visibility = (on) ? "visible" : "hidden" } } That's it. Simple enough, and hopefully useful. The next page is another live example of this technique. |
Produced by Peter Belesis and
All Rights Reserved. Legal Notices.Created: Mar. 11, 1998
Revised: Mar. 12, 1998
URL: https://www.webreference.com/dhtml/column16/segArr.html