做个放大镜看看?

近视好帮手(●’◡’●)

在这里插入图片描述

先编写简单的html页面

<body>
    <div id="small">
        <img src="2.jpg" alt="Jackson">
        <div id="mark"></div>
    </div>
    <div id="big">
        <img src="2.jpg" alt="Jackson">
    </div>
</body>

引入js

 <style>
        body{background-color: grey;}
        #small{width: 225px;height: 288px;border:black 1px solid;position: absolute;left:100px;top: 50px;}
        #small img{width: 100%;height: 100%;}
        #big img{width: 900px;height: 1150px;position: absolute;}
        #big{display:none;overflow:hidden;width: 400px;height: 400px;position: absolute;left:400px;top:50px;border: 1px solid black;}
        #mark{display:none;width:100px;height:100px;background-color: white;opacity: 0.5;filter:alpha(opacity=50);
              position: absolute;left:0px;top: 0px;}
 </style>
    

实现放大镜功能(限制出界)

<script src="jquery-3.5.1.js"></script>
    <script>
        $(function(){
            $("#small").mouseenter(function(){
                $("#mark,#big").show()
              }).mouseleave(function(){
                $("#mark,#big").hide()
              }).mousemove(function(ev){
                  var l = ev.pageX-$(this).offset().left-50;
                  var t = ev.pageY-$(this).offset().top-50;
                  if(l<=0){//限制出界
                      l=0;
                      }
                  if(l>=125){
                      l=125;
                      }
                  if(t<=0){
                      t=0;
                      }
                  if(t>=188){
                      t=188;
                      }
                  $("#mark").css({
                      left:l,
                      top:t
                  })
                  $("#big img").css({//注意是反向增大
                      left: -4 * l,
                      top: -4 * t
                  })
              })
        })
    </script>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值