实现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中的deferred对象和extend方法详解
May 08 jQuery
Jquery+Ajax+xml实现中国地区选择三级联动菜单效果(推荐)
Jun 09 jQuery
jQuery.Ajax()的data参数类型详解
Jul 23 jQuery
jQuery实现可兼容IE6的淡入淡出效果告警提示功能示例
Sep 20 jQuery
bootstrap可编辑下拉框jquery.editable-select
Oct 12 jQuery
jQuery中复合选择器简单用法示例
Mar 31 jQuery
jQuery简单判断值是否存在于数组中的方法示例
Apr 17 jQuery
jQuery实现基本动画效果的方法详解
Sep 06 jQuery
Vue CLI3.0中使用jQuery和Bootstrap的方法
Feb 28 jQuery
jquery获取input输入框中的值
Nov 13 jQuery
jQuery编写QQ简易聊天框
Aug 27 jQuery
jQuery实现回到顶部效果
Oct 19 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
阿拉伯的咖啡与水烟
2021/03/03 咖啡文化
php_screw安装使用教程(另一个PHP代码加密实现)
2014/05/29 PHP
Smarty模板常见的简单应用分析
2016/11/15 PHP
PHP实现字母数字混合验证码功能
2019/07/11 PHP
JavaScript中的其他对象
2008/01/16 Javascript
Firefox getBoxObjectFor getBoundingClientRect联系
2008/10/26 Javascript
JavaScript 高效运行代码分析
2010/03/18 Javascript
Jquery实现自定义窗口随意的拖拽
2014/03/12 Javascript
使用jquery修改表单的提交地址基本思路
2014/06/04 Javascript
显示今天的日期js代码(阳历和农历)
2014/09/30 Javascript
JS动态加载当前时间的方法
2015/02/09 Javascript
jQuery实现仿百度帖吧头部固定导航效果
2015/08/07 Javascript
终于实现了!精彩的jquery弹幕效果
2016/07/18 Javascript
Seajs是什么及sea.js 由来,特点以及优势
2016/10/13 Javascript
JavaScript DOM节点操作实例小结(新建,删除HTML元素)
2017/01/19 Javascript
Angular2自定义分页组件
2017/04/19 Javascript
bootstrap是什么_动力节点Java学院整理
2017/07/14 Javascript
VS Code转换大小写、修改选中文字或代码颜色的方法
2017/12/15 Javascript
浅谈VUE-CLI脚手架热更新太慢的原因和解决方法
2018/09/28 Javascript
原生js实现淘宝放大镜效果
2020/10/28 Javascript
[03:42]2014DOTA2西雅图国际邀请赛 Navi战队巡礼
2014/07/07 DOTA
python实现用于测试网站访问速率的方法
2015/05/26 Python
django限制匿名用户访问及重定向的方法实例
2018/02/07 Python
Python基于socket模块实现UDP通信功能示例
2018/04/10 Python
Django csrf 两种方法设置form的实例
2019/02/03 Python
jupyter notebook 恢复误删单元格或者历史代码的实现
2020/04/17 Python
解决matplotlib.pyplot在Jupyter notebook中不显示图像问题
2020/04/22 Python
CSS3教程(3):border-color网页边框色彩
2009/04/02 HTML / CSS
CSS3制作3D立方体loading特效
2020/11/09 HTML / CSS
安纳塔拉酒店度假村及水疗官方网站:Anantara Hotel
2016/08/25 全球购物
ruby如何进行集成操作?Ruby能进行多重继承吗?
2013/10/16 面试题
《狐假虎威》教学反思
2014/02/07 职场文书
环卫工人节活动总结
2014/08/29 职场文书
2015年乡镇工会工作总结
2015/05/19 职场文书
员工离职证明范本
2015/06/12 职场文书
SpringBoot2 参数管理实践之入参出参与校验的方式
2021/06/16 Java/Android