局部放大镜效果js

局部放大镜效果

实际上是两张图片 一张小图一张大图
大的图隐藏,当鼠标放在小图上,大的图才会显示,并跟随鼠标局部放大

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

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        .img {
            position: relative;
            top: 100px;
            left: 100px;
            width: 400px;
            height: 400px;
            border: 1px solid #333;
        }
        
        .mask {
            display: none;
            position: absolute;
            top: 0px;
            left: 0px;
            width: 300px;
            height: 300px;
            background-color: #FEDE4F;
            opacity: .5;
            /* 透明度 */
            cursor: move;
        }
        
        .img-big {
            display: none;
            position: absolute;
            left: 410px;
            top: 0;
            width: 500px;
            height: 500px;
            overflow: hidden;
            border: 1px solid #333;
            z-index: 999;
        }
        
        .img-big .big {
            position: absolute;
            top: 0px;
            left: 0px;
        }
    </style>
</head>


<body>
    <div class="img">
        <img src="../imgs/s3.png">
        <div class="mask"></div>
        <div class="img-big">
            <img src="../imgs/b3.jpg" class="big">
        </div>
    </div>


    <script>
        var img = document.querySelector('.img');
        var mask = document.querySelector('.mask');
        var bigbox = document.querySelector('.img-big');
        var big = document.querySelector('.big');

        img.addEventListener('mouseover', function() {

            mask.style.display = 'block';
            bigbox.style.display = 'block';

        })
        img.addEventListener('mouseout', function() {
            mask.style.display = 'none';
            bigbox.style.display = 'none';
        })
        img.addEventListener('mousemove', function(e) {
            var x = e.pageX - this.offsetLeft;
            var y = e.pageY - this.offsetTop;
            var maskX = x - mask.offsetHeight / 2;
            var maskY = y - mask.offsetHeight / 2;
            var maskMax = img.offsetWidth - mask.offsetWidth
            if (maskX <= 0) {
                maskX = 0;
            } else if (maskX >= maskMax) {
                maskX = maskMax
            }
            if (maskY <= 0) {
                maskY = 0;
            } else if (maskY >= maskMax) {
                maskY = maskMax
            }
            mask.style.top = maskY + 'px';
            mask.style.left = maskX + 'px';

            big.style.top = -maskY * big.offsetWidth / img.offsetWidth + 'px';
            big.style.left = -maskX * big.offsetWidth / img.offsetWidth + 'px';
        })
    </script>
</body>

</html>

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值