JavaScript基础学习 放大镜

JavaScript基础学习 放大镜

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

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=devcie-width,initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        .mask {
            display: none;
            position: absolute;
            top: 0;
            left: 0;
            width: 400px;
            height: 400px;
            background-color: pink;
            opacity: .5;
            cursor: move;
        }
        
        .father {
            position: relative;
            height: 500px;
            width: 500px;
            margin-left: 100px;
            margin-top: 50px;
        }
        
        .big {
            display: none;
            position: absolute;
            width: 400px;
            height: 400px;
            left: 700px;
            top: 0;
            background-color: yellow;
        }
        
        .big img {
            position: absolute;
            top: 0;
            left: 0;
        }
    </style>
</head>

<body>
    <div>
        <div class="father">
            <img src="image/b3.png">
            <div class="mask"></div>
            <div class="big">
                <img src="image/big.jpg" class="bigImg">
            </div>
        </div>

    </div>

    <script>
        var father = document.querySelector('.father');
        var mask = document.querySelector('.mask');
        var big = document.querySelector('.big');
        var bigImg = document.querySelector('.bigImg');
        //  鼠标经过大盒子时,显示遮挡层和大图片
        father.addEventListener('mouseover', function() {
            mask.style.display = 'block';
            big.style.display = 'block';
        })

        //  鼠标移开大盒子时,把遮挡层和大图片进行遮挡
        father.addEventListener('mouseout', function() {
            mask.style.display = 'none';
            big.style.display = 'none';
        })

        // 鼠标移动让我们的遮挡层跟着鼠标一起移动
        father.addEventListener('mousemove', function(e) {
            var x = e.pageX - father.offsetLeft;
            var y = e.pageY - father.offsetTop;
            var maskX = x - 200;
            var maskY = y - 200;

            // 让遮挡层不要超出我们的大盒子,如果超出就让遮挡层处于边界
            if (maskX < 0) {
                maskX = 0;
            } else if (maskX >= 300) {
                maskX = 300;
            }
            if (maskY < 0) {
                maskY = 0;
            } else if (maskY >= 300) {
                maskY = 300;
            }
            mask.style.left = maskX + 'px';
            mask.style.top = maskY + 'px';

            var bigMax = bigImg.offsetWidth - big.offsetWidth;
            var bigX = maskX * bigMax / 1000;
            var bigY = maskY * bigMax / 1000;
            bigImg.style.left = -bigX + 'px';
            bigImg.style.top = -bigY + 'px';
        })
    </script>

</body>

</html>

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值