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>

效果图:

相关推荐
paopaokaka_luck2 小时前
基于springboot+vue的智慧家政管理系统(AI问答、 WebSocket实时沟通、ECharts运营分析、订单规则与服务追踪)
java·javascript·spring boot·数据分析·echarts·mybatis
sm_926787052 小时前
RFID 标签打印的技术实现要点与二次开发实践
java·大数据·前端·c++·编辑器
IMPYLH2 小时前
HTML 的 <video> 元素
前端·html
溪语流沙3 小时前
【每天一个CSS | Day19】流光文字与一笔写成的手写签名
前端·javascript·css
逐光者9333 小时前
STM32——SPI 屏 · Flash · 高速
java·前端·stm32
To_OC3 小时前
同样是让 AI 写全栈,为什么别人一句话就够了
前端·前端框架·next.js
CDwenhuohuo3 小时前
electron pc项目打包成桌面端
前端·javascript·electron
前端大斗师3 小时前
「大于 1000」把 1000 元那单也算进去了:我在 Vue3 订单页对了 10 句话
前端·人工智能·typescript·大模型·原力计划
copilot中国版3 小时前
PPT Copilot 一键统一全篇版式,告别字体混乱、元素错位和重复排版
html·powerpoint·copilot
Jmyd01234 小时前
用 Web VR 引擎搭建 3D 虚拟展厅:从素材上云到多端实时渲染的完整数据链路
前端·3d·vr·三维数字化