1

I'd like to dynamically pass an array value (in this case, String "../name1.jpg" or "../name2.jpg") to the img src <img src="here">. Each time you hover over "Class of 2013", a random image will fade in, then fade out.

Is that possible to pass in a value by calling it as such? <img src="getImages(); ?

How would you do it?

var ImgArray = new Array();
ImgArray[0] = "../name1.jpg";
ImgArray[1] = "../name2.jpg";

$(document).ready(function() {
    show();
});

function show(){
    $(".box").hover(
        function(){ $(this).find(".overlay").fadeIn(); } ,
        function(){ $(this).find(".overlay").fadeOut(); }
    );        
}

function getImages() {
    int i = Math.random() * (max - min) + min;          
    return ImgArray[i];
}

HTML:

<div class="box">
    Class of 2013:
    <div class="overlay"> <!-- Put images to display here...--> 
        <img src="getImages();" alt="image to be displayed" />
    </div>
</div>​

Thank you


EDIT: Current Code:

<html>
<head>
    <script src="jquery.js"></script>

    <script type="text/javascript">     
        var ITLP = new Array();
        ITLP[0] = "/corp/itlp/ITLP%20Bios/DanTurcotte.jpg";
        ITLP[1] = "/corp/itlp/ITLP%20Bios/gomezwilmann.jpg";


        $(document).ready(function() {
            showimg();
        });


        function showimg()
            {
                $(".box > .overlay > img").attr("src",getImages());
                $(".box").hover(
                    function(){ $(this).find(".overlay").fadeIn(); } ,
                    function(){ $(this).find(".overlay").fadeOut(); }
                );        
            }

        function getImages() {
            int i = Math.abs(Math.random());            
            return ITLP[0];             
        }

    </script>


</head> 
<body>

    <div class="box">
        Class of 2013:
        <div class="overlay"> <!-- Put images to display here...-->                 
            <img src="" border="none"/>     
        </div>  
    </div>​

</body>
</html>

3 Answers 3

1

I prefer the classic way:

    $(".box > .overlay > img").attr("src",getImages());

Extension:

function show()
{
    $(".box > .overlay > img").attr("src",getImages());
    $(".box").hover(
        function(){ $(this).find(".overlay").fadeIn(); } ,
        function(){ $(this).find(".overlay").fadeOut(); }
    );        
}

I think your requested way doesn't work, because the browser expect a string for the source attribute.

P.S.: Take an eye on your random function. My Firefox did complain something "(missing ';' before statement")

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

7 Comments

Thanks, reporter. Not sure how to use this though
See the comment to stackoverflow.com/questions/8211744/… . It is a mathematical function. I could never find out the main meaning.
Reporter, that link doesn't provide a meaning. And, also, what is the notation .box > .overlay > img in $(".box > .overlay > img").attr("src",getImages()); ?
The definition within the $() uses the normal css selector. So: find elements with class 'box'. Within that elements find elements with css class 'overlay' and within these, find elements with the html tag 'img'
|
1

You may try this

var ImgArray = new Array();
// add images

$(document).ready(show);

// Show function
function show(){
    $('img').attr('src', ImgArray[0]).parent().hide();
    var max = ImgArray.length;
    $(".box").hover(
        function(){ $(this).find(".overlay").fadeIn(); },
        function(){ 
            var img = $(this).find(".overlay").find('img');
            var i = Math.floor(Math.random()*max);
            $(this).find(".overlay").fadeOut(function(){
                img.attr('src', ImgArray[i]);
            }); 
        }
    );        
}

DEMO.

2 Comments

is there a way to display all images in the array at once upon hover over?
At once, how ? One img can show on one image at a time, can you describe it ?
1

You want to change the source of the image in your first hover function (the one with fadeIn).

Since you're already using jQuery, just do this:

    function(){ $(this img).attr("src", getImages()); $(this).find(".overlay").fadeIn(); } ,

and set your img's src initially to "". Of course, you could simplify your jQuery if you added an id to the img tag.

EDIT I set up a fiddle with a working example here.

This is the script code:

   var ITLP = [];
   ITLP[0] = "http://lorempixel.com/400/200/nature";
   ITLP[1] = "http://lorempixel.com/400/200/city";

   function getImages() {
       var i = Math.floor(Math.random() * (ITLP.length + 1)); // from https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/random?redirectlocale=en-US&redirectslug=JavaScript%2FReference%2FGlobal_Objects%2FMath%2Frandom
       console.log(ITLP[0]);
       return ITLP[0];
   }

   function showimg() {
       $(".box").hover(

       function () {
           console.log("Hover start");
           $(".box > .overlay > img").attr("src", getImages());
           $(this).find(".overlay").fadeIn();
       },

       function () {
           console.log("Hover end");
           $(this).find(".overlay").fadeOut();
       });
   }


   showimg();

Major things I did:

  • Ordered the functions to be declared before they're used.
  • Got rid of the int i declaration that was killing getImages()

To get it this far, I used the console log a lot, and also jsfiddle's JSHint is a big help.

3 Comments

Thanks Scott. Would I put the function() { ... directly inside the $(document).ready(...? I've set the img src initially to "", but it doesn't even seem to be showing the image, or working at all
Hi, Growler. The line I gave you would replace the second line you have in your hover function. Setting this binding during document(ready) as you have should be a good thing. As reporter mentioned, you do have some bugs in the getImages() function. That may be part of the issue you're having. Check the console and see if it tells you anything.
@Growler Please check my edits, they should help you considerably.

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.