JS实现放大镜效果

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>放大镜</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }

        .small {
            width: 130px;
            height: 130px;
            position: relative;
        }

        .small .cover {
            width: 50px;
            height: 50px;
            background: orange;
            opacity: .3;
            position: absolute;
            top: 0;
            left: 0;
            display: none;

        }

        .big {
            width: 308px;
            height: 308px;
            overflow: hidden;
            position: absolute;
            top: 0px;
            left: 140px;
            display: none;
        }

        .big img {
            position: absolute;
            top: 0;
            left: 0;
        }
    </style>
</head>

<body>
    <div class="small">
        <img src="./images/small.jpg" alt="" id="img">
        <div class="cover"></div>
    </div>

    <div class="big">
        <img src="./images/big.jpg" alt="" id="pic">
    </div>

    <script>
        var small = document.querySelector('.small');//获取小图片盒子
        var cover = document.querySelector('.cover');//获取遮罩层
        var img = document.querySelector('#img');//获取小图片
        var big = document.querySelector('.big');//获取存放大图片的盒子
        var pic = document.querySelector('#pic')//获取大图片

        //鼠标移入小图片盒子遮罩层与大图片显示
        small.onmouseover = function () {
            cover.style.display = 'block';
            big.style.display = 'block';
        }

        //鼠标移出小图片盒子遮罩层与大图片隐藏
        small.onmouseout = function () {
            cover.style.display = 'none';
            big.style.display = 'none';
        };

        small.onmousemove = function (e) {
            var e = e || window.event;

            //让鼠标停在遮罩层正中间
            var L = e.clientX - cover.offsetWidth / 2;
            var T = e.clientY - cover.offsetHeight / 2;

            //将遮罩层限制在小图片盒子中
            if (L < 0) {
                L = 0;
            } else if (L > small.offsetWidth - cover.offsetWidth) {
                L = small.offsetWidth - cover.offsetWidth;
            }

            if (T < 0) {
                T = 0;
            } else if (T > small.offsetHeight - cover.offsetHeight) {
                T = small.offsetHeight - cover.offsetHeight
            }
            // 遮罩层的位置
            cover.style.left = L + 'px';
            cover.style.top = T + 'px';

            //大图与小图的比例
            var a = pic.offsetWidth/img.offsetWidth;
            var b = pic.offsetHeight/img.offsetHeight;

            // 大图移动的距离
            pic.style.left = -(L * a) + 'px';
            pic.style.top = -(T * b) + 'px';
        }
    </script>
</body>

</html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值