使用jquery实现放大镜效果

首先,我们讲解一下放大镜效果的实现方式:

方法一:准备一张高像素的大图,当鼠标放到原图上,加载显示大图的对应位置。

方法二:对原图片进行放大,也就是调整原图的长和宽。

上面我们介绍了通过两种方式实现放大镜效果,接下来,我们将以上的两种方式应用到我们的jQuery插件中。

首先,我们需要一个img元素显示原图对象,还需要一个容器作为显示框;显示框里面存放大图对象。当鼠标移动到原图上时,通过对大图进行绝对定位来显示对应的部位,实现类似放大镜的效果。

接下来,让我们定义Index.html页面,具体实现如下:

<!DOCTYPE html> <html> <head> <title>放大镜效果</title> <meta charset="utf-8"/> <meta content=""/> <meta content=""/> <link type="text/css" href="https://www.jb51.net/css/reset.css"/> <link type="text/css" href="https://www.jb51.net/css/main.css"/> <script type="text/javascript" src="https://www.jb51.net/js/jquery-1.11.1.js"></script> <script type="text/javascript" src="https://www.jb51.net/js/jquery.imageZoom.js"></script> </head> <body> <div> <div></div> <img src="https://www.jb51.net/images/iphone.jpg" /> </div> <div> <div></div> <img src="https://www.jb51.net/images/img5.jpg"/> </div> <script type="text/javascript"> $(function(){ $(".magnify").hover(function(){ $.fn.imageZoom({ small :"small", large : "large", magnify: "magnify" }); },function(){}) $(".magnify_02").hover(function(){ $.fn.imageZoom({ small : "small_02", large : "large_02", magnify: "magnify_02" }); },function(){}) }) </script> </body> </html>

css样式:

.magnify {width: 200px; margin: 50px auto; position: relative;} .large {width: 175px; height: 175px;position: absolute;border-radius: 100%;z-index:99;box-shadow: 0 0 0 7px rgba(255, 255, 255, 0.85), 0 0 7px 7px rgba(0, 0, 0, 0.25), inset 0 0 40px 2px rgba(0, 0, 0, 0.25);background: url('../images/iphone.jpg') no-repeat;display: none;} .small { display: block; } .magnify_02 {width: 400px; margin: 50px auto; position: relative;} .large_02 {width: 175px; height: 175px;position: absolute;border-radius: 100%;z-index:99;box-shadow: 0 0 0 7px rgba(255, 255, 255, 0.85), 0 0 7px 7px rgba(0, 0, 0, 0.25), inset 0 0 40px 2px rgba(0, 0, 0, 0.25);background: url('../images/iphone.jpg') no-repeat;display: none;} .small_02 { display: block; }

mousemove事件
接下来,我们通过jQuery插件形式来实现放大镜效果,当鼠标移动到small对象上方时,就会在large对象中显示大图的对应位置,这就涉及到mousemove事件了,所以,我们需要实现mousemove事件的监听方法。

实现jquery.imagezoom.js插件:

(function($) { $.fn.imageZoom = function(options) { var defaults = { scaling: 0.3, small :"small", large : "large", magnify:"magnify" }; options = $.extend(defaults, options), native_width = 0, native_height = 0, current_width = 0, current_height = 0, magnify="."+options.magnify; small="."+options.small; $small=$(small); large="."+options.large; $large=$(large); $(magnify).mousemove(function(e) { var image_object = new Image(); image_object.src = $small.attr('src'); if(!+[1,]) { native_height = image_object.height; native_width = image_object.width; } else { image_object.onload = function() { image_object.onload = null; native_height = image_object.height; native_width = image_object.width; } } current_height = $small.height(); current_width = $small.width(); var magnify_offset = $(this).offset(); var mx = e.pageX - magnify_offset.left; var my = e.pageY - magnify_offset.top; if (mx < $(this).width() && my <$(this).height() && mx > 0 && my > 0) { $large.fadeIn(100); } else { $large.fadeOut(100); } if ($large.is(":visible")) { var rx = Math.round(mx / $small.width() * native_width - $large.width() / 2) * -1, ry = Math.round(my / $small.height() * native_height - $large.height() / 2) * -1, bgp = rx + "px " + ry + "px", px = mx - $large.width() / 2, py = my - $large.height() / 2; $large.css({ left: px, top: py, backgroundPosition: bgp }); } //} }); }; })(jQuery);

注释:当鼠标移动到magnify对象中,我们需要获取鼠标在magnify中的相对坐标位置,这里我们把相对坐标定义为(mx,my),通过上图我们知道相对坐标等于(pageX - offsetLeft, pageY - offsetTop)。

现在,我们已经获取鼠标在magnify对象中的坐标值,接下来,需要获取对应大图的相应坐标,这里我们把大图的对应坐标定义为(rx,ry),我们可以通过比例关系获取(rx,ry)的值。

mx / small.width (原图的宽)= rx / native_width(大图的宽)

my / small.height (原图的长)= ry / native_height(大图的长)

通过上面的比例关系,我们知道大图的坐标(rx,ry)等于(mx/small.widthnative_width, my/small.heightnative_height)。

mousewheel事件
前面,我们通过mousemove事件来放大图片,这里我们将通过鼠标的滚轮事件实现图片放大效果。

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wgsxxs.html