Create a Rollover Plugin Starting with the downloadable assignment files, write
ID: 3724705 • Letter: C
Question
Create a Rollover Plugin
Starting with the downloadable assignment files, write the Javascript needed for the application. Use the book and lectures as a guide. In this exercise, you’ll create a plugin that displays a different image when the mouse is rolled over an image in a list of images
1. Open the HTML and JavaScript files in the week 8 zip folder
Run this application to observe how it works.
2. Review the code in the jquery.rollover.js file to see that it contains code for the standard plugin structure with the method name set to changeImage().
3. Copy the code in the ready event handler of the rollover.js file into the each() method of the plugin. Then, modify the selector for the each() method in this copied code so its function is executed for each img element that’s a descendant of the element that the changeImage() method is executed on.
4. Return to the rollover.js file, and replace the code in the ready event handler with a statement that calls the changeImage() method for the “image_rollovers” list.
5. Add a script element for the jquery.rollover.js file to the HTML file before the script element for the rollover.js file. The application should now run just like it did before.
Add your first and last name to the title on the interface. For example: Jane Doe’s Image Gallery.
index.html
</html>
jquery.rollover.js
(function($){
$.fn.changeImage = function() {
return this.each(function() {
});
};
})(jQuery);
rollover.js
$(document).ready(function() {
$("#image_rollovers img").each(function() {
// get old and new urls
var oldURL = $(this).attr("src");
var newURL = $(this).attr("id");
// preload images
var rolloverImage = new Image();
rolloverImage.src = newURL;
// set up event handlers
$(this).hover(
function() {
$(this).attr("src", newURL);
},
function() {
$(this).attr("src", oldURL);
}
); // end hover
}); // end each
}); // end ready
main.css
body {
font-family: Arial, Helvetica, sans-serif;
margin: 0 auto;
padding: 20px;
width: 760px;
border: 3px solid blue;
}
h1, ul {
margin: 0;
padding: 0;
}
h1 {
padding-bottom: .5em;
}
img {
height: 180px;
width: 240px;
}
li {
padding-right: 10px;
display: inline;
}
images stored in a folder called images
<!DOCTYPE HTML> <html lang="en"> <head> <meta charset="UTF-8"> <title>Image Rollovers</title> <link rel="stylesheet" href="main.css"> <script src="https://code.jquery.com/jquery-3.1.1.min.js"></script> <script src="rollover.js"></script> </head> <body> <main> <h1>Ram Tap Combined Test</h1> <ul id="image_rollovers"> <li><img src="images/h1.jpg" alt="" id="images/h4.jpg"></li> <li><img src="images/h2.jpg" alt="" id="images/h5.jpg"></li> <li><img src="images/h3.jpg" alt="" id="images/h6.jpg"></li> </ul> </main> </body></html>
jquery.rollover.js
(function($){
$.fn.changeImage = function() {
return this.each(function() {
});
};
})(jQuery);
rollover.js
$(document).ready(function() {
$("#image_rollovers img").each(function() {
// get old and new urls
var oldURL = $(this).attr("src");
var newURL = $(this).attr("id");
// preload images
var rolloverImage = new Image();
rolloverImage.src = newURL;
// set up event handlers
$(this).hover(
function() {
$(this).attr("src", newURL);
},
function() {
$(this).attr("src", oldURL);
}
); // end hover
}); // end each
}); // end ready
main.css
body {
font-family: Arial, Helvetica, sans-serif;
margin: 0 auto;
padding: 20px;
width: 760px;
border: 3px solid blue;
}
h1, ul {
margin: 0;
padding: 0;
}
h1 {
padding-bottom: .5em;
}
img {
height: 180px;
width: 240px;
}
li {
padding-right: 10px;
display: inline;
}
images stored in a folder called images
Ram Tap Combined TestExplanation / Answer
Steps to create roll over images
1. Get ready two pictures for the rollover impact. Select two unique pictures to influence a rollover to picture and spare them in a similar envelope where you will spare your HTML record showing a rollover picture.
2
Open any word processor of your decision. Dreamweaver will be utilized as the word processor in this article. Something else, if your content tool is clear when you open it, you have to enter HTML components to fabricate a site page.
3
Find the <head></head> segment. JavaScript code will be embedded inside the <head></head> tag. Two JavaScript capacities will be made to change the pictures. The two capacities are named MouseRollover and MouseOut in the code underneath. The picture's src property will be utilized to change the picture's source when those two capacities are called.
4
Duplicate the accompanying JavaScript code:
<script language="javascript">
function MouseRollover(MyImage) {
MyImage.src = "MyPicture2.jpg";
}
function MouseOut(MyImage) {
MyImage.src = "MyPicture1.jpg";
}
</script>
5
Glue the JavaScript code in the middle of the <head></head> area onto your content manager. The MyPicture2.jpg in the capacity MouseRollover ought to be supplanted by your rollover picture's name and the MyPicture1.jpg in the capacity called MouseOut ought to be supplanted by your unique picture's name.
6
Find the <body></body> segment. The picture tag <image src="FileName" Alt="Title" boarder="0px" width="650px" height="550px"/> will be connected to show the rollover picture. In this illustration, the Alt="Title" that alludes to the picture title's name is precluded.
7
Duplicate the accompanying code:
<div align="center">
<!- - The rollover picture shows here.- - >
<img src="MyPicture1.jpg" border="0px"
width="650px" height="550px"
onMouseOver="MouseRollover(this)"
onMouseOut="MouseOut(this)"/>
</div>
8
Glue the code in the middle of the <body></body> area. The onmouseover property is included inside the picture tag above and will be doled out to call the JavaScript work Image Rollover to change your unique picture to another rollover picture. Supplant MyPicture1.jpg with your unique picture's name. Additionally, another property called onMouseOut is included request to change the picture once again into its unique one when you move your mouse far from the rollover picture.
9
Audit the whole code. Your code should appear to be like the code beneath. You can play around with the code from this illustration and perceive how things change.
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>How to Make a JavaScript Image Rollover</title>
<!--JavaScript code goes here.-->
<script language="javascript">
function MouseRollover(MyImage) {
MyImage.src = "MyPicture2.jpg";
}
function MouseOut(MyImage) {
MyImage.src = "MyPicture1.jpg";
}
</script>
</head>
<body>
<div align="center">
<!--The rollover image displays here.-->
<img src="MyPicture1.jpg" boarder="0px" width="650px" height="550px"
/>
</div>
</body>
</html>
10. Click “File” and select “Save.”