jQuery 图片放大镜

源码HTML代码:

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8" />
		<meta name="viewport" content="width=device-width, initial-scale=1.0">
    	<title>jQuery放大镜</title>	
    	<style>
    		#small #mark{
			    width: 100px;
			    height: 100px;
			    background-color: white;
			    opacity: 0.5;
			    position: absolute;
			    /* float: left; */
			    left: 0;
			    top: 0;
			    /* z-index: 99; */
			}
			#big{
			    width: 400px;
			    height: 365px;
			    border: 1px solid #000;
			    position: absolute;
			    left: 400px;
			    top: 150px;
			    overflow: hidden;
			}
			#big img{
			    width: 750px;
			    height: 1095px;
			    position: absolute;
			}
			#small{
	            width: 250px;
	            height: 365px;
	            margin: 70px auto;
	        }
	        #small img{
	            width: 100%;
	            height: 100%;
	        }
    	</style>
	</head>
	<body>
		<h3 style="margin: 50px auto; text-align: center;">jQuery放大镜</h3>
	    <div id="small" style="width: 250px; height: 365px;">
	        <img src="img/1.jpg">     
	    </div>
	</body>
	<script type="text/javascript" src="js/jquery.js" ></script>
	<script type="text/javascript" src="js/jQuery-imgzoom.js" ></script>
	<script>
        $("#small").imgzoom({times:"1"})
    </script>
</html>

jQuery-imgzoom.js代码:

// 立即执行函数闭包
(function($){
    // 倍数,times  想放大的倍数
    // id,对应的id 
    $.fn.imgzoom=function(o){
        var e =this;
        // 找到图片
        img=$(this).find("img").eq(0),
        s={
            width:"100px",
            height:"100px",
            backgroundColor: "white",
            opacity: "0.5",
            left:"0",
            top:"0"
        };
        // console.log(img)
        o && $.extend(!0,s,o)
        // console.log(s.times)
        s.times== null ? s.times=3:null
        s.id==null ? s.id="#small":null
        if(s.times<=1){
            s.times=1.5
        }else{
            s.times=s.times
        }
        // 设置进入
        $(this).hover(function(){
            mark=$("<div id='mark'></div>"),
            big=$("<div id='big'>"),
            src=img.attr("src"),
            // console.log(src[1])
            bigImg=$("<img src="+src+" />")
            // console.log(bigImg)
            $(e).append(mark),
            $(big).append(bigImg),
            $(e).after(big)

            w=$(s.id).width()
            h=$(s.id).height()

            $("#big").css({
                width:100*s.times+"px",
                height:100*s.times-5+"px"
            })
            // console.log(w)
            $("#big img").css({
                width:w*s.times+"px",
                height:h*s.times+"px"
            })
            // console.log(365/s.times*(s.times-1))
            $(e).off("mousemove"),
            $(e).on("mousemove",function(o){
                var i=o || window.event;
                // console.log(i)
                mark=$(e).find("#mark");
                l=o.pageX-$(this).offset().left-50;
                t=o.pageY-$(this).offset().top-50;
                // console.log(l)
                l <= 0 && (l=0);
                l >= 150 && (l=150);
                t <= 0 && (t=0);
                t >= 265 && (t=265); 
                mark.css({
                    left:l,
                    top:t
                })
                $("#big img").css({
                    left:-(s.times)*l,
                    top:-(s.times)*t
                })
                // console.log(s.times)

            })
        },
        function(){
            // 移除 
            $(e).find("#mark").remove(),
            $("#big").remove()
        }) 
    }
})(jQuery);

图片一张img文件夹下1.jpg,jQuery.js版本1.0以上

效果图:

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
以下是两种jQuery图片放大镜的代码: 1. 使用HTML和CSS实现图片放大镜效果: ```html <div id="box"> <img src="./哈哈.jpg" alt=""> // 正常显示的图片 <div id="xiao"></div> </div> <div id="box2"> <img src="./哈哈.jpg" alt="" id="big"> // 要放大的图片 </div> <style> #box { position: relative; width: 400px; height: 400px; } #xiao { position: absolute; width: 100px; height: 100px; background-color: rgba(0, 0, 0, 0.5); display: none; } #box2 { position: relative; width: 400px; height: 400px; overflow: hidden; } #big { position: absolute; width: 800px; height: 800px; display: none; } </style> <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script> <script> $(document).ready(function() { $("#box").mousemove(function(e) { var x = e.pageX - $(this).offset().left; var y = e.pageY - $(this).offset().top; var xPercent = x / $(this).width(); var yPercent = y / $(this).height(); var bigImgWidth = $("#big").width(); var bigImgHeight = $("#big").height(); var moveX = -(bigImgWidth - $(this).width()) * xPercent; var moveY = -(bigImgHeight - $(this).height()) * yPercent; $("#big").css({ "left": moveX, "top": moveY }); $("#xiao").css({ "left": x - 50, "top": y - 50 }); }); $("#box").hover(function() { $("#xiao").show(); $("#big").show(); }, function() { $("#xiao").hide(); $("#big").hide(); }); }); </script> ``` 2. 使用jQuery插件实现图片放大镜效果: ```html <div id="smallBox"> <img src="/img/1.png" alt=""> <div id="zoom"></div> </div> <div id="bigBox"> <img src="/img/1.png" alt=""> </div> <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script> <script src="jquery.elevatezoom.js"></script> <script> $(document).ready(function() { $("#smallBox").elevateZoom({ zoomType: "inner", cursor: "crosshair", zoomWindowFadeIn: 500, zoomWindowFadeOut: 500 }); }); </script> ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

豆皮没有豆

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值