Easy Image

by NinjaSk8ter

HTML

<h1>Easy Image Preview with jQuery</h1>
    
   <ul>
      <li><a href="http://i.imgur.com/spDGI.jpg" class="preview" title="Lake and a mountain"><img src="http://i.imgur.com/mE3o2.jpg" alt="gallery thumbnail" /></a></li>
      <li><a href="http://i.imgur.com/g6kTp.jpg" class="preview" title="Fly fishing"><img src="http://i.imgur.com/Grt4j.jpg" alt="gallery thumbnail" /></a></li>
      <li><a href="http://i.imgur.com/LrLS6.jpg" class="preview" title="Autumn"><img src="http://i.imgur.com/joKhp.jpg" alt="gallery thumbnail" /></a></li>
      <li><a href="http://i.imgur.com/gXt5x.jpg" class="preview" title="Skiing on a mountain"><img src="http://i.imgur.com/C8Ilw.jpg" alt="gallery thumbnail" /></a></li>
   </ul>

CSS

body {
    margin:0;
    padding:10px;
    background:#fff;
    font:80% Arial, Helvetica, sans-serif;
    color:#555;
    line-height:180%;
}
h1{
    font-size:180%;
    font-weight:normal;
    color:#555;
}
h2{
    clear:both;
    font-size:160%;
    font-weight:normal;
    color:#555;
    margin:0;
    padding:.5em 0;
}
a{
    text-decoration:none;
    color:#f30;    
}
p{
    clear:both;
    margin:0;
    padding:.5em 0;
}
pre{
    display:block;
    font:100% "Courier New", Courier, monospace;
    padding:10px;
    border:1px solid #bae2f0;
    background:#e3f4f9;    
    margin:.5em 0;
    overflow:auto;
    width:800px;
}
img{
    border:none;
}
ul,li{
    margin:0;
    padding:0;
}
li{
    list-style:none;
    float:left;
    display:inline;
    margin-right:10px;
}
#preview{
    position:absolute;
    border:1px solid #ccc;
    background:#333;
    padding:5px;
    display:none;
    color:#fff;
}

JavaScript

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

this.imagePreview = function(){    
    /* CONFIG */
        
        xOffset = 10;
        yOffset = 30;
        
        // these 2 variable determine popup's distance from the cursor
        // you might want to adjust to get the right result
        
    /* END CONFIG */
    $("a.preview").hover(function(e){
        this.t = this.title;
        this.title = "";    
        var c = (this.t != "") ? "<br/>" + this.t : "";
        $("body").append("<p id='preview'><img src='"+ this.href +"' alt='Image preview' />"+ c +"</p>");                                 
        $("#preview")
            .css("top",(e.pageY - xOffset) + "px")
            .css("left",(e.pageX + yOffset) + "px")
            .fadeIn("fast");                        
    },
    function(){
        this.title = this.t;    
        $("#preview").remove();
    });    
    $("a.preview").mousemove(function(e){
        $("#preview")
            .css("top",(e.pageY - xOffset) + "px")
            .css("left",(e.pageX + yOffset) + "px");
    });            
};