注意:里边的两张图片(一大一小)可以自己添加,JQ采用jquery-1.11.3.js版,也可自行调换。

HTML代码:

<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title>jQuery放大镜</title>
<link rel="stylesheet" href="css/reset.min.css"/>
<link rel="stylesheet" href="css/index.css"/>
</head>
<body>
<div class="wrapper">
<div class="small">
<img src="img/1.jpg" alt=""/>
<div class="mask"></div>
</div>
<div class="big">
<img src="img/2.jpg" alt=""/>
</div>
</div>
<script src="js/jquery-1.11.3.js"></script>
<script src="js/index.js"></script>
</body>
</html>

CSS代码:

@charset "UTF-8";
.wrapper{
width: 800px;
margin: 50px auto;
position: relative;
}
.wrapper .small{
position: absolute;
top:;
left:;
width: 350px;
height: 350px;
border: 1px solid #000000;
box-shadow: 3px 3px 5px #aaa;
}
.wrapper .small img{
width: 100%;
height: 100%;
}
.wrapper .small .mask{
display: none;
position: absolute;
top:;
left:;
width: 150px;
height: 150px;
background-color: #000000;
opacity: .4;
cursor: move;
} .wrapper .big{
display: none;
position: absolute;
overflow: hidden;
top:;
left: 355px;
border: 1px solid #000000;
box-shadow: 3px 3px 5px #aaa;
}
.wrapper .big img{
position: absolute;
width: 800px;
height: 800px;
top:;
left:;
}

reset.min.css文件:

body,h1,h2,h3,h4,h5,h6,hr,p,blockquote,dl,dt,dd,ul,ol,li,button,input,textarea,th,td{margin:;padding:}body{font-size:12px;font-style:normal;font-family:"\5FAE\8F6F\96C5\9ED1",Helvetica,sans-serif}small{font-size:12px}h1{font-size:18px}h2{font-size:16px}h3{font-size:14px}h4,h5,h6{font-size:100%}ul,ol{list-style:none}a{text-decoration:none;background-color:transparent}a:hover,a:active{outline-width:;text-decoration:none}table{border-collapse:collapse;border-spacing:}hr{border:;height:1px}img{border-style:none}img:not([src]){display:none}svg:not(:root){overflow:hidden}html{-webkit-touch-callout:none;-webkit-text-size-adjust:100%}input,textarea,button,a{-webkit-tap-highlight-color:rgba(0,0,0,0)}article,aside,details,figcaption,figure,footer,header,main,menu,nav,section,summary{display:block}audio,canvas,progress,video{display:inline-block}audio:not([controls]),video:not([controls]){display:none;height:}progress{vertical-align:baseline}mark{background-color:#ff0;color:#000}sub,sup{position:relative;font-size:75%;line-height:;vertical-align:baseline}sub{bottom:-0.25em}sup{top:-0.5em}button,input,select,textarea{font-size:100%;outline:}button,input{overflow:visible}button,select{text-transform:none}textarea{overflow:auto}button,html [type="button"],[type="reset"],[type="submit"]{-webkit-appearance:button}button::-moz-focus-inner,[type="button"]::-moz-focus-inner,[type="reset"]::-moz-focus-inner,[type="submit"]::-moz-focus-inner{border-style:none;padding:}button:-moz-focusring,[type="button"]:-moz-focusring,[type="reset"]:-moz-focusring,[type="submit"]:-moz-focusring{outline:1px dotted ButtonText}[type="checkbox"],[type="radio"]{box-sizing:border-box;padding:}[type="number"]::-webkit-inner-spin-button,[type="number"]::-webkit-outer-spin-button{height:auto}[type="search"]{-webkit-appearance:textfield;outline-offset:-2px}[type="search"]::-webkit-search-cancel-button,[type="search"]::-webkit-search-decoration{-webkit-appearance:none}::-webkit-input-placeholder{color:inherit;opacity:.54}::-webkit-file-upload-button{-webkit-appearance:button;font:inherit}.clear:after{display:block;height:;content:"";clear:both}

JS代码:

/**
* Created by 35107 on 2017/5/8.
*/
$(function () {
var smallL, smallT, minL = 0, minT = 0, maxL, maxT;
var $wrapper = $('.wrapper'),
$small = $wrapper.children('.small'),
$mask = $small.children('.mask'),
$big = $wrapper.children('.big'),
$curImg = $small.children('img'),
$bigImg = $big.children('img');
var multiple = 2 / 3, n = null,
maskW = $curImg.width() * multiple,
maskH = $curImg.height() * multiple,
bigW = $bigImg.width() * multiple,
bigH = $bigImg.width() * multiple;
$mask.css({width: maskW, height: maskH});
$big.css({width: bigW, height: bigH}); $small.on({
mouseenter: function (e) {
$mask.css('display', 'block');
$big.css('display', 'block');
setPos(e);
},
mousemove: function (e) {
setPos(e);
},
mouseleave: function () {
$mask.css('display', 'none');
$big.css('display', 'none');
}
}); function setPos(e) {
if (!smallL) {
smallL = $small.offset().left;
smallT = $small.offset().top;
maxL = $small.innerWidth() - maskW;
maxT = $small.innerHeight() - maskW;
n = ($bigImg.width() - bigW ) / maxL;
} var curL = e.clientX - smallL - maskW / 2,
curT = e.clientY - smallT - maskH / 2;
curL = curL < minL ? minL : (curL > maxL ? maxL : curL);
curT = curT < minT ? minT : (curT > maxT ? maxT : curT);
$mask.css({left: curL, top: curT});
$bigImg.css({left: -curL * n, top: -curT * n})
} function calculatedWidth() { }
});

最新文章

  1. Velocity语法--转载
  2. SQL Server 2008 备份改进版
  3. 20160502-struts2入门--ognl表达式
  4. 关于封装unity3d的dll时候的进一步总结
  5. SpringMVC03controller中定义多个方法
  6. 项目中那些事|ListView中嵌套ListView问题
  7. centos6.5 ssh免密码登陆
  8. 浅淡python中的with,上下文管理器
  9. js操作css样式,null和undefined的区别?
  10. 小程序实践(五):for循环绑定item的点击事件
  11. Tomcat 部署及配置
  12. 不要在 foreach 循环里进行元素的 remove/add 操作。remove 元素请使用 Iterator 方式,如果并发操作,需要对 Iterator 对象加锁
  13. 运营商挂时长神器,批量导入账号,导出账号状态,随机修改MAC地址
  14. python 5
  15. java 从一个工程action 跳转到另外一个工程action
  16. UML标准建模语言与应用实例
  17. 终于明白vim 和 grep 中 的正则表达式的用法, vim 正则表达式 和grep基本正则表达式 几乎一样
  18. 转 kafka架构简介
  19. 深圳Uber司机本周(7.13-7.19凌晨4:00)的奖励政策
  20. javascript面试题(一)(转载)

热门文章

  1. Swift学习笔记(10)--枚举
  2. HDU 4588 Count The Carries 数位DP || 打表找规律
  3. Android开发系列(十六):【Android小游戏成语连连看】第二篇
  4. KendoUi中KendoDropDownList控件的使用——三级级联模块的实现
  5. vim 技巧之用宏命令批量处理文件
  6. 关于ajax访问express服务器的跨域问题
  7. LA 2531 The K-league 最大流
  8. OpenCV —— 直方图与匹配
  9. Oracel 格式化日期 to_char()
  10. Codefroces Educational Round 26 837 B. Flag of Berland