Results 1 to 6 of 6

Thread: Mouse-over with 2 different images changing?

  1. #1
    Join Date
    Feb 2005
    Posts
    71
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Mouse-over with 2 different images changing?

    Is it possible to change two diffent images when I move my mouse over a third image? For examle, I want to be able to move the mouse over an image of a thumbnail of a Russian flag with the result that 2 (or possibly more) images on the same page change to garphics with Russian text.

  2. #2
    Join Date
    Aug 2004
    Posts
    10,143
    Thanks
    3
    Thanked 1,008 Times in 993 Posts
    Blog Entries
    16

    Default

    Yes, the main thing to understand is how to change an image using JavaScript, which is via the syntax:

    Code:
    document.images["myimage"].src="new.gif"
    where "myimage" is the name of the image you're changing. So a function like:

    Code:
    <script>
    function changeimage(imgname, imgsrc){
    document.images[imgname].src=imgsrc
    }
    </script>
    can be used to generically change any image. Regarding your question, you would call the above function this way:

    Code:
    <a href="#" onMouseover="changeimage('dog', 'new.gif'); changeimage('cat', 'new2.gif');"><img src="what.gif"></a>

  3. #3
    Join Date
    Feb 2005
    Posts
    71
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default

    Thanks very much for your reply. Unfortunatrly, I didn't seem to be able to implement it.

    If the image that I want to move the mouse over is called 3.gif, and the 2 images that I want to change when I move the mouse over 3.gif are 1.gif and 2.gif, please can you specify the exact code that I need to paste?

    Sorry to be so dim, but java is all still a bit of a mystery to me.

  4. #4
    Join Date
    Dec 2004
    Location
    UK
    Posts
    2,358
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by robertsaunders
    If the image that I want to move the mouse over is called 3.gif, [...]
    The original filename of any of the images is not important. What is, is the id or name attribute values of the two img elements you want to alter.

    Let's assume you have the following three img elements. They're precise location in the document isn't a concern, nor are the attribute values. The first element is your control element, whilst the second and third are the ones to be manipulated.

    HTML Code:
    <img alt="" src="a.png">
    <img id="img1" alt="" src="b1.png">
    <img id="img2" alt="" src="c1.png">
    In order to alter the images, we need to execute some code in response to the mouseover event. The function we will call, setSource is shown below:

    Code:
    function setSource() {
      for(var j = 0, n = (arguments.length & ~1); j < n; j += 2) {
        document.images[j].src = arguments[j + 1];
      }
    }
    This function takes pairs of strings. The first string in each pair is the id or name of the img element, and the second string is a URL (relative or absolute) to the new image resource.

    For example (using the markup snippet above):

    HTML Code:
    <img alt="" src="a.png"
     onmouseover="setSource('img1', 'b2.png', 'img2', 'c2.png');">
    <img id="img1" alt="" src="b1.png">
    <img id="img2" alt="" src="c1.png">
    hovering the cursor over the control element would instruct the second image (img1) to display b2.png, and the third image (img2) to display c2.png.

    Sorry to be so dim, but java is all still a bit of a mystery to me.
    Indeed, as Java and ECMAScript[1] are two completely different languages. Please don't get them confused. We are currently dealing with ECMAScript and the Document Object Model (DOM).

    Hope that helps,
    Mike


    [1] The standardised version of Microsoft's JScript, and Netscape's JavaScript.

  5. #5
    Join Date
    Feb 2005
    Posts
    71
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default

    The function we will call, setSource is shown below:

    Code:
    function setSource() {
      for(var j = 0, n = (arguments.length & ~1); j < n; j += 2) {
        document.images[j].src = arguments[j + 1];
      }
    }

    Thanks mike, that was v helpful. However, I was a little unsure exactly where I would insert the code above. Would this go within the <head> tabs, or does it not matter where it goes? Does it need to be inserted within <script> tabs, or just as it is?

  6. #6
    Join Date
    Dec 2004
    Location
    UK
    Posts
    2,358
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by robertsaunders
    Thanks mike, that was v helpful.
    Hmm. Except for the mistake in the code. I've corrected it below. I changed some of the code and must have deleted something I didn't mean to.

    [...] I was a little unsure exactly where I would insert the code [...]. Would this go within the <head> tabs, or does it not matter where it goes?
    Technically, it doesn't really matter. As long as the code is parsed before someone does something to require it (that is, before it's called), the code will work fine. The best way though is to place it in the head element.

    Does it need to be inserted within <script> tabs [...]
    Yes:

    HTML Code:
    <script type="text/javascript">
      function setSource() {
        for(var j = 0, n = (arguments.length & ~1); j < n; j += 2) {
          document.images[arguments[j]].src = arguments[j + 1];
        }
      }
    </script>
    Mike

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •