HTML hyperlink with mouse over image

I have an Html hyperlink. I need to link this hyperlink to another page. When I put my mouse over the link. It should show an image. how to do it

0


source to share


3 answers


It depends on where you need to display the image. If you are looking for something along a line next to or behind a link, you can accomplish this via CSS using a hover link background image:

a:link
{
   background-image:none;
}

a:hover
{
   background-image:url('images/icon.png');
   background-repeat:no-repeat;
   background-position:right;
   padding-right:10px /*adjust based on icon size*/
}

      

I did it from my head, so you may need to make minor adjustments.



If you want to show the image somewhere else on the page, you can accomplish this with javascript to hide / show the image on the mouseover event.

If that doesn't solve your problem, you may be able to provide additional information to help each answer the correct answer.

+3


source


you can do it with javascript ..

This will create a square that follows the mouse over the div or over the element.

Create a .js file with this content:

 

var WindowVisible = null;
function WindowShow() { 
    this.bind = function(obj,url,height,width) {
        obj.url = url;
        obj.mheight = height;
        obj.mwidth = width;
        obj.onmouseover = function(e) {
            if (WindowVisible == null) {
                if (!e) e = window.event;
                var tmp = document.createElement("div");
                tmp.style.position = 'absolute';
                tmp.style.top = parseInt(e.clientY + 15) + 'px';
                tmp.style.left = parseInt(e.clientX + 15) + 'px';
                    var iframe = document.createElement('iframe');
                    iframe.src = this.url;
                    iframe.style.border = '0px';
                    iframe.style.height = parseInt(this.mheight)+'px';
                    iframe.style.width = parseInt(this.mwidth)+'px';
                    iframe.style.position = 'absolute';
                    iframe.style.top = '0px';
                    iframe.style.left = '0px';
                tmp.appendChild(iframe);
                tmp.style.display = 'none';
                WindowVisible = tmp;
                document.body.appendChild(tmp);
                tmp.style.height = parseInt(this.mheight) + 'px';
                tmp.style.width = parseInt(this.mwidth) + 'px';
                tmp.style.display = 'block';
            }
        }
        obj.onmouseout = function() {
            if (WindowVisible != null) {
                document.body.removeChild(WindowVisible);
                WindowVisible = null;
            }
        }
        obj.onmousemove = function(e) {
            if (!e) e = window.event;
            WindowVisible.style.top = parseInt(e.clientY + 15) + 'px';
            WindowVisible.style.left = parseInt(e.clientX + 15) + 'px';
        }
    }
}



      

Then in your html do the following:

  • Include the .js file <script type="text/javascript" src="myfile.js"></script>

  • Put on your webpage:



 

<script type="text/javascript">
            var asd = new WindowShow();
            asd.bind(document.getElementById('go1'),'IMAGE URL HERE!',400,480);
</script>


      

Here is the complete implementation in HTML:

<html>
<head>
    <title>test page</title>
    <style>
        div.block { width: 300px; height: 300px; background-color: red; }
        iframe { border: 0px; padding: 0px; margin: 0px; }
    </style>
    <script type="text/javascript" src="window_show.js"></script>
</head>
<body>
    <div id="go1" style="background-color: red; width: 200px; height: 200px;"></div>

    <script type="text/javascript">
        var asd = new WindowShow();
        asd.bind(document.getElementById('go1'),'IMAGE URL HERE!',400,480);
    </script>
</body>

      

bye bye!

0


source


You can do it easily with jquery:

$("li").hover(
  function () {
    $(this).append($("<img src="myimage.jpg"/>"));
  }, 
  function () {
    $(this).find("img:last").remove();
  }
);

      

Some more complete examples that have actually been tested: http://docs.jquery.com/Events/hover

0


source







All Articles