DHTML Lab: Dynamic Page Segments; The Stylesheet
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. Border/ 1. 2. 3. 4. 5. 6. 7. 8. | The External ScriptWhen our script is loaded into our page in the HEAD of our document, it first creates the CSS STYLE rules that define the positioned elements. Two classes of elements are created, segment, for all page segments, and last, for any possible footer. Unlike previous examples of dynamic style creation, this one has several declarations based on conditions: semi = ";"; styStr = "<STYLE TYPE='text/css'>" styStr += ".segment{"; styStr += "position:absolute;"; styStr += "visibility:hidden;"; if (NS4) styStr += "width:" // keep with next line + (innerWidth-leftPos-margRight) + semi; if (borWid > 0) { styStr += "border-width:" + borWid + semi; styStr += "border-color:" + borCol + semi; styStr += "border-style:" + borSty + semi; } if (NS4) { if (borWid > 0 && padding <= 3) styStr += "padding: 0;"; if (borWid > 0 && padding > 3) styStr += "padding:" + (padding-3) + semi; if (borWid == 0) styStr += "padding:" + padding + semi; } else { styStr += "padding:" + padding + semi; } if (backCol != null) { if (NS4) { if (borWid > 0 && padding <= 3) { styStr +=// keep with next line "layer-background-color:" + backCol + semi; } if (borWid > 0 && padding > 3) { styStr += "background-color:" + backCol + semi; } if (borWid == 0) { styStr +=// keep with next line "layer-background-color:" + backCol + semi; } } else { styStr += "background-color:" + backCol + semi; } } styStr += "}"; styStr += ".last{position:absolute;visibility:hidden}"; styStr += "</STYLE>"; document.write(styStr); The .segment ClassSince we are defining positioned elements, the first two declaration are for position and visibility. We need all our segments to be initially hidden, to allow for their overlapping positioning and footer placement, before the user sees them. The width property For Navigator, we simply take the window's innerWidth, and subtract from that the horizontal space we require on the left (leftPos) and right (margRight). Explorer can assign a value to the width property at any time, so we will do it programmatically later. If, however, we wanted to assign a value at this time, using the same method as Navigator, it would be difficult, because we cannot yet determine the inner width of the client window. In the past, we have used the Explorer property, document.body.clientWidth, as the equivalent of Navigator's window.innerWidth. This is true, only if the page body has started loading. Navigator bases its property on the window, which, of course, always exists. Explorer bases its property on the page body width, derived from the BODY tag, which in this case, because the STYLE is in the HEAD of the document, has not yet been parsed. The border property The padding problem in Navigator In our previous column, Cross-Browser Hierarchical Menus, we illustrated and discussed the 3-pixel addition to element dimensions when a border is assigned. We also demonstrated the two background color options available, how they affect padding and the final display. If you are not familiar with this problem, please take the time to read the relevant page in the above article. A supplementary illustration is provided in the left column. In our style definition above, we apply padding to the segment elements, in Navigator, depending on the border variable and the padding variable: if (NS4) { if (borWid > 0 && padding <= 3) styStr += "padding: 0;"; if (borWid > 0 && padding > 3) styStr += "padding:" + (padding-3) + semi; if (borWid == 0) styStr += "padding:" + padding + semi; } else { styStr += "padding:" + padding + semi; } If we have specified a border, then three extra pixels of padding are automatically added, so we add padding only if more than three are called for, and then it is always three less. This will make our Navigator and Explorer elements look the same. If there is no border then padding is added normally. In the case of Explorer, the padding works as expected. The background-color problem in Navigator if (backCol != null) { if (NS4) { if (borWid > 0 && padding <= 3) { styStr +=// keep with next line "layer-background-color:" + backCol + semi; } if (borWid > 0 && padding > 3) { styStr += "background-color:" + backCol + semi; } if (borWid == 0) { styStr +=// keep with next line "layer-background-color:" + backCol + semi; } } else { styStr += "background-color:" + backCol + semi; } } The left column illustrates all the possible combinations. Our script allows only those that will display well. Explorer handles the background-color property as expected. The .last ClassIf we have a footer in our document, we have assigned a class of last to it, as you recall. The only properties necessary are position and visibility. The initial visibility value is hidden, since this element will be the last to be positioned, and the user must be spared unnecessary "jerking" of the elements on-screen as they move to their final positions. The remainder of our external script contains functions. The first to execute, as we know, is initIt(), called just before the page has finished loading. |
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/segBG.html