Practical No
Practical No
Theory
Rollover means a webpage changes when the user moves his or her mouse
over an object on the page. It is often used in advertising. There are two ways
to create rollover, using plain HTML or using a mixture of JavaScript and
HTML. We will demonstrate the creation of rollovers using both methods.
Syntax
Users can follow the below syntax to change styles of image on rollover.
object.onmouseover = function(){myScript};
object.onmouseout = function(){myScript};
object.addEventListener("mouseover", myScript);
object.addEventListener("mouseout", myScript);
Algorithm
Step1 − Access the image element by id.
deomImage.onmouseover = function() {
document.demo.style = "height:200px;width:200px";
});
Step 3 − Assign onmouseout event to image element deomImage.
deomImage.onmouseout = function() {
document.demo.style = "height:100px;width:100px;"
});
Events
onmouseover − It is an event that will be called whenever a user will rollover on the
image element.
onmouseout − This event will be triggered when the user will mouse pointer outside
the image.
Example
Using JavaScript onmouseover and onmouseout events
In the below example, we have created the image element and given the default
width and height to the image. We have added the “onmouseover” event to the
image element to apply a different style when the user rollover to the image.
Furthermore, when the user moves the cursor pointer outside the image element, we
have applied the “onmouseout” event to apply the default style.
Program :
Example
<!DOCTYPE html>
<html>
<body>
<h2> Image rollover with mouse event. </h2>
<h4> Rollover on the below image to change the styles of the image. </h4>
<img src="durga.jpg" style="height:100px;width:100px;" id="demo"
name="demo" alt="demo Image">
<script>
let deomImage = document.getElementById("demo");
deomImage.onmouseover = function() {deomImage.style = "height:200px;
width:200px";};
deomImage.onmouseout = function() {deomImage.style = "height:100px;
width:100px";};
</script>
</body>
</html>
Example
<html>
<head>
<title> Show image rollover with mouse event. </title>
</head>
<body>
<h2> Showing image rollover with mouse event. </h2>
<h4> Rollover on the below image to change the styles of the image. </h4>
<img src="durga.jpg" style="height:100px;width:100px;"
id="demo" name="demo" alt="demo Image">
<script>
let deomImage = document.getElementById("demo");
deomImage.addEventListener( "mouseover", function () {
document.demo.style = "height:200px; width:200px";
});
deomImage.addEventListener( "mouseout", function () {
document.demo.style = "height:100px; width:100px;"
})
</script>
</body>
</html>
Output :
Conclusion :