zoukankan      html  css  js  c++  java
  • jQuery 放大镜功能 简易放大镜

    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="utf-8">
    <title>放大的图片</title>
    <script src="./js/jquery.min.js"></script>
    <style>
    *{margin:0;padding: 0;}
    .box_images{600px;margin: 0 100px; text-align: center;position:relative;}
    .box_images .box{auto;height:350px;overflow: hidden;max- 100%;max-height: 100%;display:flex;align-items: center;justify-content: center}
    .box_images .box img{auto;height:auto;max-height: 100%;max- 100%;}
    .fat{position: absolute; top: 0; left: 110%;400px;display: none;overflow: hidden;box-sizing: border-box; height: 350px;}
    .fatBox{position: relative;display: block;;100%;height:100%;}
    .fatBox img{position: absolute;top:0;left:0;object-fit: cover;}
    </style>
    </head>
    <body>
    <div class="box_images">
    <div class="box">
    <img src="images/mei.jpg" alt="">
    <!--<img src="images/beauty.jpg" alt="">-->
    </div>
    <div class="fat">
    <div class="fatBox">
    <img src="" alt="">
    </div>
    </div>
    </div>

    <script>

    function magnify(){
      let imagesBox = $(".box");
      let boxImg = $(".box img");
      let boxImgH = boxImg.height();
      let boxImgw = boxImg.width();
      let fat = $(".fat");
      let fatImg = $(".fat img");
      //定义一个变量来确定移动比例;
      let nums = 3;  

      //当前鼠标移入,移除

      boxImg.hover(function(){
        let $_this = $(this);
        fat.show()//显示放大区域
        let srcIs = $_this.attr("src");
        fatImg.attr("src", srcIs)
      },function(){
        fat.hide();//隐藏放大的区域
      });
      //当鼠标滑动的时候
      boxImg.on("mousemove",function(e){
      event = e || window.event;//兼容IE
      let offset_top = $(this).offset().top;
      let offset_left = $(this).offset().left;
      //动态设置 fatImg 的宽度
      fatImg.css({boxImgw*nums,height:boxImgH*nums,'object-fit': 'cover'});
      let x = e.pageX , y = e.pageY;
      let xx = x - offset_left;  
      let yy = y - offset_top;
      let xy = (x - offset_left) * nums;
      let yx = (y - offset_top) * nums;
      if(xy > fatImg.width() - fat.width() ){
        xy = fatImg.width() - fat.width()
      }
      if(yx > fatImg.height() - fat.height() ){
        yx = fatImg.height() - fat.height()
      }
      fatImg.css({
        top:-yx,
        left:-xy
      })
      })
    }
    magnify();
    </script>


    </body>
    </html>

    说明: 此代码属于个人笔记。不做他用勿喷。

  • 相关阅读:
    Linux常用命令
    全文搜索服务器solr
    非关系型数据库之redis
    springMVC流程
    浅谈spring框架的控制反转和依赖注入
    Java基础之数组
    Java基础之方法
    跨域访问接口,传递参数
    Centos 6无法使用yum
    内网穿透工具:钉钉HTTP内网穿透使用与讲解
  • 原文地址:https://www.cnblogs.com/yangyang520/p/12175936.html
Copyright © 2011-2022 走看看