JS放大镜

作者: sky丶星如雨 | 来源:发表于2017-07-01 16:06 被阅读25次
    <!doctype html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport"
              content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
        <meta http-equiv="X-UA-Compatible" content="ie=edge">
        <title>Document</title>
        <style>
            *{
                margin:0;
                padding:0;
            }
            #box{
                width: 350px;
                height: 350px;
                border:1px solid #ccc;
                position: relative;
                margin:100px;
            }
            #small{
                position: relative;
            }
            #mask{
                width: 100px;
                height: 100px;
                position: absolute;
                top:0;
                left:0;
                background: rgba(255,255,0,.4);
                cursor: move;
                display: none;
            }
            #big{
                width: 450px;
                height: 450px;
                position: absolute;
                top:0;
                left: 360px;
                overflow: hidden;
                display: none;
            }
            #big img{
                position: absolute;
                top:0;
                left:0;
            }
        </style>
    </head>
    <body>
    <div id="box">
        <div id="small">
            ![](images/001.jpg)
            <div id="mask"></div>
        </div>
        <div id="big">
            ![](images/0001.jpg)
        </div>
    </div>
    <script>
        // 获取元素
        var box = document.getElementById("box");
        var small = document.getElementById("small");
        var mask = document.getElementById("mask");
        var big = document.getElementById("big");
        var bigImg = big.querySelector("img");
    
        small.onmouseover = function () {
            mask.style.display  = "block";
            big.style.display = "block";
        };
        small.onmouseout = function () {
            mask.style.display  = "none";
            big.style.display = "none";
        };
    
        small.onmousemove = function (event) {
           var e = event ||window.event;
           // 获取鼠标位置(可视区域)
           var mX = e.clientX;
           var mY = e.clientY;
           // 计算放大镜的移动范围
            var maxLeft = box.offsetWidth - mask.offsetWidth;
            var maxTop = box.offsetHeight - mask.offsetHeight;
            // 设置临时位置
            var tempLeft = mX - box.offsetLeft - mask.offsetWidth / 2;
            var tempTop = mY - box.offsetTop - mask.offsetHeight / 2;
           //  设置最终位置
            var nowLeft = 0;
            var nowTop = 0;
            if(tempLeft >= maxLeft){
                nowLeft = maxLeft;
            }else if(tempLeft>0 && tempLeft < maxLeft){
                nowLeft = tempLeft;
            }
            if(tempTop >= maxTop){
                nowTop = maxTop;
            }else if(tempTop > 0 && tempTop < maxTop){
                nowTop = tempTop;
            }
    
            // 遮罩跟随鼠标
            mask.style.left = nowLeft + "px";
            mask.style.top = nowTop + "px";
    
            // 计算移动比例
            var ratioX = box.offsetWidth / bigImg.offsetWidth;
            var ratioY = box.offsetHeight / bigImg.offsetHeight;
            // 大图位置
            bigImg.style.left = - nowLeft / ratioX + "px";
            bigImg.style.top = - nowTop / ratioY + "px";
    
        }
    
    </script>
    </body>
    </html>
    

    这种布局方式相对来说更好操作,小图和大图在同一个盒子里;
    难点在于获取鼠标在盒子中的位置,不明白的朋友可以自己画图,可以更直观的理解。
    再一点就是遮罩移动,大图移动,强调下,移动的不是大图的父级盒子,而是图片本身,根据图片比例移动,比如,遮罩在小图上移动1px,那么在大盒子上移动的就是 1 / 小图 / 大图 。代码中有所体现,如有哪里不清楚,请在下方留言,我会第一时间回复!

    相关文章

      网友评论

        本文标题:JS放大镜

        本文链接:https://www.haomeiwen.com/subject/fxfgcxtx.html