JavaScript DHTML/HTML/Layer
Содержание
- 1 Accessing Layers with JavaScript
- 2 Adjusting Layer clip Properties (W3C)
- 3 Layer Clipping Properties (W3C)
- 4 Layer vs. Clip Dimension Properties (W3C)
- 5 Dragging a Layer
- 6 Layer Background Colors (W3C)
- 7 JsLib 1.3
- 8 Setting the layer.style.visibility Property of Nested Layers (W3C)
- 9 layer.style.zIndex Property of Sibling Layers
- 10 Resizing a Layer (W3C)
- 11 Layer Backgrounds (W3C)
- 12 Nested Layer Coordinates (W3C)
Accessing Layers with JavaScript
<source lang="html4strict">
/* JavaScript Unleashed, Third Edition by Richard Wagner and R. Allen Wyke ISBN: 067231763X Publisher Sams CopyRight 2000
- /
<html> <head>
<title>JavaScript Unleashed</title> <style type="text/css"> </style> <script type="text/javascript" language="JavaScript1.2"> </script>
</head> <body onload="checkBrowser()">
DIV 1
<form name="form1"> <input type="button" value="Hide"Image from book onclick="changeState("layer1","hidden")"> <input type="button" value="Show"Image from book onclick="changeState("layer1","visible")"> <form>
</body> </html>
</source>
Adjusting Layer clip Properties (W3C)
<source lang="html4strict">
/* JavaScript Bible, Fourth Edition by Danny Goodman John Wiley & Sons CopyRight 2001
- /
<HTML> <HEAD> <TITLE>Layer Clip</TITLE> <SCRIPT LANGUAGE="JavaScript"> var origLayerWidth = 0 var origLayerHeight = 0 var currTop, currRight, currBottom, currLeft function init() {
origLayerWidth = parseInt(document.getElementById("display").style.width) origLayerHeight = parseInt(document.getElementById("display").style.height) currTop = 0 currRight = origLayerWidth currBottom = origLayerHeight currLeft = 0 showValues()
} function setClip(field) {
var val = parseInt(field.value) switch (field.name) { case "top" : currTop = val
break
case "right" : currRight = val break case "bottom" : currBottom = val break case "left" : currLeft = val break case "width" : currRight = currLeft + val break case "height" : currBottom = currTop + val break } adjustClip() showValues()
} function adjustClip() {
document.getElementById("display").style.clip = "rect(" + currTop + "px " + currRight + "px " + currBottom + "px " + currLeft + "px)"
} function showValues() {
var form = document.forms[0] form.top.value = currTop form.right.value = currRight form.bottom.value = currBottom form.left.value = currLeft form.width.value = currRight - currLeft form.height.value = currBottom - currTop
} var intervalID function revealClip() {
var midWidth = Math.round(origLayerWidth /2) var midHeight = Math.round(origLayerHeight /2) currTop = midHeight currBottom = midHeight currRight = midWidth currLeft = midWidth intervalID = setInterval("stepClip()",1)
} function stepClip() {
var widthDone = false var heightDone = false if (currLeft > 0) { currLeft += -2 currRight += 2 } else { widthDone = true } if (currTop > 0) { currTop += -1 currBottom += 1 } else { heightDone = true } adjustClip() showValues() if (widthDone && heightDone) { clearInterval(intervalID) }
} </SCRIPT> </HEAD> <BODY onLoad="init()">
Layer Clipping Properties (W3C)
Enter new clipping values to adjust the visible area of the layer.
<FORM>
layer.style.clip (left): | <INPUT TYPE="text" NAME="left" SIZE=3 onChange="setClip(this)"> |
layer.style.clip (top): | <INPUT TYPE="text" NAME="top" SIZE=3 onChange="setClip(this)"> |
layer.style.clip (right): | <INPUT TYPE="text" NAME="right" SIZE=3 onChange="setClip(this)"> |
layer.style.clip (bottom): | <INPUT TYPE="text" NAME="bottom" SIZE=3 onChange="setClip(this)"> |
layer.style.clip (width): | <INPUT TYPE="text" NAME="width" SIZE=3 onChange="setClip(this)"> |
layer.style.clip (height): | <INPUT TYPE="text" NAME="height" SIZE=3 onChange="setClip(this)"> |
<INPUT TYPE="button" VALUE="Reveal Original Layer" onClick="revealClip()"> </FORM>
ARTICLE I
<P> Congress shall make no law respecting an establishment of religion, or prohibiting the free exercise thereof; or
abridging the freedom of speech, or of the press; or the right of the people peaceably to assemble, and to petition the government for a redress of grievances.
</P>
</BODY> </HTML>
</source>
Comparison of Layer and Clip Location Properties (W3C)
<source lang="html4strict">
/* JavaScript Bible, Fourth Edition by Danny Goodman John Wiley & Sons CopyRight 2001
- /
<HTML> <HEAD> <TITLE>Layer vs. Clip</TITLE> <SCRIPT LANGUAGE="JavaScript"> var currClipTop = 0 var currClipLeft = 0 var currClipRight = 360 var currClipBottom = 180 function setClip(field) {
var val = parseInt(field.value) switch (field.name) { case "clipBottom" : currClipBottom = val break case "clipRight" : currClipRight = val break } adjustClip() showValues()
} function adjustClip() {
document.getElementById("display").style.clip = "rect(" + currClipTop + "px " + currClipRight + "px " + currClipBottom + "px " + currClipLeft + "px)"
} function setLayer(field) {
var val = parseInt(field.value) switch (field.name) { case "width" : document.getElementById("display").style.width = val + "px" break case "height" : document.getElementById("display").style.height = val + "px" break } showValues()
} function showValues() {
var form = document.forms[0] var elem = document.getElementById("display") var clipRect = getClipRect(elem) form.width.value = parseInt(elem.style.width) form.height.value = parseInt(elem.style.height) form.clipRight.value = clipRect.right form.clipBottom.value = clipRect.bottom
} // convert clip property string to an object function getClipRect(elem) {
var clipString = elem.style.clip // assumes "rect(npx, npx, npx, npx)" form // get rid of "rect(" clipString = clipString.replace(/rect\(/,"") // get rid of "px)" clipString = clipString.replace(/px\)/,"") // get rid of remaining "px" strings clipString = clipString.replace(/px/g,",") // turn remaining string into an array clipArray = clipString.split(",") // make object out of array values
var clipRect = {top:parseInt(clipArray[0]), right:parseInt(clipArray[1]),
bottom:parseInt(clipArray[2]), left:parseInt(clipArray[3])} return clipRect
} </SCRIPT> </HEAD> <BODY onLoad="showValues()">
Layer vs. Clip Dimension Properties (W3C)
Enter new layer and clipping values to adjust the layer.<P>
<FORM>
layer.style.width: | <INPUT TYPE="text" NAME="width" SIZE=3 onChange="setLayer(this)"> |
layer.style.height: | <INPUT TYPE="text" NAME="height" SIZE=3 onChange="setLayer(this)"> |
layer.style.clip (right): | <INPUT TYPE="text" NAME="clipRight" SIZE=3 onChange="setClip(this)"> |
layer.style.clip (bottom): | <INPUT TYPE="text" NAME="clipBottom" SIZE=3 onChange="setClip(this)"> |
</FORM>
ARTICLE I
<P> Congress shall make no law respecting an establishment of religion, or prohibiting the free exercise thereof; or abridging the freedom of speech, or of the press; or the right of the people peaceably to assemble, and to petition the government for a redress of grievances. </P>
</BODY> </HTML>
</source>
<source lang="html4strict">
/* JavaScript Unleashed, Third Edition by Richard Wagner and R. Allen Wyke ISBN: 067231763X Publisher Sams CopyRight 2000
- /
<html> <body>
<script language="JavaScript">
</script>
This is a block of moving buttons
<form>
<input type="button"
value="UP" onClick="moveUp()">
<input type="button"
value="DOWN" onCLick="moveDown()">
<input type="button"
value="LEFT" onClick="moveLeft()">
<input type="button"
value="RIGHT" onClick="moveRight()">
<input type="button"
value="SHOW/HIDE Text Box" onClick="showHide()">
</form>
<script language="JavaScript">
</script>
<script language="JavaScript">
</script>
<script language="JavaScript">
</script>
</body> </html>
</source>
Dragging a Layer (W3C)
<source lang="html4strict">
/* JavaScript Bible, Fourth Edition by Danny Goodman John Wiley & Sons CopyRight 2001
- /
<HTML> <HEAD> <TITLE>Layer Dragging</TITLE> <STYLE TYPE="text/css"> .draggable {cursor:hand} </STYLE> <SCRIPT LANGUAGE="JavaScript"> var engaged = false var offsetX = 0 var offsetY = 0 function dragIt(evt) {
evt = (evt) ? evt : (window.event) ? window.event : "" var targElem = (evt.target) ? evt.target : evt.srcElement if (engaged) { if (targElem.className == "draggable") { while (targElem.id != "myLayer" && targElem.parentNode) { targElem = targElem.parentNode } if (evt.pageX) { targElem.style.left = evt.pageX - offsetX + "px" targElem.style.top = evt.pageY - offsetY + "px" } else { targElem.style.left = evt.clientX - offsetX + "px" targElem.style.top = evt.clientY - offsetY + "px" }
return false
} }
} function engage(evt) {
evt = (evt) ? evt : (window.event) ? window.event : "" var targElem = (evt.target) ? evt.target : evt.srcElement if (targElem.className == "draggable") { while (targElem.id != "myLayer" && targElem.parentNode) { targElem = targElem.parentNode } if (targElem.id == "myLayer") { engaged = true if (evt.pageX) { offsetX = evt.pageX - targElem.offsetLeft offsetY = evt.pageY - targElem.offsetTop } else { offsetX = evt.offsetX - document.body.scrollLeft offsetY = evt.offsetY - document.body.scrollTop if (navigator.userAgent.indexOf("Win") == -1) { offsetX += document.body.scrollLeft offsetY += document.body.scrollTop } } return false } }
} function release(evt) {
evt = (evt) ? evt : (window.event) ? window.event : "" var targElem = (evt.target) ? evt.target : evt.srcElement if (targElem.className == "draggable") { while (targElem.id != "myLayer" && targElem.parentNode) { targElem = targElem.parentNode } if (engaged && targElem.id == "myLayer") { engaged = false } }
} </SCRIPT> </HEAD> <BODY>
Dragging a Layer
Drag me around the window.
</LAYER> <SCRIPT LANGUAGE="JavaScript"> document.onmousedown = engage document.onmouseup = release document.onmousemove = dragIt document.onmouseout = release </SCRIPT> </BODY> </HTML>
</source>
Hide and show layer
<source lang="html4strict">
/* JavaScript Application Cookbook By Jerry Bradenbaugh Publisher: O"Reilly Series: Cookbooks ISBN: 1-56592-577-7
- /
<HTML> <HEAD> <TITLE>dhtml.js example</TITLE> <SCRIPT LANGUAGE="JavaScript1.2"> // dhtml.js // Set browser-determined global variables var NN = (document.layers ? true : false); var hideName = (NN ? "hide" : "hidden"); var showName = (NN ? "show" : "visible"); var zIdx = -1; function genLayer(sName, sLeft, sTop, sWdh, sHgt, sVis, copy) {
if (NN) { document.writeln("<LAYER NAME="" + sName + "" LEFT=" + sLeft + " TOP=" + sTop + " WIDTH=" + sWdh + " HEIGHT=" + sHgt + " VISIBILITY="" + sVis + """ + " z-Index=" + zIdx + ">" + copy + "</LAYER>"); } else {document.writeln("
); } }
// Define a function to hide layers function hideSlide(name) {
refSlide(name).visibility = hideName; }
// Define a function to reveal layers function showSlide(name) {
refSlide(name).visibility = showName; }
// Define a central function to reference layers function refSlide(name) {
if (NN) { return document.layers[name]; } else { return eval("document.all." + name + ".style"); } }
</SCRIPT> </HEAD> <BODY> [<A HREF="javascript: hideSlide("myLayer");">Hide</A>] [<A HREF="javascript: showSlide("myLayer");">Show</A>] <SCRIPT LANGUAGE="JavaScript1.2">
</SCRIPT> </BODY> </HTML>
</source>
"layer" and "ilayer" Tag Properties
<source lang="html4strict">
Property Description above A Layer object higher in z-order of all layers in the document (null if the layer is topmost). background The URL of an image to use as the background for the layer. below A Layer object lower in z-order of all layers in the document (null if the layer is lowest). bgcolor The background color for the layer. clip Defines the clipping rectangle. Anything outside of this rectangle is clipped from view. height The height in pixels of the layer. left The x-axis position in pixels of the layer, relative to the origin of its parent layer. name The name of the layer. src The URL for the layer"s content source. top The y-axis position in pixels of the layer, relative to the origin of its parent layer. visibility Defines the layer"s visibility attributes. width The width in pixels of the layer. z-index The relative z-order of the layer, relative to its siblings and parent.
</source>
Layer Background Colors (W3C)
<source lang="html4strict">
/* JavaScript Bible, Fourth Edition by Danny Goodman John Wiley & Sons CopyRight 2001
- /
<HTML> <HEAD> <SCRIPT LANGUAGE="JavaScript"> function setColor(evt) {
evt = (evt) ? evt : (window.event) ? window.event : "" if (evt) { var elem = (evt.target) ? evt.target : evt.srcElement if (elem.className == "palette") { document.getElementById("display").style.backgroundColor = elem.style.backgroundColor } }
} document.onmouseover = setColor </SCRIPT> </HEAD> <BODY>
Layer Background Colors (W3C)
<SCRIPT LANGUAGE="JavaScript"> var oneLayer var colorTop = 100 var colorLeft = 20 var colorWidth = 40 var colorHeight = 40 var colorPalette = new Array("aquamarine","coral","forestgreen",
"goldenrod","red","magenta","navy","teal")
for (var i = 0; i < colorPalette.length; i++) {
oneLayer = "\n"document.write(oneLayer)
} </SCRIPT>
Some reversed text to test against background colors.
</BODY> </HTML>
</source>
Layer seek
<source lang="html4strict">
// Seek nested NN4 layer from string name function seekLayer(doc, name) {
var theObj; for (var i = 0; i < doc.layers.length; i++) { if (doc.layers[i].name == name) { theObj = doc.layers[i]; break; } // dive into nested layers if necessary if (doc.layers[i].document.layers.length > 0) { theObj = seekLayer(document.layers[i].document, name); } } return theObj;
} // Convert object name string or object reference // into a valid element object reference function getRawObject(obj) {
var theObj; if (typeof obj == "string") { if (isW3C) { theObj = document.getElementById(obj); } else if (isIE4) { theObj = document.all(obj); } else if (isNN4) { theObj = seekLayer(document, obj); } } else { // pass through object reference theObj = obj; } return theObj;
}
</source>
Layer "srcFilter" Example
<source lang="html4strict">
<html> <head> <script language="JavaScript"> function function1() { myLayer.filters[0].Apply();
if (myLayer.style.visibility == "visible") { myLayer.style.visibility = "hidden"; myLayer.filters.revealTrans.transition = 2; } else { myLayer.style.visibility = "visible"; myLayer.filters[0].revealTrans.transition = 3; } myLayer.filters[0].Play(); var m = window.event.srcFilter; alert(m);
} </script> </head> <body bottommargin="150">
<img src="http://www.wbex.ru/style/logo.png" width="99" height="75">
<input type="button" id="myButton" onClick="function1();" value="Play Transition"> </body> </html>
</source>
Methods and Properties of the Layer Object
<source lang="html4strict">
/* JavaScript Unleashed, Third Edition by Richard Wagner and R. Allen Wyke ISBN: 067231763X Publisher Sams CopyRight 2000
+---------+----------------+-------------------------------------------------+
Type Item Description
+---------+----------------+-------------------------------------------------+
Method
+---------+----------------+-------------------------------------------------+
captureEvents() Specifies the event types to capture.
+---------+----------------+-------------------------------------------------+
handleEvent() Invokes handler for specified event.
+---------+----------------+-------------------------------------------------+
load() Loads a new URL.
+---------+----------------+-------------------------------------------------+
moveAbove() Moves the layer above another layer.
+---------+----------------+-------------------------------------------------+
moveBelow() Moves the layer below another layer.
+---------+----------------+-------------------------------------------------+
moveBy() Moves the layer to a specified position.
+---------+----------------+-------------------------------------------------+
moveTo() Moves the top-left corner of the window to the specified screen coordinates.
+---------+----------------+-------------------------------------------------+
moveToAbsolute() Changes the layer position to the specified pixel coordinates within the page.
+---------+----------------+-------------------------------------------------+
releaseEvents() Sets the layer to release captured events of the specified type.
+---------+----------------+-------------------------------------------------+
resizeBy() Resizes the layer by the specified height and width values.
+---------+----------------+-------------------------------------------------+
resizeTo() Resizes the layer to have the specified height and width values.
+---------+----------------+-------------------------------------------------+
routeEvent() Passes a captured event along the normal event hierarchy.
+---------+----------------+-------------------------------------------------+
Property
+---------+----------------+-------------------------------------------------+
above Specifies the layer above.
+---------+----------------+-------------------------------------------------+
background Refers to the background image of the layer.
+---------+----------------+-------------------------------------------------+
below Specifies the layer below.
+---------+----------------+-------------------------------------------------+
bgColor Refers to the background color of the layer.
+---------+----------------+-------------------------------------------------+
clip.bottom Refers to the bottom of the layer"s clipping area.
+---------+----------------+-------------------------------------------------+
clip.height Refers to the height of the layer"s clipping area.
+---------+----------------+-------------------------------------------------+
clip.left Refers to the left of the layer"s clipping area.
+---------+----------------+-------------------------------------------------+
clip.right Refers to the right of the layer"s clipping area.
+---------+----------------+-------------------------------------------------+
clip.top Refers to the top of the layer"s clipping area.
+---------+----------------+-------------------------------------------------+
clip.width Refers to the width of the layer"s clipping area.
+---------+----------------+-------------------------------------------------+
document Refers to the Document object that contains the layer.
+---------+----------------+-------------------------------------------------+
left Refers to the x-coordinate of the layer.
+---------+----------------+-------------------------------------------------+
name Refers to the name of the layer.
+---------+----------------+-------------------------------------------------+
pageX Refers to the x-coordinate relative to the document.
+---------+----------------+-------------------------------------------------+
pageY Refers to the y-coordinate relative to the document.
+---------+----------------+-------------------------------------------------+
parentLayer Refers to the containing layer.
+---------+----------------+-------------------------------------------------+
siblingAbove Refers to the layer above in the zIndex.
+---------+----------------+-------------------------------------------------+
siblingBelow Refers to the layer below in the zIndex.
+---------+----------------+-------------------------------------------------+
src Refers to the source URL for the layer.
+---------+----------------+-------------------------------------------------+
top Refers to the y-coordinate of the layer.
+---------+----------------+-------------------------------------------------+
visibility Refers to the visibility state of the layer.
+---------+----------------+-------------------------------------------------+
window Refers to the Window or Frame object that contains the layer.
+---------+----------------+-------------------------------------------------+
x Refers to the x-coordinate of the layer.
+---------+----------------+-------------------------------------------------+
y Refers to the y-coordinate of the layer.
+---------+----------------+-------------------------------------------------+
z Refers to the relative z-order of this layer with respect to its siblings.
+---------+----------------+-------------------------------------------------+
- /
</source>
Monitors divisions (or layers) on dynamic Web pages (DHTML)
<source lang="html4strict">
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <HTML>
<HEAD> <TITLE>JsLib 1.3 - Exemple - dyna.js</TITLE> <META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1"> <META NAME="Author" CONTENT="Etienne CHEVILLARD"> <SCRIPT TYPE="text/javascript" LANGUAGE="Javascript">
/* dyna.js
* Role : controle les divisions des pages Web dynamiques * Projet : JsLib * Auteur : Etienne CHEVILLARD (echevillard@users.sourceforge.net) * Version : 1.3 * Creation : 07/07/2001 * Mise a jour : 23/02/2005 * Bogues connues : - Opera 5/6 ne gere pas la couleur de fond "transparent" * - Opera 5/6 ne gere pas la modification du contenu des divisions */
// --- Variables globales --- // choix du DOM (Document Object Model) var dyna_w3=0; // DOM-1 du W3C : Mozilla, Netscape 6/7, IE 5/6, Opera 7, Safari var dyna_ie=0; // IE 4 var dyna_nn=0; // Netscape Navigator 4 var dyna_op=0; // Opera 4/5/6 // determine le DOM utilise par le navigateur if (document.getElementById && document.getElementsByTagName)
dyna_w3=1;
else if (document.all && navigator.userAgent.toLowerCase().indexOf("opera")!=-1)
dyna_op=1;
else if (document.all)
dyna_ie=1;
else if (document.layers)
dyna_nn=1;
// recharge la page si redimensionnement de la fenetre sous Netscape 4 (bogue) window.onresize=function () {
if (dyna_nn) { history.go(0); }
} // --- Fonctions --- // parcoure les divisions du DOM Netscape 4 pour trouver celle souhaitee function dyna_obtenirDivNN4(objet, nom) {
var divs=objet.layers; var divTrouvee; for (var i=0; i<divs.length; i++) { if (divs[i].id==nom) divTrouvee=divs[i]; else if (divs[i].layers.length > 0) var tmp=dyna_obtenirDivNN4(divs[i], nom); if (tmp) divTrouvee=tmp; } return divTrouvee;
} // fin dyna_obtenirDivNN4(objet, nom) // indique si le navigateur accepte le DHTML function accepteDHTML() {
return (dyna_w3 || dyna_ie || dyna_nn || dyna_op);
} // fin accepteDHTML() // rend invisible la division specifiee function cacherDivision(id) {
var ldiv=obtenirDivision(id); if (!ldiv) return false; if (dyna_w3 || dyna_ie || dyna_op) ldiv.style.visibility="hidden"; else if (dyna_nn) ldiv.visibility="hide"; return true;
} // fin cacherDivision(id) // cree une nouvelle division et l"ajoute au DOM // -- merci a Victor Lopes (victor3.lopes@voila.fr) function creerDivision(id, x, y, largeur, hauteur) {
if ((!id) || (id=="") || (obtenirDivision(id))) return false; if (isNaN(x)) x=0; if (isNaN(y)) y=0; if (isNaN(largeur) || (largeur<0)) largeur=0; if (isNaN(hauteur) || (hauteur<0)) hauteur=0; if (dyna_w3 && document.createElement) { var ndiv=document.createElement("DIV"); ndiv.id=id; ndiv.style.position="absolute"; ndiv.style.visibility="hidden"; ndiv.style.left=x; ndiv.style.top=y; ndiv.style.width=largeur; ndiv.style.height=hauteur; ndiv.style.zIndex=0; document.body.appendChild(ndiv); } else if (dyna_ie || dyna_op) {var html="
document.body.insertAdjacentHTML("beforeEnd", html); } else if (dyna_nn){ document.layers[id]=new Layer(largeur); var ndiv=document.layers[id]; ndiv.visibility="hide"; ndiv.left=x; ndiv.top=y; ndiv.document.width=largeur; ndiv.document.height=hauteur; ndiv.clip.left=0; ndiv.clip.right=largeur; ndiv.clip.top=0; ndiv.clip.bottom=hauteur; ndiv.zIndex=0; } return true;
} // fin creerDivision(id, x, y, largeur, hauteur) // deplace la division specifiee du nombre de pixels specifie function deplacerDivisionDe(id, px, py) {
return (modifierPosGaucheDivision(id, parseInt(obtenirPosGaucheDivision(id)) + px) && modifierPosHautDivision(id, parseInt(obtenirPosHautDivision(id)) + py));
} // fin deplacerDivisionDe(id, px, py) // deplace la division specifiee vers les coordonnees specifiees function deplacerDivisionVers(id, x, y) {
return (modifierPosGaucheDivision(id, x) && modifierPosHautDivision(id, y));
} // fin deplacerDivisionVers(id, x, y) // modifie le code HTML contenu dans la division specifiee function modifierCodeDivision(id, code) {
var ldiv=obtenirDivision(id); if (!ldiv) return false; if (!code) code=""; if (navigator.userAgent.toLowerCase().indexOf("mac")!=-1) code+="\n"; if (dyna_w3) { ldiv.innerHTML=""; ldiv.innerHTML=code; } else if (dyna_ie || dyna_op) { ldiv.innerHTML=code; } else if (dyna_nn) { ldiv.document.open(); ldiv.document.write(code); ldiv.document.close(); } return true;
} // fin modifierCodeDivision(id, code) // modifie la couleur de fond de la division specifiee function modifierCouleurFondDivision(id, couleur) {
var ldiv=obtenirDivision(id); if (!ldiv) return false; if ((!couleur) || (couleur=="")) couleur="transparent"; if (dyna_op) ldiv.style.background=couleur; else if (dyna_w3 || dyna_ie) ldiv.style.backgroundColor=couleur; else if (dyna_nn) { if (couleur.toLowerCase()=="transparent") ldiv.bgColor=null; else ldiv.bgColor=couleur; } return true;
} // fin modifierCouleurFondDivision(id, couleur) // modifie les dimensions de la division specifiee function modifierDimensionsDivision(id, largeur, hauteur) {
return (modifierLargeurDivision(id, largeur) && modifierHauteurDivision(id, hauteur));
} // fin modifierDimensionsDivision(id, largeur, hauteur) // modifie la hauteur de la division specifiee function modifierHauteurDivision(id, hauteur) {
var ldiv=obtenirDivision(id); if (!ldiv) return false; if (isNaN(hauteur) || (hauteur<0)) hauteur=0; if (dyna_w3) ldiv.style.height=hauteur; else if (dyna_ie || dyna_op) ldiv.style.pixelHeight=hauteur; else if (dyna_nn) { ldiv.document.height=hauteur; ldiv.clip.top=0; ldiv.clip.bottom=hauteur; } return true;
} // fin modifierHauteurDivision(id, hauteur) // modifie l"image de fond de la division specifiee function modifierImageFondDivision(id, image) {
var ldiv=obtenirDivision(id); if (!ldiv) return false; if (dyna_w3 || dyna_ie || dyna_op) { if ((!image) || (image=="")) ldiv.style.backgroundImage="url(null)"; else ldiv.style.backgroundImage="url("+image+")"; } else if (dyna_nn) { ldiv.background.src=image; } return true;
} // fin modifierImageFondDivision(id, image) // modifie la largeur de la division specifiee function modifierLargeurDivision(id, largeur) {
var ldiv=obtenirDivision(id); if (!ldiv) return false; if (isNaN(largeur) || (largeur<0)) largeur=0; if (dyna_w3) ldiv.style.width=largeur; else if (dyna_ie || dyna_op) ldiv.style.pixelWidth=largeur; else if (dyna_nn) { ldiv.document.width=largeur; ldiv.clip.left=0; ldiv.clip.right=largeur; } return true;
} // fin modifierLargeurDivision(id, largeur) // modifie la position horizontale de la division specifiee function modifierPosGaucheDivision(id, x) {
var ldiv=obtenirDivision(id); if (!ldiv) return false; if (isNaN(x)) x=0; if (dyna_w3) ldiv.style.left=x; else if (dyna_ie || dyna_op) ldiv.style.pixelLeft=x; else if (dyna_nn) ldiv.left=x; return true;
} // fin modifierPosGaucheDivision(id, x) // modifie la position verticale de la division specifiee function modifierPosHautDivision(id, y) {
var ldiv=obtenirDivision(id); if (!ldiv) return false; if (isNaN(y)) y=0; if (dyna_w3) ldiv.style.top=y; else if (dyna_ie || dyna_op) ldiv.style.pixelTop=y; else if (dyna_nn) ldiv.top=y; return true;
} // fin modifierPosHautDivision(id, y) // modifie l"index de superposition de la division specifiee function modifierZIndexDivision(id, z) {
var ldiv=obtenirDivision(id); if (!ldiv) return false; if ((isNaN(z)) || (parseInt(z)<0)) z=0; if (dyna_w3 || dyna_ie || dyna_op) ldiv.style.zIndex=z; else if (dyna_nn) ldiv.zIndex=z; return true;
} // fin modifierZIndexDivision(id, z) // rend visible la division specifiee function montrerDivision(id) {
var ldiv=obtenirDivision(id); if (!ldiv) return false; if (dyna_w3 || dyna_ie || dyna_op) ldiv.style.visibility="visible"; else if (dyna_nn) ldiv.visibility="show"; return true;
} // fin montrerDivision(id) // retourne la couleur de fond de la division specifiee function obtenirCouleurFondDivision(id) {
var ldiv=obtenirDivision(id); if (!ldiv) return ""; if (dyna_op) return ldiv.style.background; else if (dyna_w3 || dyna_ie) return ldiv.style.backgroundColor; else if (dyna_nn) return ldiv.bgColor;
} // fin obtenirCouleurFondDivision(id) // retourne une reference sur la division d"identifiant specifie function obtenirDivision(id) {
if (dyna_w3) return document.getElementById(id); else if (dyna_ie || dyna_op) return document.all[id]; else if (dyna_nn) return dyna_obtenirDivNN4(document, id); return "";
} // fin obtenirDivision(id) // retourne la hauteur de la division specifiee // -- merci a Victor Lopes (victor3.lopes@voila.fr) function obtenirHauteurDivision(id) {
var ldiv=obtenirDivision(id); if (!ldiv) return 0; if (dyna_w3) { var re = /\D/g; return parseInt((ldiv.style.height).replace(re, "")); } else if (dyna_ie || dyna_op) return parseInt(ldiv.style.pixelHeight); else if (dyna_nn) return parseInt(ldiv.clip.bottom);
} // fin obtenirHauteurDivision(id) // retourne la hauteur de la zone de navigation function obtenirHauteurZone() {
if (self.innerHeight) return parseInt(self.innerHeight); else if (document.documentElement && document.documentElement.clientHeight) return parseInt(document.documentElement.clientHeight); else if (document.body) return parseInt(document.body.clientHeight); return 0;
} // fin obtenirHauteurZone() // retourne l"URL de l"image de fond de la division specifiee function obtenirImageFondDivision(id) {
var ldiv=obtenirDivision(id); if (!ldiv) return ""; var img=""; if (dyna_w3 || dyna_ie || dyna_op) img=ldiv.style.backgroundImage; else if (dyna_nn) img=ldiv.background.src; if (img.substring(0, 4)=="url(") img=img.substring(4, img.length-1); return img;
} // fin obtenirImageFondDivision(id) // retourne la largeur de la division specifiee // -- merci a Victor Lopes (victor3.lopes@voila.fr) function obtenirLargeurDivision(id) {
var ldiv=obtenirDivision(id); if (!ldiv) return 0; if (dyna_w3) { var re = /\D/g; return parseInt((ldiv.style.width).replace(re, "")); } else if (dyna_ie || dyna_op) return parseInt(ldiv.style.pixelWidth); else if (dyna_nn) return parseInt(ldiv.clip.right);
} // fin obtenirLargeurDivision(id) // retourne la largeur de la zone de navigation function obtenirLargeurZone() {
if (self.innerWidth) return parseInt(self.innerWidth); else if (document.documentElement && document.documentElement.clientWidth) return parseInt(document.documentElement.clientWidth); else if (document.body) return parseInt(document.body.clientWidth); return 0;
} // fin obtenirLargeurZone() // retourne la position horizontale de la division specifiee function obtenirPosGaucheDivision(id) {
var ldiv=obtenirDivision(id); if (!ldiv) return 0; if (dyna_w3) { var re = /\D/g; return parseInt((ldiv.style.left).replace(re, "")); } else if (dyna_ie || dyna_op) return parseInt(ldiv.style.pixelLeft); else if (dyna_nn) return parseInt(ldiv.left); return 0;
} // fin obtenirPosGaucheDivision(id) // retourne la position verticale de la division specifiee function obtenirPosHautDivision(id) {
var ldiv=obtenirDivision(id); if (!ldiv) return 0; if (dyna_w3) { var re = /\D/g; return parseInt((ldiv.style.top).replace(re, "")); } else if (dyna_ie || dyna_op) return parseInt(ldiv.style.pixelTop); else if (dyna_nn) return parseInt(ldiv.top);
} // fin obtenirPosHautDivision(id) // retourne l"index de superposition de la division specifiee function obtenirZIndexDivision(id) {
var ldiv=obtenirDivision(id); if (!ldiv) return 0; if (dyna_w3 || dyna_ie || dyna_op) return parseInt(ldiv.style.zIndex); else if (dyna_nn) return parseInt(ldiv.zIndex);
} // fin obtenirZIndexDivision(id)
</SCRIPT> </HEAD> <BODY onLoad="majFormulaire()">
JsLib 1.3
Exemple - dyna.js
<NOSCRIPT> <P>Erreur : votre navigateur ne reconnait pas le Javascript ou est configuré pour ne pas prendre en compte le code Javascript. Dans ce dernier cas, vous pouvez modifier la configuration dans les préférences/options de votre navigateur.
</NOSCRIPT> <P>Est-ce que votre navigateur prend en charge le DHTML ? <SCRIPT TYPE="text/javascript" LANGUAGE="Javascript"> if (accepteDHTML()) document.write("oui"); else document.write("non"); </SCRIPT> <P>Largeur de la zone de navigation (en pixels) : <SCRIPT TYPE="text/javascript" LANGUAGE="Javascript">document.write(obtenirLargeurZone());</SCRIPT>
Hauteur de la zone de navigation (en pixels) : <SCRIPT TYPE="text/javascript" LANGUAGE="Javascript">document.write(obtenirHauteurZone());</SCRIPT> <P>Divisions :
Division 1
Division 2
Division 3 imbriquée dans division 2
<SCRIPT TYPE="text/javascript" LANGUAGE="Javascript"> var chx; function majFormulaire() { chx=document.f1.l1.options[document.f1.l1.selectedIndex].value; document.f1.t1.value = obtenirZIndexDivision(chx); document.f1.t2.value = obtenirPosGaucheDivision(chx); document.f1.t3.value = obtenirPosHautDivision(chx); document.f1.t4.value = obtenirLargeurDivision(chx); document.f1.t5.value = obtenirHauteurDivision(chx); document.f1.t6.value = obtenirCouleurFondDivision(chx); document.f1.t7.value = obtenirImageFondDivision(chx); return true; } </SCRIPT> <FORM ACTION="GET" NAME="f1"> <INPUT NAME="b1" TYPE="button" VALUE="Creer Division 4" onClick="creerDivision("d4","500","240","150","150"); modifierCodeDivision("d4","Division 4"); document.f1.l1.selectedIndex=3; majFormulaire(); document.f1.b1.disabled=true;"> <P> <P> <P>Opérations sur la division <SELECT NAME="l1" SIZE="1" onChange="return majFormulaire()"> <OPTION VALUE="d1" SELECTED>1</OPTION> <OPTION VALUE="d2">2</OPTION> <OPTION VALUE="d3">3</OPTION> <OPTION VALUE="d4">4</OPTION> </SELECT> : <P>Visibilité : <INPUT TYPE="button" VALUE="Montrer" onClick="montrerDivision(chx)"> <INPUT TYPE="button" VALUE="Cacher" onClick="cacherDivision(chx)"> <P>Index de superposition (Z-index) : <INPUT TYPE="text" NAME="t1" SIZE=5> <INPUT TYPE="button" VALUE="Modifier" onClick="modifierZIndexDivision(chx, parseInt(document.f1.t1.value))">
Position horizontale (en pixels) : X = <INPUT TYPE="text" NAME="t2" SIZE=5> <INPUT TYPE="button" VALUE="Déplacer" onClick="modifierPosGaucheDivision(chx, parseInt(document.f1.t2.value))"> <P>Position verticale (en pixels) : Y = <INPUT TYPE="text" NAME="t3" SIZE=5> <INPUT TYPE="button" VALUE="Déplacer" onClick="modifierPosHautDivision(chx, parseInt(document.f1.t3.value))"> </TD> |
</TD></TR></TABLE> <P>Largeur (en pixels) : Largeur = <INPUT TYPE="text" NAME="t4" SIZE=10> <INPUT TYPE="button" VALUE="Redimensionner" onClick="modifierLargeurDivision(chx, parseInt(document.f1.t4.value))"> <P>Hauteur (en pixels) : Hauteur = <INPUT TYPE="text" NAME="t5" SIZE=10> <INPUT TYPE="button" VALUE="Redimensionner" onClick="modifierHauteurDivision(chx, parseInt(document.f1.t5.value))"> <P>Couleur de fond (code RGB ou nom HTML) : <INPUT TYPE="text" NAME="t6" SIZE=10> <INPUT TYPE="button" VALUE="Changer la couleur" onClick="modifierCouleurFondDivision(chx, document.f1.t6.value)"> <INPUT TYPE="button" VALUE="Transparent" onClick="modifierCouleurFondDivision(chx, "transparent"); majFormulaire()"> <P>Image de fond (URL) : <INPUT TYPE="text" NAME="t7" SIZE=40> <INPUT TYPE="button" VALUE="Changer l"image" onClick="modifierImageFondDivision(chx, document.f1.t7.value)"> <INPUT TYPE="button" VALUE="Aucune image" onClick="modifierImageFondDivision(chx, null); majFormulaire()"> <P>Contenu (code HTML) : </HTML>
</source>
Nested Layer Visibility Relationships (W3C)<source lang="html4strict"> /* JavaScript Bible, Fourth Edition by Danny Goodman John Wiley & Sons CopyRight 2001
<HTML> <HEAD> <TITLE>layer.style.visibility (W3C)</TITLE> <SCRIPT LANGUAGE="JavaScript"> function setOuterVis(type) { document.getElementById("outerDisplay").style.visibility = type } function setInnerVis(type) { document.getElementById("innerDisplay").style.visibility = type } </SCRIPT> </HEAD> <BODY> Setting the layer.style.visibility Property of Nested Layers (W3C)Click the buttons to see what happens when you change the visibility of the outer layer and inner layer objects.<P> <FORM>
Control outer layer property: <FORM>
Control inner layer property: <P>Placeholder text for raw inner layer.</P> </BODY> </HTML>
</source>
Relationships Among zIndex Values (W3C)<source lang="html4strict"> /* JavaScript Bible, Fourth Edition by Danny Goodman John Wiley & Sons CopyRight 2001
<HTML> <HEAD> <TITLE>layer.style.zIndex</TITLE> <SCRIPT LANGUAGE="JavaScript"> function setZ(field) { switch (field.name) { case "top" : document.getElementById("topLayer").style.zIndex = parseInt(field.value) break case "mid" : document.getElementById("middleLayer").style.zIndex = parseInt(field.value) break case "bot" : document.getElementById("bottomLayer").style.zIndex = parseInt(field.value) } showValues() } function showValues() { var botLayer = document.getElementById("bottomLayer") var midLayer = document.getElementById("middleLayer") var topLayer = document.getElementById("topLayer") document.forms[0].bot.value = botLayer.style.zIndex document.forms[1].mid.value = midLayer.style.zIndex document.forms[2].top.value = topLayer.style.zIndex } </SCRIPT> </HEAD> <BODY onLoad="showValues()"> layer.style.zIndex Property of Sibling LayersEnter new zIndex values to see the effect on three layers.<P> <FORM>
Control Original Bottom Layer:
</FORM> <FORM>
Control Original Middle Layer:
<FORM>
Control Original Top Layer:
</FORM> Original Bottom Layer
Original Middle DIV
Original Top Layer
</BODY> </HTML> </source>
Resizing a Layer (W3C)<source lang="html4strict"> /* JavaScript Bible, Fourth Edition by Danny Goodman John Wiley & Sons CopyRight 2001
<HTML> <HEAD> <TITLE>Layer Resizing</TITLE> <SCRIPT LANGUAGE="JavaScript"> var engaged = false var offsetX = 0 var offsetY = 0 function resizeIt(evt) { evt = (evt) ? evt : (window.event) ? window.event : "" var targElem = (evt.target) ? evt.target : evt.srcElement if (targElem.className == "draggable") { if (engaged) { if (evt.pageX) { targElem.style.width = (evt.pageX - targElem.offsetLeft - offsetX) + "px" targElem.style.height = (evt.pageY - targElem.offsetTop - offsetY) + "px" } else { var elemWidth = evt.clientX - targElem.offsetLeft - offsetX - (parseInt(targElem.style.left) - parseInt(targElem.offsetLeft)) var elemHeight = evt.clientY - targElem.offsetTop - offsetY - (parseInt(targElem.style.top) - parseInt(targElem.offsetTop)) targElem.style.width = elemWidth + "px" targElem.style.height = elemHeight + "px" } } } } function engage(evt) { evt = (evt) ? evt : (window.event) ? window.event : "" var targElem = (evt.target) ? evt.target : evt.srcElement if (targElem.className == "draggable") { while (targElem.id != "myLayer" && targElem.parentNode) { targElem = targElem.parentNode } if (targElem.id == "myLayer") { if (evt.pageX && (evt.pageX > ((parseInt(targElem.style.width) - 20) + targElem.offsetLeft)) && (evt.pageY > ((parseInt(targElem.style.height) - 20) + targElem.offsetTop))) { offsetX = evt.pageX - parseInt(targElem.style.width) - targElem.offsetLeft offsetY = evt.pageY - parseInt(targElem.style.height) - targElem.offsetTop engaged = true } else if ((evt.offsetX > parseInt(targElem.style.width) - 20) && (evt.offsetY > parseInt(targElem.style.height) - 20)) { offsetX = evt.offsetX - parseInt(targElem.style.width) - document.body.scrollLeft offsetY = evt.offsetY - parseInt(targElem.style.height) - document.body.scrollTop engaged = true if (navigator.userAgent.indexOf("Win") == -1) { offsetX += document.body.scrollLeft offsetY += document.body.scrollTop } } return false } } } function release(evt) { evt = (evt) ? evt : (window.event) ? window.event : "" var targElem = (evt.target) ? evt.target : evt.srcElement if (targElem.className == "draggable") { while (targElem.id != "myLayer" && targElem.parentNode) { targElem = targElem.parentNode } if (engaged && targElem.id == "myLayer") { engaged = false } } } </SCRIPT> </HEAD> <BODY> Resizing a Layer (W3C)Here is some content inside the layer. See what happens to it as you resize the layer via the bottom-right 20-pixel handle. <SCRIPT LANGUAGE="JavaScript"> document.onmousedown = engage document.onmouseup = release document.onmousemove = resizeIt document.onmouseout = release </SCRIPT> </BODY> </HTML>
</source>
Setting Layer Backgrounds (W3C)<source lang="html4strict"> /* JavaScript Bible, Fourth Edition by Danny Goodman John Wiley & Sons CopyRight 2001
<HTML> <HEAD> <SCRIPT LANGUAGE="JavaScript"> function setBg(URL) { document.getElementById("bgExpo").style.backgroundImage = "url(" + URL + ")" } </SCRIPT> </HEAD> <BODY> Layer Backgrounds (W3C)Some text, which may or may not read well with the various backgrounds. </BODY> </HTML>
</source>
Testing Nested Layer Coordinate Systems (W3C)<source lang="html4strict"> /* JavaScript Bible, Fourth Edition by Danny Goodman John Wiley & Sons CopyRight 2001
<HTML> <HEAD> <TITLE>Nested Layer Coordinates (W3C)</TITLE> <SCRIPT LANGUAGE="JavaScript"> // offsets within page function getGrossOffsetLeft(elem) { var offset = 0 while (elem.offsetParent) { // correct for IE/Mac discrepancy between offset and style coordinates, // but not if the parent is HTML element (NN6) offset += (elem.offsetParent.tagName != "HTML") ? parseInt(elem.style.left) - parseInt(elem.offsetLeft) : 0 elem = elem.offsetParent offset += elem.offsetLeft } return offset } function getGrossOffsetTop(elem) { var offset = 0 while (elem.offsetParent) { // correct for IE/Mac discrepancy between offset and style coordinates, // but not if the parent is HTML element (NN6) offset += (elem.offsetParent.tagName != "HTML") ? parseInt(elem.style.top) - parseInt(elem.offsetTop) : 0 elem = elem.offsetParent offset += elem.offsetTop } return offset } // offsets within element"s positioning context function getNetOffsetLeft(offset, elem) { while (elem = getParentLayer(elem)) { // correct for IE/Mac discrepancy between offset and style coordinates, // but not if the parent is HTML element (NN6) offset -= (elem.offsetParent.tagName != "HTML") ? parseInt(elem.style.left) - parseInt(elem.offsetLeft) : 0 offset -= elem.offsetLeft } return offset } function getNetOffsetTop(offset, elem) { while (elem = getParentLayer(elem)) { // correct for IE/Mac discrepancy between offset and style coordinates, // but not if the parent is HTML element (NN6) offset -= (elem.offsetParent.tagName != "HTML") ? parseInt(elem.style.top) - parseInt(elem.offsetTop) : 0 offset -= elem.offsetTop } return offset } // find positioning context parent element function getParentLayer(elem) { if (elem.parentNode) { while (elem.parentNode != document.body) { elem = elem.parentNode while (elem.nodeType != 1) { elem = elem.parentNode } if (elem.style.position == "absolute" || elem.style.position == "relative") { return elem } elem = elem.parentNode } return null } else if (elem.offsetParent && elem.offsetParent.tagName != "HTML") { return elem.offsetParent } else { return null } } // functions that respond to changes in text boxes function setOuterPage(field) { var val = parseInt(field.value) var elem = document.getElementById("outerDisplay") switch (field.name) { case "pageX" : elem.style.left = ((elem.offsetParent) ? getNetOffsetLeft(val, elem) : val) + "px" break case "pageY" : elem.style.top = ((elem.offsetParent) ? getNetOffsetTop(val, elem) : val) + "px" break } showValues() } function setOuterLayer(field) { var val = parseInt(field.value) switch (field.name) { case "left" : document.getElementById("outerDisplay").style.left = val + "px" break case "top" : document.getElementById("outerDisplay").style.top = val + "px" break } showValues() } function setInnerPage(field) { var val = parseInt(field.value) var elem = document.getElementById("innerDisplay") switch (field.name) { case "pageX" : elem.style.left = ((elem.offsetParent) ? getNetOffsetLeft(val, elem) : val) + "px" break case "pageY" : elem.style.top = ((elem.offsetParent) ? getNetOffsetTop(val, elem) : val) + "px" break } showValues() } function setInnerLayer(field) { var val = parseInt(field.value) switch (field.name) { case "left" : document.getElementById("innerDisplay").style.left = val + "px" break case "top" : document.getElementById("innerDisplay").style.top = val + "px" break } showValues() } function showValues() { var form = document.forms[0] var outer = document.getElementById("outerDisplay") var inner = document.getElementById("innerDisplay") form.elements[0].value = outer.offsetLeft + ((outer.offsetParent) ? getGrossOffsetLeft(outer) : 0) form.elements[1].value = outer.offsetTop + ((outer.offsetParent) ? getGrossOffsetTop(outer) : 0) form.elements[2].value = parseInt(outer.style.left) form.elements[3].value = parseInt(outer.style.top) form.elements[4].value = inner.offsetLeft + ((inner.offsetParent) ? getGrossOffsetLeft(inner) : 0) form.elements[5].value = inner.offsetTop + ((inner.offsetParent) ? getGrossOffsetTop(inner) : 0) form.elements[6].value = parseInt(inner.style.left) form.elements[7].value = parseInt(inner.style.top) } </SCRIPT> </HEAD> <BODY onLoad="showValues()"> Nested Layer Coordinates (W3C)Enter new page and layer coordinates for the outer layer and inner layer objects.<P> <FORM>
</FORM> ARTICLE I<P> Congress shall make no law respecting an establishment of religion, or prohibiting the free exercise thereof; or abridging the freedom of speech, or of the press; or the right of the people peaceably to assemble, and to petition the government for a redress of grievances. </P> </BODY> </HTML>
</source>
The layer while rolling over the link.<source lang="html4strict"> /* JavaScript Unleashed, Third Edition by Richard Wagner and R. Allen Wyke ISBN: 067231763X Publisher Sams CopyRight 2000
<html> <head> <title>JavaScript Unleashed</title> <style type="text/css"> </style> <script type="text/javascript" language="JavaScript1.2"> </script> </head> <body onload="checkBrowser()"> Hello World! <a href="javascript:void(0)" onmouseout="changeState("layer1","hidden")" onmouseover="changeState("layer1","visible")"> Rollover to show and hide the layer. </a> </body> </html> </source> |