找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
积分等级发帖收益的说明江阴论坛帮助汇总江阴论坛管理规则(必看)江阴论坛版主招聘中江阴论坛已运行
查看: 1724|回复: 0

[技术分享] 放大镜 - 移动镜片

[复制链接]
发表于 2010-5-18 13:54:42 | 显示全部楼层 |阅读模式
缩略图和镜片组成的 DOM 结构如下.
  1. <a id="thumb" href="#">
  2.         <img src="http://img.alibaba.com/photo/291909368/Free-Shipping-Popular-ladies-shoes-Tote-shoes-paypal-accept-201001.jpg" alt="Thumbnail" />
  3.         <span id="glass"></span>
  4. </a>
复制代码
  1. /**
  2. * 获取镜片在放大目标元素上的位置
  3. * @param ev                触发的事件
  4. * @param thumb                缩略图对象
  5. * @param glass                镜片对象
  6. * @return                        x:镜片在放大目标元素上的横向位置, y:镜片在放大目标元素上的纵向位置
  7. */
  8. function getGlassOffset(ev, thumb, glass) {
  9.         var offset = {
  10.                 left:0,
  11.                 top:0
  12.         };

  13.         // 偏移量
  14.         var thumbOffset = getCumulativeOffset(thumb);
  15.         // 鼠标在页面上的位置
  16.         var mousePoint = getMousePoint(ev);
  17.         // 镜片实际尺寸
  18.         var glassSize = getSize(glass);
  19.         // 简缩图实际尺寸
  20.         var thumbSize = getSize(thumb);

  21.         // 光标横向位置
  22.         var cursorX = mousePoint.x - thumbOffset.left;
  23.         // 镜片横向偏移量
  24.         offset.left = cursorX - glassSize.width / 2;
  25.         if(offset.left < 0) {
  26.                 offset.left = 0;
  27.         } else if(offset.left > thumbSize.width - glassSize.width) {
  28.                 offset.left = thumbSize.width - glassSize.width;
  29.         }

  30.         // 光标纵向位置
  31.         var cursorY = mousePoint.y - thumbOffset.top;
  32.         // 镜片纵向偏移量
  33.         offset.top = cursorY - glassSize.height / 2;
  34.         if(offset.top < 0) {
  35.                 offset.top = 0;
  36.         } else if(offset.top > thumbSize.height - glassSize.height) {
  37.                 offset.top = thumbSize.height - glassSize.height;
  38.         }

  39.         return offset;
  40. }
复制代码
原文:http://www.neoease.com/javascript-magnifier-move-glass/
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|江阴人家

GMT+8, 2024-4-26 22:46 , Processed in 0.017057 second(s), 16 queries .

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表