1.1.1 摘要

相信大家都见过或使用过放大镜效果,甚至实现过该效果,它一般应用于放大查看商品图片,一些电商网站(例如:凡客,京东商城,阿里巴巴等)都有类似的图片查看效果。

在接下来的博文中,我们将向大家介绍通过jQuery实现放大镜效果。

目录

1.1.2 正文

实现原理

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

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

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

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

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

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

  1. <!doctype html>
  2. <html lang="en-US">
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html;charset=utf-8">
  5. <title>jQuery Image Zoom Demo</title>
  6. <meta name="author" content="Jackson Huang">
  7. </head>
  8. <body>
  9. <div class="magnify">
  10. <div class="large"></div>
  11. <img class="small" src="./img/1.jpg" width="700" />
  12. </div>
  13. </body>
  14. </html>

上面,我们定义了small对象用于显示原图,而large对象作为一个显示框用来显示大图的对应位置。

mousemove事件

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

现在,让我们实现jquery.imagezoom.js插件吧!

  1. ;
  2. (function ($) {
  3.  
  4. $.fn.imageZoom = function (options) {
  5.  
  6. // The native width and height of the image.
  7. var native_width = 0,
  8. native_height = 0,
  9. current_width = 0,
  10. current_height = 0,
  11. $small = $(".small"),
  12. $large = $(".large");
  13.  
  14. $(".magnify").mousemove(function (e) {
  15. /* Act on the event */
  16. if (!native_width && !native_height) {
  17. var image_object = new Image();
  18. image_object.src = $small.attr('src');
  19.  
  20. // Gets the image native height and width.
  21. native_height = image_object.height;
  22. native_width = image_object.width;
  23.  
  24. // Gets the image current height and width.
  25. current_height = $small.height();
  26. current_width = $small.width();
  27.  
  28. } else {
  29.  
  30. // Gets .maginfy offset coordinates.
  31. var magnify_offset = $(this).offset(),
  32.  
  33. // Gets coordinates within .maginfy.
  34. mx = e.pageX - magnify_offset.left,
  35. my = e.pageY - magnify_offset.top;
  36.  
  37. // Checks the mouse within .maginfy or not.
  38. if (mx < $(this).width() && my < $(this).height() && mx > 0 && my > 0) {
  39. $large.fadeIn(100);
  40. } else {
  41. $large.fadeOut(100);
  42. } if ($large.is(":visible")) {
  43. /* Gets the large image coordinate by ratio
  44. small.x / small.width = large.x / large.width
  45. small.y / small.height = large.y / large.height
  46. then we need to keep pointer in the centre,
  47. so deduct the half of .large width and height.
  48. */
  49. var rx = Math.round(mx / $small.width() * native_width - $large.width() / 2) * -1,
  50. ry = Math.round(my / $small.height() * native_height - $large.height() / 2) * -1,
  51. bgp = rx + "px " + ry + "px",
  52. px = mx - $large.width() / 2,
  53. py = my - $large.height() / 2;
  54. $large.css({
  55. left: px,
  56. top: py,
  57. backgroundPosition: bgp
  58. });
  59. }
  60.  
  61. }
  62. });
  63. });

上面,我实现了mousemove事件的监听方法,当鼠标移动到magnify对象中,我们需要获取当前鼠标的相对坐标位置,下面我们通过图片讲解如何获取鼠标的相对坐标位置。

相对坐标

  

图1鼠标相对坐标位置

当鼠标移动到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.width*native_width, my/small.height*native_height)。

通过上述的公式,我们可以获取大图对应坐标位置,当鼠标移动到magnify对象中就显示对应位置的大图部位,接下来我们需要实现大图的加载实现了。

background-position属性

在实现大图加载显示之前,首先介绍CSS中背景定位background-position的知识。

图2 CSS background-position

上面,有一个100x100像素的图片它由四种颜色组成,而且每种颜色占50 x50像素,接下来,我们将通过修改该图片CSS的background-position属性值来显示该图片的不同位置。

我们看到在大正方形下有两行小正方形,它们显示的颜色位置都不相同,这里我们通过修改每个div元素CSS的background-position属性值实现的。

例如:第一行的蓝色方形,我们设置CSS的background-position属性为:0px -50px;这相当于原图往上移动50px,第一行的其他方形也通过左右和上下移动实现的。

但第二行的方形就显得更加奇怪了,因为它们都由四种颜色组成,而且颜色的位置都不一样,这究竟是怎样实现的呢?

例如:第二行的第一个方形,我们设置CSS的background-position属性为:25px 25px;这相当于原图向下和向右移动了25px,由于image wrap的作用它会填充剩余位置的颜色。

现在,我们已经了解到了CSS的background-position属性的作用,所以我们通过修改large对象的background-position属性来显示对应的图像部分,具体实现如下:

  1. $large.css({
  2. left: px,
  3. top: py,
  4. backgroundPosition: bgp
  5. });

上面,我们通过加载大图的方式来实现放大镜效果,接下来,我们将介绍通过调整原图的长和宽来实现放大镜效果。

mousewheel事件

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

由于,不同的浏览器有不同的滚轮事件。主要是有三种:onmousewheel(IE 6/7/8)、mousewheel(IE9,Chrome,Safari和Opera)和DOMMouseScroll(只有Firefox支持),关于这三个事件这里不做详细的介绍了。

由于不同浏览器之间存在着差异,为了实现浏览器之间的兼容,所以,我们需要监听以上三种滚轮事件(onmousewheel,mousewheel和DOMMouseScroll),具体实现如下:

  1. $(".magnify").bind('DOMMouseScroll mousewheel onmousewheel', function(e) {
  2. });

上面,我们实现了兼容不同浏览器的滚轮事件监听方法,接下来,判断滚轮向上或向下也要考虑不同浏览器的兼容性,主流的览器(IE、Opera、Safari、Firefox、Chrome)中Firefox 使用detail,其余四类使用wheelDelta;两者只在取值上不一致,代表含义一致,detail与wheelDelta只各取两个值,detail只取±3,wheelDelta只取±120,其中正数表示为向上,负数表示向下。

由于detail和wheelDelta都有两个值表示向上或向下滚动,所以不同浏览器间可以通过以下方式实现兼容,具体实现如下:

  1. $(".magnify").bind('DOMMouseScroll mousewheel onmousewheel', function(e) {
  2.  
  3. // cross-browser wheel delta
  4. var e = window.event || e; // old IE support.
  5. var delta = Math.max(-1, Math.min(1, (e.wheelDelta || -e.detail)));
  6. });

上面,我们已经处理了不同浏览器滚轮监听方法,当用户滚动滚轮时需要动态地修改原图的尺寸,这里我们定义缩放比scaling为0.3,也就是说每当用户滚动一下滚轮原图就按0.3的比例进行缩放,具体实现如下:

  1. // Gets the image scaling height and width.
  2. native_height += (native_height * scaling * delta);
  3. native_width += (native_width * scaling * delta);
  4.  
  5. // Update backgroud image size.
  6. $large.css('background-size', native_width + "px " + native_height + "px");

现在,我们已经实现了通过滚轮对图片进行缩放查看的效果,完整的实现如下:

  1. /***********************************
  2. * Author: Jackson Huang
  3. * Blog: http://www.cnblogs.com/rush
  4. * Date: 8/23/2013
  5. * Reference:
  6. * http://www.sitepoint.com/html5-javascript-mouse-wheel/
  7. * http://thecodeplayer.com/walkthrough/magnifying-glass-for-images-using-jquery-and-css3
  8. ***********************************/
  9.  
  10. ;
  11. (function($) {
  12.  
  13. $.fn.imageZoom = function(options) {
  14.  
  15. // The native width and height of the image.
  16. var defaults = {
  17. scaling: 0.3
  18. };
  19.  
  20. // Combines object defaults and options.
  21. options = $.extend(defaults, options),
  22. native_width = 0,
  23. native_height = 0,
  24. current_width = 0,
  25. current_height = 0,
  26. $small = $(".small"),
  27. $large = $(".large");
  28.  
  29. $(".magnify").mousemove(function(e) {
  30. /* Act on the event */
  31. if (!native_width && !native_height) {
  32. var image_object = new Image();
  33. image_object.src = $small.attr('src');
  34.  
  35. // Gets the image native height and width.
  36. native_height = image_object.height;
  37. native_width = image_object.width;
  38.  
  39. // Gets the image current height and width.
  40. current_height = $small.height();
  41. current_width = $small.width();
  42.  
  43. } else {
  44.  
  45. // Gets .maginfy offset coordinates.
  46. var magnify_offset = $(this).offset(),
  47.  
  48. // Gets coordinates within .maginfy.
  49. mx = e.pageX - magnify_offset.left,
  50. my = e.pageY - magnify_offset.top;
  51.  
  52. // Checks the mouse within .maginfy or not.
  53. if (mx < $(this).width() && my < $(this).height() && mx > 0 && my > 0) {
  54. $large.fadeIn(100);
  55. } else {
  56. $large.fadeOut(100);
  57. }
  58. if ($large.is(":visible")) {
  59. /* Gets the large image coordinate by ratio
  60. small.x / small.width = large.x / large.width
  61. small.y / small.height = large.y / large.height
  62. then we need to keep pointer in the centre,
  63. so deduct the half of .large width and height.
  64. */
  65. var rx = Math.round(mx / $small.width() * native_width - $large.width() / 2) * -1,
  66. ry = Math.round(my / $small.height() * native_height - $large.height() / 2) * -1,
  67. bgp = rx + "px " + ry + "px",
  68. px = mx - $large.width() / 2,
  69. py = my - $large.height() / 2;
  70. $large.css({
  71. left: px,
  72. top: py,
  73. backgroundPosition: bgp
  74. });
  75. }
  76.  
  77. }
  78. });
  79.  
  80. $(".magnify").bind('DOMMouseScroll mousewheel onmousewheel', function(e) {
  81. var image_object = new Image();
  82. image_object.src = $large.attr('src');
  83.  
  84. // cross-browser wheel delta
  85. e = window.event || e; // old IE support.
  86. var delta = Math.max(-1, Math.min(1, (e.wheelDelta || -e.detail)));
  87.  
  88. // Gets the image scaling height and width.
  89. native_height += (native_height * defaults.scaling * delta);
  90. native_width += (native_width * defaults.scaling * delta);
  91.  
  92. // The image can't smaller than the original.
  93. if (native_height < current_height) {
  94. native_height = current_height;
  95. }
  96.  
  97. if (native_width < current_width) {
  98. native_width = current_width;
  99. }
  100.  
  101. // console.log("native_height: " + native_height + " native_width: " + native_width);
  102.  
  103. // Gets .maginfy offset coordinates.
  104. var magnify_offset = $(this).offset(),
  105. mx = e.pageX - magnify_offset.left,
  106. my = e.pageY - magnify_offset.top;
  107.  
  108. // Update backgroud image size.
  109. $large.css('background-size', native_width + "px " + native_height + "px");
  110.  
  111. /* Gets the large image coordinate by ratio
  112. small.x / small.width = large.x / large.width
  113. small.y / small.height = large.y / large.height
  114. then we need to keep pointer in the centre,
  115. so deduct the half of .large width and height.
  116. */
  117. var rx = Math.round(mx / $small.width() * native_width - $large.width() / 2) * -1,
  118. ry = Math.round(my / $small.height() * native_height - $large.height() / 2) * -1,
  119. bgp = rx + "px " + ry + "px",
  120. px = mx - $large.width() / 2,
  121. py = my - $large.height() / 2;
  122.  
  123. $large.css({
  124. left: px,
  125. top: py,
  126. backgroundPosition: bgp
  127. });
  128. });
  129. };
  130. })(jQuery);

 

图3 放大镜效果

上面,我们实现了放大镜效果,当我们鼠标停留在图片上方会自动放大图片的相应部位,当然我们可以通过滚轮调整放大的比例。

1.1.3 总结

在本博文中,我们介绍了如何实现放大镜效果,总的来说,我们可以通过两种方式实现放大镜效果,而且在博文中都给出了详细的介绍,通过mousemove事件实现加载大图的效果,mousewheel事件实现动态修改原图的尺寸。

这只是一个简单的程序,我们还有很大的改善空间,提供一个内容丰富和功能强大的程序是我们的目标。

参考

Demo

jQuery实现放大镜效果的更多相关文章

  1. 使用jquery实现放大镜效果

    原文:使用jquery实现放大镜效果 实现原理 首先,我们讲解一下放大镜效果的实现方式: 方法一:准备一张高像素的大图,当鼠标放到原图上,加载显示大图的对应位置. 方法二:对原图片进行放大,也就是调整 ...

  2. js、jquery实现放大镜效果

    在一些电商网站的商品详情页面,都会有放大镜效果,实现起来并不是很困难,今天用了两个小时,写了一个放大镜效果的实例,来分享给大家! 实现的效果大概是这个样子的 预览 先来看一下效果吧,点击下面的链接预览 ...

  3. 【Demo】jQuery 图片放大镜效果——模仿淘宝图片放大效果

    实现功能: 模仿淘宝图片放大效果,鼠标移动到小图片的某一处,放大镜对应显示大图片的相应位置. 实现效果: 实现代码: <!DOCTYPE html> <html> <he ...

  4. Jquery版放大镜效果

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  5. 用JavaScript中jQuery编写放大镜效果

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  6. 用jquery实现放大镜效果

    ----css代码--- *{margin:0;padding:0;} .showimg{position:relative;width:450px;height:420px;border:1px s ...

  7. 关于jQuery中实现放大镜效果

    1.1.1 摘要 相信大家都见过或使用过放大镜效果,甚至实现过该效果,它一般应用于放大查看商品图片,一些电商网站(例如:凡客,京东商城,阿里巴巴等)都有类似的图片查看效果. 在接下来的博文中,我们将向 ...

  8. jQuery实现图片放大镜效果

    实现图片放大镜的原理: 给放大镜元素一个对应的html元素为<div class='right'> 设置这个div的宽高固定为某个值(350px,350px) 设置div的css为超出部分 ...

  9. jquery放大镜效果

    <!DOCTYPE html> <html lang="zh-cn"> <head> <meta charset="utf-8& ...

随机推荐

  1. Vue组件之自定义表单组件

    今天又看了一遍vue的文档,记得之前学习的时候,官方文档中有提过,v-model指令是一个语法糖,做两件事,一个是给表单控件元素绑定value,第二个是当输入时更新绑定的值,不过后来在"表单 ...

  2. Windows下的UDP爆了10054--远程主机强迫关闭了一个现有的连接

    原文地址:http://www.cnblogs.com/pasoraku/p/5612105.html 故事是这样的. 前几天在网上逛,看到了一个漂亮的坦克模型. 我觉得这个坦克可以做一个游戏,那需要 ...

  3. XML文件的读写

    using System; using System.Collections.Generic; using System.Xml; namespace COMMON { public class Xm ...

  4. Oracle 11g 导库导不出空表问题

    Oracle11g 会遇到这样的问题,用exp整库导出的时候,会遇到这样的问题,库里的空表没办法导出 select * from all_all_tables aa where aa.owner='U ...

  5. C语言教学--二维数组和指针的理解

    对于初学者对二维数组和指针的理解很模糊, 或者感觉很难理解, 其实我们和生活联系起来, 这一切都会变得清晰透彻. 我们用理解一维数组的思想来理解二维数组, 对于一维数组,每个箱子里存放的是具体的苹果, ...

  6. 和以往印象不同的Java

    Java编程概述 一个Java源文件至多有一个public类,但是可以有很多class的定义 public static void main (String args[]) public 代表公共的, ...

  7. leaflet创建简单地图

    一.leaflet介绍: 1.Leaflet 是一个为建设移动设备友好的互动地图,而开发的现代的.开源的 JavaScript 库.它是由 Vladimir Agafonkin 带领一个专业贡献者团队 ...

  8. 如何用70行Java代码实现深度神经网络算法(转)

    对于现在流行的深度学习,保持学习精神是必要的——程序员尤其是架构师永远都要对核心技术和关键算法保持关注和敏感,必要时要动手写一写掌握下来,先不用关心什么时候用到——用不用是政治问题,会不会写是技术问题 ...

  9. Eclipse JAVA项目的 目录结构 和 导入

    说明:本文所有测试以java工程为例: 1. Eclipse下的java工程目录 eclipse的基本工程目录叫做workspace,每个运行时的eclipse实例只能对应一个workspace,也就 ...

  10. android 的闪屏效果

    android的闪屏效果,就是我们刚开始启动应用的时候弹出的界面或者动画,过2秒之后自动的跳转到主界面. 其实,实现这个效果很简单,使用Handler对象的postDelayed方法就可以实现.在这个 ...