0

Need help changing the images with mouseover and mouse out with preloaded images. I was able to figure out the preloading but I can't get the image to change. If anybody can help me out it would be greatly appreciated.

"use strict";
 var $ = function(id) { return document.getElementById(id); };

window.onload = function preload() {
var image1 = $("image1");           
var image2 = $("image2");           

// preload images 
var links = $("image_list").getElementsByTagName("a");
var i, link, image;
for(i = 0; i < links.length;i++){
    link = links[i];
    image = new Image();
    image.src = link.href;
 }


  // attach mouseover and mouseout events for each image

   image1.mouseover=function(){
       image1=image.src="images/release.jpg";
  };

  image1.onmouseout = function() {
    image1=image.src="images/hero.jpg";

  };


    image2.onmouseover = function() {
    image.src="images/deer.jpg";
};
    image2.onmouseout= function(){
    image.src="images/bison.jpg";
};


};

3 Answers 3

1

The way is changing the .src attribute of an image object.

image1.src="images/release.jpg";

instead of

image1=image.src="images/release.jpg";

Althought we don't usually do it with javascript. The way is with CSS, displaying the image as background-image of a DOM element and setting an other background-image on the same element on :hover.

Sign up to request clarification or add additional context in comments.

1 Comment

Alright I got it to work, thank you. And yeah I know, I had to do it this way for my javascript class.
0

"use strict";
var $ = function(id) { return document.getElementById(id); };

window.onload = function() {
    var image1 = $("image1");           
    var image2 = $("image2");           
    
    // preload images 
    var links = $("image_list").getElementsByTagName("a");
    var i, link, image;
    for(i = 0; i < links.length; i++){
		link = links[i];
		image = new Image();
		image.src = link.href;
	}

    // attach mouseover and mouseout events for each image
    image1.onmouseover = function() {
        image1.src = "images/release.jpg";
    };
    image1.onmouseout = function() {
        image1.src = "images/hero.jpg";
    };
    
    image2.onmouseover = function() {  
        image2.src="images/deer.jpg";
    };
    image2.onmouseout = function() {
        image2.src="images/bison.jpg";
    };
};

1 Comment

Welcome to SO, Xuan Weng! Code-only answers are discouraged here, as they provide no insight into how the problem was solved. Please update your solution with an explanation of how your code solves the OP's problem :)
0

"use strict";
var $ = function(id) { return document.getElementById(id); };

window.onload = function() {
    var image1 = $("image1");           
    var image2 = $("image2");           
    
    // preload images 
    var links = $("image_list").getElementsByTagName("a");
    var i, link, image;
    for(i = 0; i < links.length; i++){
		link = links[i];
		image = new Image();
		image.src = link.href;
	}

    // attach mouseover and mouseout events for each image
    image1.onmouseover = function() {
        image1.src = "images/release.jpg";
    };
    image1.onmouseout = function() {
        image1.src = "images/hero.jpg";
    };
    
    image2.onmouseover = function() {  
        image2.src="images/deer.jpg";
    };
    image2.onmouseout = function() {
        image2.src="images/bison.jpg";
    };
};
body {
    font-family: Verdana, Arial, Helvetica, sans-serif;
    font-size: 90%;
    background-color: white;
    width: 790px;
    margin: 0 auto;
    border: 3px solid blue;
    padding: 1em;
}
h1 {
    color: blue;
    margin: 0;
    padding: 0;
    text-align: center;
}
p {
    margin: 0;
    padding: .5em 0;
    text-align: center;	
}
ul { 
    display: none; 
}
<!DOCTYPE html>
<html>
<head>
  <title>Image Rollover</title>
  <link rel="stylesheet" type="text/css" href="rollover.css">
  <script type="text/javascript" src="rollover.js"></script>
</head>

<body>
    <main>
        <h1>Fishing Images</h1>
        <p>Move your mouse over an image to change it and back out of the
            image to restore the original image.</p>
        <ul id="image_list">
            <li><a href="images/release.jpg" title="Catch and Release"></a></li>
            <li><a href="images/deer.jpg" title="Deer at Play"></a></li>
            <li><a href="images/hero.jpg" title="The Big One!"></a></li>
            <li><a href="images/bison.jpg" title="Grazing Bison"></a></li>
        </ul>
        <p>
            <img id="image1" src="images/hero.jpg" alt="">
            <img id="image2" src="images/bison.jpg" alt="">
        </p>
    </main>
</body>
</html>

1 Comment

Welcome to SO, Xuan Weng! You've already answered this question, so it would be helpful if you deleted one of your redundant answers (and added explanations to your code as well!)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.