实现jquery放大镜的两种方法

 更新时间:2018年02月22日 14:10:17   作者:web_emmet  
本篇文章给大家详细分析了用jquery实现放大镜效果的方法,以及代码分享,有兴趣的学习下。

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> 

相关文章

  • 使用隐藏的new来创建对象

    使用隐藏的new来创建对象

    JQ中发现的,jQuery.Event类。估计作者是为了减少代码量。定义一个类,但不用new关键字去创建该类对象,而使用方法调用()方式去创建该对象。
    2011-03-03
  • 基于jquery的跟随屏幕滚动代码

    基于jquery的跟随屏幕滚动代码

    我们在很多网站看到,当我们滚动网页时,网页内的广告或某个小区域并不会消失,而是浮动在屏幕的某个地方,特别是一些局域广告
    2012-07-07
  • jquery Deferred 快速解决异步回调的问题

    jquery Deferred 快速解决异步回调的问题

    下面小编就为大家带来一篇jquery Deferred 快速解决异步回调的问题。小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2016-04-04
  • jQuery插件imgPreviewQs实现上传图片预览

    jQuery插件imgPreviewQs实现上传图片预览

    这篇文章主要介绍了jQuery插件imgPreviewQs实现上传图片预览的相关资料,需要的朋友可以参考下
    2016-01-01
  • jQuery中map函数的两种方式

    jQuery中map函数的两种方式

    本文给大家分享jquery中map函数的两种方式,非常不错,具有参考借鉴价值,需要的朋友参考下吧
    2017-04-04
  • 基于jQuery实现的查看全文功能【实用】

    基于jQuery实现的查看全文功能【实用】

    本文分享了利用jQuery实现的查看全文功能:文本内容少于四行,不显示查看全文;超过五行时才显示出来并有此功能;很实用,下面就跟小编一起来看看吧
    2016-12-12
  • jQuery Tools Dateinput使用介绍

    jQuery Tools Dateinput使用介绍

    对于时间控件我比较喜欢用jQuery-ui的那个,我也比较推荐那个,所以这篇我就不对属性,函数,API进行翻译了
    2012-07-07
  • jQuery中parent()方法用法实例

    jQuery中parent()方法用法实例

    这篇文章主要介绍了jQuery中parent()方法用法,实例分析了parent()方法的功能、定义及取得紧邻父元素的使用技巧,需要的朋友可以参考下
    2015-01-01
  • jQuery加PHP实现图片上传并提交的示例代码

    jQuery加PHP实现图片上传并提交的示例代码

    这篇文章主要介绍了jQuery加PHP实现图片上传并提交的实例,文中讲解非常细致,代码帮助大家更好的理解和学习,感兴趣的朋友可以了解下
    2020-07-07
  • JQuery 解析多维的Json数据格式

    JQuery 解析多维的Json数据格式

    对博客系统已经做到了博客评论模块部分了,对单篇博文进行静态化的同时对博文的评论部分采取AJAX的方式去读取。
    2009-11-11

最新评论