Home > Article > Web Front-end > jq view preview image example sharing
Moving the mouse over the thumbnail will display the large image of the image, and the large image will follow the mouse movement; or move the mouse over the prompt text to display the image. It also contains a direction discrimination function. Specifically, if the thumbnail is in the left half of the page, the large image will be displayed on the right side of the mouse. If the thumbnail is in the right half of the page, the previewed large image will be on the left side of the mouse. show.
html structure
<a href="xx.jpg">缩略图</a>
When the mouse covers 3499910bf9dac5ae3c52d5ede7383485</a> ;
Get the preview image address
Preview image structure
<p id='preview'><p> <img src='"+$(this).attr('href')+"' /> <p>"+$(this).attr('title')+"</p></p></p>
Add to body, use absolute positioning
- Plug-in development
Because I wanted to try the plug-in development model, I wrote
$.fn.preview=function(){ ...... }
jQuery.fn = jQuery.prototype. For prototype
, each jq object can be used
<style>.imgbox{ margin-top: 150px; text-align: center;}.imgbox img { display: inline-block; width: 250px; height: 144px;}</style><script> $(function(){ $("a.preview").preview(); //页面加载完后执行 });</script><body> <p class="page"> <p class="imgbox"> <a class="preview" href="./img/cool_couple_dark.jpg" title="cool cuple"><img src="./img/cool_couple_dark.jpg" alt="cool couple"></a> <a class="preview" href="./img/cool_couple_dark.jpg" title="cool cuple"><img src="./img/cool_couple_dark.jpg" alt="cool couple"></a> <a class="preview" href="./img/cool_couple_dark.jpg" title="cool cuple"><img src="./img/cool_couple_dark.jpg" alt="cool couple"></a> <a class="preview" href="./img/cool_couple_dark.jpg" title="cool cuple"><img src="./img/cool_couple_dark.jpg" alt="cool couple"></a> <a class="preview" href="./img/cool_couple_dark.jpg" title="cool cuple">查看</a> </p> </p></body>
jquery-imgpreview.js
(function($){ $.fn.preview=function () { $(this).each(function () { var xOffset = 10; var yOffset = 20; var screenW =$(window).width(); $(this).hover(function (e) { var imgsrc= $(this).attr("href") if(/.png$|.gif$|.jpg$|.bmp$/.test(imgsrc)){ $('body').append("<p id='preview'><p><img src='"+imgsrc+"' /><p>"+$(this).attr('title')+"</p></p></p>"); $('#preview').css({ width:'325px', position:'absolute', left:e.pageX+xOffset+'px', top:e.pageY+yOffset+'px', backgroundColor:"#eeeeee", padding:"4px", border:"1px solid #f3f3f3", zIndex:1000 }), $('#preview > p > img').css({ width:'100%', height:'100%' }) } },function () { $('#preview').remove(); }).mousemove(function(e){ $("#preview").css("top",e.pageY+ "px") if(e.pageX < screenW/2){ $("#preview").css("left",(e.pageX + xOffset) + "px").css("right","auto"); }else{ $("#preview").css("right",(screenW - e.pageX + xOffset) + "px").css("left","auto"); } }); }) }})(jQuery)
The above is the detailed content of jq view preview image example sharing. For more information, please follow other related articles on the PHP Chinese website!