jQuery实例 - 商品放大镜

<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <meta name="viewport" content="width=device-width">
    <meta http-equiv="X-UA-Compatible" content="IE=8">
    <title>购物商城</title>

    <style>
            *{
                margin: 0;
                padding: 0;
            }
            .outer{
                position:relative;
                width:350px;
                height:350px;
                border:1px solid black;
            }
            .outer .small-box{
                position: relative;
                z-index: 1;
            }
            .outer .small-box .mark{
                position: absolute;
                display: block;
                width: 350px;
                height: 350px;
                background-color: #fff;
                filter: alpha(opacity=0);
                opacity: 0;
                z-index: 10;
            }
            .outer .small-box .float-box{
                display: none;
                width: 175px;
                height: 175px;
                position: absolute;
                background: #DAEEFC;
                filter: alpha(opacity=40);
                opacity: 0.4;
            }
            .outer .big-box{
                position: absolute;
                top: 0;
                left: 351px;
                width: 350px;
                height: 350px;
                overflow: hidden;
                border: 1px solid transparent;
                z-index: 1;
            }
            .outer .big-box img{
                position: absolute;
                z-index: 5
            }
    </style>
</head>
<body>

    <div  class="outer">
        <div  class="small-box">
            <div  class="mark"></div>
            <div  class="float-box" ></div>
            <img width="350" height="350" src="../../css/1.jpg">
        </div>
        <div>
            <img width="900px" height="900px" src="../../css/1.jpg" >
        </div>
    </div>


<script src="../../jquery-1.12.4.js"></script>

<script>
   $(function(){
        $(".mark").mouseover(function () {
            $(".float-box").css("display","block");
            $(".big-box").css("display","block");
        });

        $(".mark").mouseout(function () {
            $(".float-box").css("display","none");
            $(".big-box").css("display","none");
        });



        $(".mark").mousemove(function (e) {

            var _event = e || window.event;  //兼容多个浏览器的event参数模式

            var float_box_width  = $(".float-box")[0].offsetWidth;
            var float_box_height = $(".float-box")[0].offsetHeight;//175,175


            var float_box_width_half  =  float_box_width / 2;
            var float_box_height_half =  float_box_height/ 2;//87.5,87.5


            var small_box_width  =  $(".outer")[0].offsetWidth;
            var small_box_height =  $(".outer")[0].offsetHeight;//360,360


            var mouse_left = _event.clientX   - float_box_width_half;
            var mouse_top = _event.clientY  - float_box_height_half;


            if (mouse_left < 0) {
                mouse_left = 0;
            } else if (mouse_left > small_box_width - float_box_width) {
                mouse_left = small_box_width - float_box_width;
            }
            if (mouse_top < 0) {
                mouse_top = 0;
            } else if (mouse_top > small_box_height - float_box_height) {
                mouse_top = small_box_height - float_box_height;
            }

            $(".float-box").css("left",mouse_left + "px");
            $(".float-box").css("top",mouse_top + "px");
            
            
            var percentX = ($(".big-box img")[0].offsetWidth - $(".big-box")[0].offsetWidth) / (small_box_width - float_box_width);
            var percentY = ($(".big-box img")[0].offsetHeight - $(".big-box")[0].offsetHeight) / (small_box_height - float_box_height);
            console.log($(".big-box img")[0].offsetWidth,$(".big-box")[0].offsetWidth,small_box_width,float_box_width)
            console.log(percentX,percentY)
            $(".big-box img").css("left",-percentX * mouse_left + "px");
            $(".big-box img").css("top",-percentY * mouse_top + "px")

        })
   })

</script>
</body>
</html>