JavaScript实现放大镜效果

问题描述:利用JavaScript知识实现鼠标在左侧照片上移动,右侧盒子内出现放大版的对应位置效果图。

详细代码:

javascript 复制代码
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            list-style: none;
            box-sizing: border-box;
        }

        .box {
            width: 1250px;
            height: 700px;
            background-color: cadetblue;
            margin: 50px auto;
            padding-top: 30px;
        }

        .box_left {
            width: 400px;
            height: 400px;
            border: 1px solid #666;

        }

        .box_left .pic_box {
            width: 400px;
            height: 400px;
            position: relative;

        }

        .box_left .pic_box .pic {
            width: 100%;
            height: 100%;
        }

        .box_left .pic_box .pic img {
            width: 100%;
            height: 100%;
        }

        .box_left .pic_box .big_box {
            width: 500px;
            height: 500px;
            overflow: hidden;
            border: 1px solid #666;
            position: absolute;
            left: 450px;
            top: 0px;
        }

        .box_left .pic_box .big_box img {
            width: 800px;
            position: absolute;
            left: 0px;
            top: 0px;
        }

        .mask {
            width: 300px;
            height: 300px;
            background-color: bisque;
            opacity: 0.7;
            position: absolute;
            left: 0;
            top: 0px;
        }
    </style>
</head>

<body>
    <div class="box">
        <!-- 左侧放大镜 -->
        <div class="box_left">
            <div class="pic_box">
                <div class="pic">
                    <img src="./5.jpg" alt="">
                    <div class="mask"></div>

                </div>
                <div class="big_box">
                    <img src="./5.jpg" alt="">
                </div>
            </div>
            <!-- <ol>
                <li></li>
            </ol> -->

        </div>
        <!-- 右侧产品购买的信息 -->
        <div class="box_right"></div>
    </div>
    <script>
        var pic_box = document.querySelector('.pic_box');
        var mask = document.querySelector('.mask');
        var pic = document.querySelector('.pic');
        var big_box = document.querySelector('.big_box');
        var big_pic = document.querySelector('.big_box img');
        pic.onmouseenter = function () {
            mask.style.display = 'block';
            big_box.style.display = 'block';
        }
        pic.onmouseleave = function () {
            mask.style.display = 'none';
            big_box.style.display = 'none';
        }
        pic.onmousemove=function(e){
            e=e||window.event;
            // 鼠标在盒子内的坐标
            var x=e.pageX-pic_box.offsetLeft;
            var y=e.pageY-pic_box.offsetTop;
            console.log(x,y);
            var maskx=pic.offsetWidth-mask.offsetWidth;
            var masky=pic.offsetHeight-mask.offsetHeight;
            x=x-mask.offsetWidth*0.5;
            y=y-mask.offsetHeight*0.5;
            if(x<=0){
                x=0;

            }else if(x>maskx){
                x=maskx;
            }
            if(y<=0){
                y=0;

            }else if(y>=masky){
                y=masky;
            }
            mask.style.left=x+'px';
            mask.style.top=y+'px';
            var picmaxX=big_box.offsetWidth-big_pic.offsetWidth;
            big_pic.style.left=picmaxX*x/maskx+'px';
            big_pic.style.top=picmaxX*y/masky+'px';

        } 
    </script>
</body>

</html>

效果图:

相关推荐
张元清2 分钟前
React useTimeout Hook:声明式 setTimeout 与自动清理 (2026)
javascript·react.js
禁止摆烂_才浅39 分钟前
JavaScript 类型判断:instanceof 与 constructor 原理深度解析
前端·javascript·面试
gezg1 小时前
DeepSeek Harness 插件:Excel 拖进输入框,AI 自己去读文件
前端·ai编程
Asize1 小时前
HTTP 明明无状态,登录态怎么就保住了——React + Zustand + JWT 鉴权全流程拆解
前端·javascript
两只羊ovo1 小时前
MCP:AI 界的 USB-C,把模型和世界「插」起来
前端
Darling噜啦啦1 小时前
JWT 登录鉴权全链路:从 Zustand 状态管理到 Axios 拦截器,彻底搞懂前端鉴权工程
前端
可涵不会debug1 小时前
LangChain 示例选择器(Example selectors)完整基础概念解读
服务器·前端·数据库
Csvn1 小时前
😱 React `<StrictMode>`:为什么 useEffect 被调用了两次?别慌,这是特性不是 bug
前端
Asize1 小时前
2 道大厂面试题:TS 工具类型我懂了,CSS 3 列布局把我问住了
前端·css·typescript
胡萝卜术1 小时前
从"氛围编程"到规范驱动:两次创造如何让 AI 协作从碰运气变成工程流水线
前端·面试·github