首页 > web前端 > js教程 > 正文

如何用jquery做出放大镜效果

php中世界最好的语言
发布: 2018-03-14 16:50:35
原创
1737人浏览过

这次给大家带来如何用jquery做出放大镜效果,用jquery做出放大镜效果的注意事项有哪些,下面就是实战案例,一起来看一下。

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

<body> 
  <p id="father"> 
    <p id="container"> 
      @@##@@ 
      @@##@@ 
      <p class="shade"></p> 
    </p> 
    <p class="small first">@@##@@</p> 
    <p class="small second">@@##@@</p> 
  </p>    
  <p class="big"> 
    @@##@@ 
    @@##@@ 
  </p> 
</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> 
  <p id="father"> 
    <p id="container"> 
      @@##@@ 
      @@##@@ 
      @@##@@ 
      <p class="shade"> 
        @@##@@ 
        @@##@@ 
        @@##@@ 
      </p> 
    </p> 
    <p class="small first">@@##@@</p> 
    <p class="small second">@@##@@</p> 
    <p class="small third">@@##@@</p> 
  </p> 
</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>
登录后复制

相信看了本文案例你已经掌握了方法,更多精彩请关注php中文网其它相关文章!

推荐阅读:

jquery添加带有样式的HTML标签

jquery怎样给动态生成的标签绑定事件

jQuery怎样获取标签子元素的值

jQuery怎样获取并且修改P标签内的值

如何用jquery做出放大镜效果如何用jquery做出放大镜效果如何用jquery做出放大镜效果如何用jquery做出放大镜效果如何用jquery做出放大镜效果如何用jquery做出放大镜效果如何用jquery做出放大镜效果如何用jquery做出放大镜效果如何用jquery做出放大镜效果如何用jquery做出放大镜效果如何用jquery做出放大镜效果如何用jquery做出放大镜效果如何用jquery做出放大镜效果如何用jquery做出放大镜效果如何用jquery做出放大镜效果

以上就是如何用jquery做出放大镜效果的详细内容,更多请关注php中文网其它相关文章!

最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号