WD Notes Unit 5
WD Notes Unit 5
WD Notes Unit 5
Introduction:
Dynamic HTML is not a new markup language. It is a collection of technologies that allows
dynamic changes to documents defined with HTML. Specifically, a dynamic HTML document
is an HTML document whose tag attributes, tag contents, or element style properties can be
changed by user interaction or the occurrence of a browser event after the document has been,
and is still being, displayed. Such changes can be made with an embedded script that accesses
the elements of the document as objects in the associated DOM structure.
Elements can be moved to new positions on the display.
Elements can be made to appear and disappear.
Foreground (text) and background colors can be changed.
The font, font size and font style an be changed.
Element content can be changed.
The stacking order of overlapping elements (such as pictures) can be changed.
The mouse cursor position can be changed.
Text can be made to move across the display.
Positioning Elements:
We can position any element anywhere in the display of a document, also to move an element to
a new position in the display dynamically, using JavaScript to change the positioning style
properties of the element. These style properties, which are appropriately named left and top,
dictate the distance from the left and top of some reference point to where the element is to
appear. Another style property, position, interacts with left and top to provide a higher level of
control of placement and movement of elements. The position property has three possible values:
absolute, relative, and static.
1.Absolute Positioning:
The absolute value for position is specified when the element is to be placed at a specific location
in the document display without regard to the positions of other elements. For example, if a
paragraph of text is to appear 100 pixels from the left edge and 200 pixels from the top of the
display window, the following element could be used:
One use of absolute positioning is to superimpose special text over a paragraph of ordinary text
to create an effect similar to a watermark on paper.
Output:
<html>
<head>
<title> absolute positioning </title>
<style>
.regtext {position:absolute;top:70px;left:25px;}
<style>
<head>
<body>
<p class=”regtext”>
Apples are good for health
</p>
</body>
</html>
2. Relative Positioning
An element that has the position property set to relative, but does not specify top and left property
values, is placed in the document as if the position attribute were not set at all. However, such an
element can be moved later. If the top and left properties are given values, they displace the
element by the specified amount from the position where it would have been placed (if top and
left had not been set).
3.Static Positioning:
The default value for the position property is static. A statically positioned element is placed in
the document as if it had the position value of relative but no values for top or left were given.
Moving Elements:
An HTML element whose position property is set to either absolute or relative can be moved.
Moving an element is simple: Changing the top or left property values causes the element to
move on the display. If its position is set to absolute, the element moves to the new values of top
and left; if its position is set to relative, it moves from its original position by distances given by
the new values of top and left.
Element Visibility:
Document elements can be specified to be visible or hidden with the value of their
visibility property. The two possible values for visibility are, quite naturally, visible and
hidden. The appearance or disappearance of an element can be controlled by the user
through a widget. The following example displays an image and allows the user to toggle
a button, causing the image to appear and not appear in the document display (once again,
the event handler is in a separate file):
Dynamic Content:
The content of an element is accessed through the value property of its associated
JavaScript object. So, changing the content of an element is not essentially different from
changing the style properties of the element.
Assistance to a browser user filling out a form can be provided with an associated text
area, often called a help box. The content of the help box can change, depending on the
placement of the mouse cursor. When the cursor is placed over a particular input field,
the help box can display advice on how the field is to be filled in. When the cursor is
moved away from all input fields, the help box content can be changed to simply indicate
that assistance is available.
Stacking Elements:
The top and left properties allow the placement of an element anywhere in the two
dimensions of the display of a document. Although the display is restricted to two
physical dimensions, the effect of a third dimension is possible through the simple
concept of stacked elements, such as that used to stack windows in graphical user
interfaces. Although multiple elements can occupy the same space in the document, one
is considered to be on top and is displayed. The top element hides the parts of the lower
elements on which it is superimposed. The placement of elements in this third dimension
is controlled by the z-index attribute of the element. An element whose z-index is greater
than that of an element in the same space will be displayed over the other element,
effectively hiding the element with the smaller z-index value. The JavaScript style
property associated with the z-index attribute is zIndex.
In the example, three images are placed on the display so that they overlap. In the HTML
description of this situation, each image tag includes an onclick attribute, which is used
to trigger the execution of a JavaScript handler function. First, the function defines DOM
addresses for the last top element and the new top element. Then, the function sets the
zIndex value of the two elements so that the old top element has a value of 0 and the new
top element has the value 10, effectively putting it at the top. The script keeps track of
which image is currently on top with the global variable topp, which is changed every
time a new element is moved to the top with the toTop function.
Locating the Mouse Cursor:
In JavaScript, we have methods that help us to capture the location of the mouse on the
screen. The top left corner of the screen is (0, 0) i,e, X, and Y coordinates are (0, 0).
This means that vertical zero is the topmost point and horizontal zero is the leftmost
point. In this article, we are going to learn how to find the Coordinates of the Mouse
cursor in JavaScript.
It can be implemented using the clientX and clientY methods of the event:
event.clientX: It gives the horizontal coordinate of the event.
event.clientY: It gives the vertical coordinate of the mouse.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<script>
function coordinate(event) {
let x = event.clientX;
let y = event.clientY;
document.getElementById("X").value = x;
document.getElementById("Y").value = y;
}
</script>
<body onmousemove="coordinate(event)">
X-coordinate
<input type="text" id="X">
<br>
<br>
Y-coordinate
<input type="text" id="Y">
</body>
</html>
Reacting to a Mouse Click:
In this case, the mousedown and mouseup events are used, respectively, to show and hide the
message “Please don’t click here!” on the display under the mouse cursor whenever the mouse
button is clicked, regardless of where the cursor is at the time.