Solved

Hover over text, thumbnail image appears

Posted on 2004-10-05
14
1,177 Views
Last Modified: 2008-02-01
I'm putting some wallpapers on my website that people can download by clicking on a text link and the appropriate size.
What I would like to do is when the mouse hovers over the text link, a thumbnail of the wallpaper will appear.

I think i've seen this before possibly at QXL.com.

Can anyone help?
0
Comment
Question by:matreece
  • 6
  • 6
  • 2
14 Comments
 
LVL 15

Expert Comment

by:justinbillig
ID: 12225809
<htmL>
<body>
<a href="#" id="Wallpaper1.jpg" onmouseover="InsertImg( this.id );" onmouseout="InsertImg( 'none' );">Wallpaper1</a>
<br><br>

<img src="holder.jpg" height="1" width="1" id="idWallpaper">

<script language="javascript">
function InsertImg( strImage )
{

      // Get a reference to the img
        var objImage = document.getElementById( "idWallpaper" );

      // Do we clear the img?
       if( strImage != "none" )
       {
            // No
            objImage.src = strImage;
            objImage.style.height = 200;
            objImage.style.width = 200;
       }
       else
       {
            objImage.src = "holder.jpg";
            objImage.style.height = 1;
            objImage.style.width = 1;
        }
}
</script>
</body>
</html>



0
 
LVL 15

Expert Comment

by:justinbillig
ID: 12225822
change this line

<a href="#" id="Wallpaper1.jpg" onmouseover="InsertImg( this.id );" onmouseout="InsertImg( 'none' );">Wallpaper1</a>

to this

<a href="#" id="Wallpaper1.jpg" onmouseover="InsertImg( this.id );" onmouseout="InsertImg( 'none' );" onclick="(return false);">Wallpaper1</a>
0
 

Author Comment

by:matreece
ID: 12228104
Thats almost what im after although ideally i wanted it to appear next to the cursor. I guess it would be in a layer that follows the cursor when hovering over the link text.
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
LVL 3

Expert Comment

by:hemebond
ID: 12235108
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
      <head>
            <title>Q_21156215</title>
            <!--[if IE]><script type="text/javascript">var ie = true;</script><![endif]-->
            <style type="text/css">
                  dl      {position:relative;}
                  dt      {position:absolute;}
            </style>
      </head>
      <body>
            <dl>
                  <dt><img src="/images/1.jpg" alt=""></dt>
                  <dd><a href="">800x600</a></dd>
                  <dd><a href="">1024x768</a></dd>
            </dl>
      </body>
      <script type="text/javascript">
            var thumbnail = null;
            var list = document.getElementsByTagName("dd");
            for(var i = 0; i < list.length; i++)
            {
                  if(window.ie)
                  {
                        list[i].onmouseover = show_thumbnail;
                        list[i].onmouseout = hide_thumbnail;
                        list[i].onmousemove = move_thumbnail;
                  }
                  else
                  {
                        list[i].addEventListener('mouseover',show_thumbnail,true);
                        list[i].addEventListener('mouseout',hide_thumbnail,true);
                        list[i].addEventListener('mousemove',move_thumbnail,true);
                  }
                  list[i].parentNode.getElementsByTagName('dt')[0].style.display = 'none';
            }

            function show_thumbnail(e)
            {
                  thumbnail = (window.ie) ? this.parentNode.getElementsByTagName('dt')[0] : e.currentTarget.parentNode.getElementsByTagName('dt')[0];
                  thumbnail.style.display = '';
            }

            function hide_thumbnail(e)
            {
                  thumbnail.style.display = 'none';
            }

            function move_thumbnail(e)
            {
                  if(window.ie)
                  {
                        thumbnail.style.top = window.event.clientY + "px";
                        thumbnail.style.left = window.event.clientX + "px";
                  }
                  else
                  {
                        thumbnail.style.top = e.clientY + "px";
                        thumbnail.style.left = e.clientX + "px";
                  }
            }
      </script>
</html>
0
 
LVL 3

Expert Comment

by:hemebond
ID: 12235114
Hmm. Remove

dt     {position:absolute;}

and add

list[i].parentNode.getElementsByTagName('dt')[0].style.position = 'absolute';

below

list[i].parentNode.getElementsByTagName('dt')[0].style.display = 'none';
0
 

Author Comment

by:matreece
ID: 12235554
This is exactly what i'm after.
Just a couple of problems though:
I'm getting an error saying "'thumbnail.style' is null or not an object"
also
The thumbnail doesn't sit next to the cursor and if i add a couple more links underneath the thumbnail drops down further.
Is it something to do with the first line of code (the bit that says '<!--[if IE]')?

Many thanks for your help
0
 
LVL 3

Expert Comment

by:hemebond
ID: 12242696
That <!--[if IE]> is just for Internet Explorer. Other browsers will ignore it, but IE will create that script section that sets a variable so I can hack around IE's rubbish support.

Is this error you're getting only in Internet Explorer? Because it seems to work fine for me, but the debugger in IE isn't working for me, so I can't check anything. Have you used the code exactly as it is here? If not, can you link to the page you've used it in please.
0
 

Author Comment

by:matreece
ID: 12247092
This is how my code looks below.
You'll notice when rolling over the bottom links the thumbnail moves further away from the cursor.
And im still getting that error.
Your help is most appreciated.

--------------------------------------

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
     <head>
          <title>Q_21156215</title>
  <!--[if IE]><script type="text/javascript">var ie = true;</script><![endif]-->
             <style type="text/css">
               dl     {position:relative;}
          </style>
     </head>
     <body>
          <dl>
               <dt><img src="image1.jpg" width="100" height="100" alt=""></dt>
               <dd><a href="#">800x600</a></dd>
               <dd><a href="#">1024x768</a></dd>
          </dl>
              <dl>
               <dt><img src="image2.jpg" width="100" height="100"></dt>
               <dd><a href="#">800x600</a></dd>
               <dd><a href="#">1024x768</a></dd>
          </dl>
                            <dl>
               <dt><img src="image3.jpg" width="100" height="100"></dt>
               <dd><a href="#">800x600</a></dd>
               <dd><a href="#">1024x768</a></dd>
          </dl>
     </body>
     <script type="text/javascript">
          var thumbnail = null;
          var list = document.getElementsByTagName("dd");
          for(var i = 0; i < list.length; i++)
          {
               if(window.ie)
               {
                    list[i].onmouseover = show_thumbnail;
                    list[i].onmouseout = hide_thumbnail;
                    list[i].onmousemove = move_thumbnail;
               }
               else
               {
                    list[i].addEventListener('mouseover',show_thumbnail,true);
                    list[i].addEventListener('mouseout',hide_thumbnail,true);
                    list[i].addEventListener('mousemove',move_thumbnail,true);
               }
               list[i].parentNode.getElementsByTagName('dt')[0].style.display = 'none';
                     list[i].parentNode.getElementsByTagName('dt')[0].style.position = 'absolute';
          }

          function show_thumbnail(e)
          {
               thumbnail = (window.ie) ? this.parentNode.getElementsByTagName('dt')[0] : e.currentTarget.parentNode.getElementsByTagName('dt')[0];
               thumbnail.style.display = '';
          }

          function hide_thumbnail(e)
          {
               thumbnail.style.display = 'none';
          }

          function move_thumbnail(e)
          {
               if(window.ie)
               {
                    thumbnail.style.top = window.event.clientY + "px";
                    thumbnail.style.left = window.event.clientX + "px";
               }
               else
               {
                    thumbnail.style.top = e.clientY + "px";
                    thumbnail.style.left = e.clientX + "px";
               }
          }
     </script>
</html>
0
 
LVL 3

Expert Comment

by:hemebond
ID: 12253590
I going to need a line number for the error. I still can't break it.

Btw, change the last code block to this:

thumbnail.style.top = e.layerY + "px";
thumbnail.style.left = e.layerX + "px";
0
 

Author Comment

by:matreece
ID: 12256898
Ok, heres the new code.
The error is Line 63 Character 21 'thumbnail.style' is null or not an object.

-------------------------------

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
     <head>
          <title>Q_21156215</title>
  <!--[if IE]><script type="text/javascript">var ie = true;</script><![endif]-->
             <style type="text/css">
               dl     {position:relative;}
          </style>
     </head>
     <body>
          <dl>
               <dt><img src="images/arcadiaInShipyard41024.jpg" width="100" height="100" alt=""></dt>
               <dd><a href="wallpaperPopArcadiaAtSeaSmall.htm">800x600</a></dd>
               <dd><a href="wallpaperPopArcadiaAtSeaLarge.htm">1024x768</a></dd>
          </dl>
              <dl>
               <dt><img src="images/arcadiaInElba1024.jpg" width="100" height="100"></dt>
               <dd><a href="wallpaperPopArcadiaAtSeaSmall.htm">800x600</a></dd>
               <dd><a href="wallpaperPopArcadiaAtSeaLarge.htm">1024x768</a></dd>
          </dl>
                            <dl>
               <dt><img src="images/arcadiaInElba1024.jpg" width="100" height="100"></dt>
               <dd><a href="wallpaperPopArcadiaAtSeaSmall.htm">800x600</a></dd>
               <dd><a href="wallpaperPopArcadiaAtSeaLarge.htm">1024x768</a></dd>
          </dl>
     </body>
     <script type="text/javascript">
          var thumbnail = null;
          var list = document.getElementsByTagName("dd");
          for(var i = 0; i < list.length; i++)
          {
               if(window.ie)
               {
                    list[i].onmouseover = show_thumbnail;
                    list[i].onmouseout = hide_thumbnail;
                    list[i].onmousemove = move_thumbnail;
               }
               else
               {
                    list[i].addEventListener('mouseover',show_thumbnail,true);
                    list[i].addEventListener('mouseout',hide_thumbnail,true);
                    list[i].addEventListener('mousemove',move_thumbnail,true);
               }
               list[i].parentNode.getElementsByTagName('dt')[0].style.display = 'none';
                     list[i].parentNode.getElementsByTagName('dt')[0].style.position = 'absolute';
          }

          function show_thumbnail(e)
          {
               thumbnail = (window.ie) ? this.parentNode.getElementsByTagName('dt')[0] : e.currentTarget.parentNode.getElementsByTagName('dt')[0];
               thumbnail.style.display = '';
          }

          function hide_thumbnail(e)
          {
               thumbnail.style.display = 'none';
          }

          function move_thumbnail(e)
          {
               if(window.ie)
               {
                    thumbnail.style.top = window.event.clientY + "px";
                    thumbnail.style.left = window.event.clientX + "px";
               }
               else
               {
                    thumbnail.style.top = e.layerY + "px";
          thumbnail.style.left = e.layerX + "px";
               }
          }
     </script>
</html>
0
 
LVL 3

Expert Comment

by:hemebond
ID: 12263076
Stupid freakin Internet Explorer. It was calling the move function before it called the over function, so it didn't actually have an object to move.

if(window.ie)
{
      if(!thumbnail) return; // stupid freakin IE
      thumbnail.style.top = window.event.clientY + "px";
      thumbnail.style.left = window.event.clientX + "px";
}
0
 

Author Comment

by:matreece
ID: 12274534
Sorry to be a pain - the error has gone which is now fine.
But when i hover over the top link the thumbnail appears slightly to the left of the cursor but when i hover over the bottom two links the thumbnails seem to fall further down the page away from the cursor.
i've included my new code below.
Do you have any idea why it does that?

---------------------------------

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
     <head>
          <title>Q_21156215</title>
  <!--[if IE]><script type="text/javascript">var ie = true;</script><![endif]-->
             <style type="text/css">
               dl     {position:relative;}
          </style>
     </head>
     <body>
          <dl>
               <dt><img src="image1.jpg" width="100" height="100" alt=""></dt>
               <dd><a href="#">800x600</a></dd>
               <dd><a href="#">1024x768</a></dd>
          </dl>
              <dl>
               <dt><img src="image2.jpg" width="100" height="100"></dt>
               <dd><a href="#">800x600</a></dd>
               <dd><a href="#">1024x768</a></dd>
          </dl>
                            <dl>
               <dt><img src="image3.jpg" width="100" height="100"></dt>
               <dd><a href="#">800x600</a></dd>
               <dd><a href="#">1024x768</a></dd>
          </dl>
     </body>
     <script type="text/javascript">
          var thumbnail = null;
          var list = document.getElementsByTagName("dd");
          for(var i = 0; i < list.length; i++)
          {
               if(window.ie)
               {
                    list[i].onmouseover = show_thumbnail;
                    list[i].onmouseout = hide_thumbnail;
                    list[i].onmousemove = move_thumbnail;
               }
               else
               {
                    list[i].addEventListener('mouseover',show_thumbnail,true);
                    list[i].addEventListener('mouseout',hide_thumbnail,true);
                    list[i].addEventListener('mousemove',move_thumbnail,true);
               }
               list[i].parentNode.getElementsByTagName('dt')[0].style.display = 'none';
                     list[i].parentNode.getElementsByTagName('dt')[0].style.position = 'absolute';
          }

          function show_thumbnail(e)
          {
               thumbnail = (window.ie) ? this.parentNode.getElementsByTagName('dt')[0] : e.currentTarget.parentNode.getElementsByTagName('dt')[0];
               thumbnail.style.display = '';
          }

          function hide_thumbnail(e)
          {
               thumbnail.style.display = 'none';
          }

          function move_thumbnail(e)
          {
               if(window.ie)
               {
                      if(!thumbnail) return;
                      thumbnail.style.top = window.event.clientY + "px";
               thumbnail.style.left = window.event.clientX + "px";
               }
               else
               {
                    thumbnail.style.top = e.layerY + "px";
          thumbnail.style.left = e.layerX + "px";
               }
          }
     </script>
</html>
0
 
LVL 3

Accepted Solution

by:
hemebond earned 150 total points
ID: 12280932
if(window.ie)
{
      if(!thumbnail) return;
      thumbnail.style.top = window.event.y + "px";
      thumbnail.style.left = window.event.x + "px";
}
0
 

Author Comment

by:matreece
ID: 12284712
Many thanks.
Works perfectly.
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question