实现jquery放大镜的两种方法


Posted in jQuery onFebruary 22, 2018

jquery写的两种放大镜效果,没有使用到插件。调理和思路清晰。不是使用面向对象方式写的,初学者较容易看懂。废话不多说,看代码。图片这里就不上传了,大家自己找下。最好是找到比例的,这样效果比较好。

<body> 
  <div id="father"> 
    <div id="container"> 
      <img src="img/400_1.jpg" style="display: block;"> 
      <img src="img/400_2.jpg" > 
      <div class="shade"></div> 
    </div> 
    <div class="small first"><img src="img/50_1.jpg"></div> 
    <div class="small second"><img src="img/50_2.jpg"></div> 
  </div> 
   
  <div class="big"> 
    <img src="img/800_1.jpg" style="display: block;"> 
    <img src="img/800_2.jpg"> 
  </div> 
</body>

css代码

*{padding: 0; margin: 0;} 
  #father .small{width: 50px; height: 50px; border: 2px solid #ccc; bottom: 0; position: absolute;} 
  #father .second{left: 70px;} 
  .third{left: 140px;} 
  #father{position: relative; top: 100px; left: 50px; height: 460px;} 
  #container{position: absolute; width: 400px; height: 400px;} 
  #container img{position: absolute; display: none;} 
 
  .shade{width: 200px; height: 200px; position: absolute; background: #000; opacity: 0.4; top: 0; 
    left: 0; display: none;} 
  .big{width: 400px; height: 400px; position: absolute; top: 100px; overflow: hidden; left: 500px; display: none;} 
  .big img{width: 800px; height: 800px; position: absolute; display: none;}

js代码

<script type="text/javascript" src='js/jquery-1.12.4.min.js'></script> 
  <script type="text/javascript"> 
    $(function () { 
 
      changePic('.first',0); 
      changePic('.second',1); 
 
      var shadeWidth = $('.shade').width(),//阴影的宽度 
        shadeHeight = $('.shade').height(),//阴影的高度 
        middleWidth = $('#container').width(),//容器的宽度 
        middleHeight = $('#container').height(),//容器的高度 
        bigWidth = $('.big').width(),//放大图片盒子的宽度 
        bigHeight = $('.big').height(),//放大图片盒子的高度 
        rateX = bigWidth / shadeWidth,//放大区和遮罩层的宽度比例 
        rateY = bigHeight / shadeHeight;//放大区和遮罩层的高度比例 
 
      //当鼠标移入与移出时阴影与放大去显现/消失 
      $('#container').hover(function() { 
        $('.shade').show(); 
        $('.big').show(); 
      }, function() { 
        $('.shade').hide(); 
        $('.big').hide(); 
      }).mousemove(function(e) {//当鼠标移动时,阴影和放大区图片进行移动 
 
        //记录下光标距离页面的距离 
        var x = e.pageX, 
          y = e.pageY; 
 
        //设置遮罩层的位置 
        $('.shade').offset({ 
          top: y-shadeHeight/2, 
          left: x-shadeWidth/2 
        });    
 
        //获取遮罩层相对父元素的位置 
        var cur = $('.shade').position(), 
          _top = cur.top, 
          _left = cur.left, 
          hdiffer = middleHeight - shadeHeight, 
          wdiffer = middleWidth - shadeWidth; 
 
        if (_top < 0) _top = 0; 
        else if (_top > hdiffer) _top = hdiffer; 
        if (_left < 0) _left = 0; 
        else if (_left > wdiffer) _left =wdiffer; 
 
        //判断完成后设置遮罩层的范围 
        $('.shade').css({ 
          top: _top, 
          left: _left 
        }); 
 
        //设置放大区图片移动 
        $('.big img').css({ 
          top: - rateY*_top, 
          left: - rateX*_left 
        }); 
 
      });; 
 
      //封装的改变图片显示的函数 
      function changePic (element,index) { 
        $(element).click(function() { 
          $('#container img').eq(index).css('display', 'block').siblings().css('display', 'none'); 
          $('.big img').eq(index).css('display', 'block').siblings().css('display', 'none'); 
        }); 
      } 
       
    });

以上是常用的,下面这个是在原图基础上放大的

htm

<body> 
  <div id="father"> 
    <div id="container"> 
      <img src="img/400_1.jpg" style="display: block;"> 
      <img src="img/400_2.jpg" > 
      <img src="img/400_3.jpg" > 
      <div class="shade"> 
        <img src="img/800_1.jpg" style="display: block;"> 
        <img src="img/800_2.jpg"> 
        <img src="img/800_3.jpg"> 
      </div> 
    </div> 
    <div class="small first"><img src="img/50_1.jpg"></div> 
    <div class="small second"><img src="img/50_2.jpg"></div> 
    <div class="small third"><img src="img/50_3.jpg"></div> 
  </div> 
</body>

css代码

*{padding: 0; margin: 0;} 
    #father .small{width: 50px; height: 50px; border: 2px solid #ccc; bottom: 0; position: absolute;} 
    #father .second{left: 70px;} 
    .third{left: 140px;} 
    #father{position: relative; top: 100px; left: 50px; height: 460px;} 
    #container{position: absolute; width: 400px; height: 400px;} 
    #container img{position: absolute; display: none;} 
    .shade{width: 200px; height: 200px; position: absolute; top: 0;left: 0; display: none; border-radius: 50%; overflow: hidden; background: #000;} 
    .shade img{display: none; width: 800px; height: 800px; position: absolute;}

js代码

<span style="white-space:pre">  </span><script type="text/javascript" src='js/jquery-1.12.4.min.js'></script> 
  <script type="text/javascript"> 
    $(function () { 
 
      changePic('.first',0); 
      changePic('.second',1); 
      changePic('.third',2); 
 
      var shadeWidth = $('.shade').width(),//阴影的宽度 
        shadeHeight = $('.shade').height(),//阴影的高度 
        middleWidth = $('#container').width(),//容器的宽度 
        middleHeight = $('#container').height(),//容器的高度 
        bigImgWidth = $('.shade img').width(),//放大图片盒子的宽度 
        bigImgHeight = $('.shade img').height(),//放大图片盒子的高度 
        rateX = bigImgWidth / middleWidth,//放大区和遮罩层的宽度比例2 
        rateY = bigImgHeight / middleHeight;//放大区和遮罩层的高度比例2 
 
      //当鼠标移入与移出时阴影与放大去显现/消失 
      $('#container').hover(function() { 
        $('.shade').show(); 
        $('.big').show(); 
      }, function() { 
        $('.shade').hide(); 
        $('.big').hide(); 
      }).mousemove(function(e) {//当鼠标移动时,阴影和放大区图片进行移动 
         
 
        //记录下光标距离页面的距离 
        var x = e.pageX, 
          y = e.pageY; 
 
        //设置遮罩层的位置 
        $('.shade').offset({ 
          top: y-shadeHeight/2, 
          left: x-shadeWidth/2 
        });    
 
        //获取遮罩层相对父元素的位置 
        var cur = $('.shade').position(), 
          _top = cur.top, 
          _left = cur.left, 
          hdiffer = middleHeight - shadeHeight, 
          wdiffer = middleWidth - shadeWidth; 
 
        if (_top < 0) _top = 0; 
        else if (_top > hdiffer) _top = hdiffer; 
        if (_left < 0) _left = 0; 
        else if (_left > wdiffer) _left =wdiffer; 
 
        //判断完成后设置遮罩层的范围 
        $('.shade').css({ 
          top: _top, 
          left: _left 
        }); 
 
         
        //设置放大区图片移动 
        $('.shade img').css({ 
          top: - _top*rateY*3/2, 
          left: - _left*rateX*3/2 
        }); 
 
      });; 
 
      //封装的改变图片显示的函数 
      function changePic (element,index) { 
        $(element).click(function() { 
          $('#container img').eq(index).css('display', 'block').siblings().css('display', 'none'); 
          $('.shade img').eq(index).css('display', 'block').siblings().css('display', 'none'); 
        }); 
      } 
       
    }); 
<span style="white-space:pre">  </span></script>
jQuery 相关文章推荐
jQuery+PHP+Mysql实现抽奖程序
Apr 12 jQuery
jQuery复合事件用法示例
Jun 10 jQuery
jQuery实现导航栏头部菜单项点击后变换颜色的方法
Jul 19 jQuery
jQuery选择器之属性过滤选择器详解
Sep 28 jQuery
jQuery实现的粘性滚动导航栏效果实例【附源码下载】
Oct 19 jQuery
jQuery实现左右滑动的toggle方法
Mar 03 jQuery
关于jquery中attr()和prop()方法的区别
May 28 jQuery
jQuery实现仿京东防抖动菜单效果示例
Jul 06 jQuery
jQuery实现的监听导航滚动置顶状态功能示例
Jul 23 jQuery
利用jquery和BootStrap实现动态滚动条效果
Dec 03 jQuery
Jquery的Ajax技术使用方法
Jan 21 jQuery
jQuery+ajax实现批量删除功能完整示例
Jun 06 jQuery
jQuery实现滚动到底部时自动加载更多的方法示例
Feb 18 #jQuery
jQuery实现鼠标响应式透明度渐变动画效果示例
Feb 13 #jQuery
jQuery实现鼠标响应式淘宝动画效果示例
Feb 13 #jQuery
jQuery实现的鼠标响应缓冲动画效果示例
Feb 13 #jQuery
jquery+css3实现熊猫tv导航代码分享
Feb 12 #jQuery
jQuery实现定时隐藏对话框的方法分析
Feb 12 #jQuery
JS/jQuery实现DIV延时几秒后消失或显示的方法
Feb 12 #jQuery
You might like
php class中self,parent,this的区别以及实例介绍
2013/04/24 PHP
php笔记之:文章中图片处理的使用
2013/04/26 PHP
PHP网页游戏学习之Xnova(ogame)源码解读(六)
2014/06/23 PHP
ThinkPHP中使用Ueditor富文本编辑器
2015/09/02 PHP
php原生数据库分页的代码实例
2019/02/18 PHP
laravel 关联关系遍历数组的例子
2019/10/10 PHP
Prototype使用指南之selector.js
2007/01/10 Javascript
转换字符串为json对象的方法详解
2013/11/29 Javascript
Javascript获取表单名称(name)的方法
2015/04/02 Javascript
详解nodejs微信jssdk后端接口
2017/05/25 NodeJs
vue 使用Jade模板写html,stylus写css的方法
2018/02/23 Javascript
解决angularJS中input标签的ng-change事件无效问题
2018/09/13 Javascript
JavaScript中callee和caller的区别与用法实例分析
2019/06/28 Javascript
JS数组及对象遍历方法代码汇总
2020/06/16 Javascript
vue实现移动端触屏拖拽功能
2020/08/21 Javascript
python打开网页和暂停实例
2014/09/30 Python
Python中List.index()方法的使用教程
2015/05/20 Python
Python环境下搭建属于自己的pip源的教程
2016/05/05 Python
Python编程使用*解包和itertools.product()求笛卡尔积的方法
2017/12/18 Python
对Python之gzip文件读写的方法详解
2019/02/08 Python
Django框架模板语言实例小结【变量,标签,过滤器,继承,html转义】
2019/05/23 Python
Django中Middleware中的函数详解
2019/07/18 Python
python中通过selenium简单操作及元素定位知识点总结
2019/09/10 Python
Python3+selenium实现cookie免密登录的示例代码
2020/03/18 Python
Keras:Unet网络实现多类语义分割方式
2020/06/11 Python
django使用channels实现通信的示例
2020/10/19 Python
外企C语言笔试题
2013/11/10 面试题
大学生咖啡店创业计划书
2014/01/21 职场文书
正风肃纪剖析材料
2014/02/18 职场文书
法制宣传实施方案
2014/03/13 职场文书
求职信结尾怎么写
2014/05/26 职场文书
爱护公物演讲稿
2014/09/09 职场文书
保证书格式
2015/01/16 职场文书
如何用vue实现网页截图你知道吗
2021/11/17 Vue.js
分析MySQL优化 index merge 后引起的死锁
2022/04/19 MySQL
Redis实现订单过期删除的方法步骤
2022/06/05 Redis