惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

L
LINUX DO - 热门话题
U
Unit 42
博客园 - 司徒正美
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
J
Java Code Geeks
H
Hackread – Cybersecurity News, Data Breaches, AI and More
Hugging Face - Blog
Hugging Face - Blog
aimingoo的专栏
aimingoo的专栏
GbyAI
GbyAI
P
Privacy International News Feed
T
Threatpost
Microsoft Azure Blog
Microsoft Azure Blog
NISL@THU
NISL@THU
T
Tenable Blog
P
Privacy & Cybersecurity Law Blog
L
Lohrmann on Cybersecurity
The Last Watchdog
The Last Watchdog
AWS News Blog
AWS News Blog
C
Cisco Blogs
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Cisco Talos Blog
Cisco Talos Blog
Microsoft Security Blog
Microsoft Security Blog
雷峰网
雷峰网
C
CERT Recently Published Vulnerability Notes
Y
Y Combinator Blog
人人都是产品经理
人人都是产品经理
C
Cybersecurity and Infrastructure Security Agency CISA
D
Darknet – Hacking Tools, Hacker News & Cyber Security
G
GRAHAM CLULEY
Google DeepMind News
Google DeepMind News
H
Help Net Security
A
Arctic Wolf
Stack Overflow Blog
Stack Overflow Blog
S
Security Affairs
S
Securelist
Application and Cybersecurity Blog
Application and Cybersecurity Blog
云风的 BLOG
云风的 BLOG
Last Week in AI
Last Week in AI
L
LangChain Blog
博客园 - 【当耐特】
L
LINUX DO - 最新话题
Security Archives - TechRepublic
Security Archives - TechRepublic
Blog — PlanetScale
Blog — PlanetScale
N
News | PayPal Newsroom
B
Blog RSS Feed
Apple Machine Learning Research
Apple Machine Learning Research
S
Security @ Cisco Blogs
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
T
Troy Hunt's Blog

博客园 - 蜗牛的礼物

Visual Studio Install 2022安装问题 C#使用SHA256withRSA加密对接口进行访问 JsBarcode生成一维码 C#执行存储超时过长,但是数据库执行很快问题 远程服务器无法复制文件上去问题处理 SQLServer数据同步 + Nginx代理自动切换SFTP和Web网站 Html转Text SQLServer数据库邮件异常信息获取 SQLServer分批删除 CMD查询端口被占用并关闭 SQLServer循环生成文件死锁问题 查询数据库表 ASP.NET Core的Web页面调用微信的扫一扫、上传图片等功能 Vue UI创建项目问题 Html5QRCode扫描条形码+二维码+5秒没扫出后截图以文件扫描 JS根据元素Id截图 Html5QRCode扫描条形码+二维码 HTML播放语音 JS分隔换行成数组+去重 C#获取邮箱邮件信息+附件下载 C#XML读取
JS放大镜
蜗牛的礼物 · 2024-11-08 · via 博客园 - 蜗牛的礼物

代码:

<!DOCTYPE html>
<html>

    <head>
        <meta charset="UTF-8">
        <title>商品图片放大镜</title>
    </head>
    <style>
        body {
            margin: 0;
            padding: 0;
        }
        
        #app {
            padding: 10px;
            position: relative;
        }
        /** 默认图片*/
        
        .img-box {
            position: relative;
            left: 10px;
            top: 150px;
            width: 300px;
            height: 300px;
            text-align: center;
            border: 1px solid #83b4ff;
            border-radius: 4px;
            overflow: hidden;
            cursor: zoom-in;
        }
        
        .img1 {
            width: 100%;
            height: 100%;
        }
        
        .img-select {
            width: 100px;
            height: 100px;
            position: absolute;
            left: 0;
            top: 0;
            background: #00ff6633;
            border-radius: 4px;
            display: none;
        }
        /** 临时放大图片*/
        
        .img-temp-box {
            position: absolute;
            left: 0;
            top: 0;
            width: 400px;
            height: 400px;
            display: none;
            overflow: hidden;
        }
        
        .img2 {
            width: 200%;
            height: 200%;
            position: absolute;
            left: 0;
            top: 0;
        }
    </style>

    <body>
        <div id="app">
            <div class="img-box">
                <img src="C:\Users\3859\Desktop\img\W1.jpg" class="img1" />
                <div class="img-select"></div>
            </div>
        </div>
        <!-- 一般放置最外边-->
        <div class="img-temp-box">
            <img src="" class="img2" />
        </div>
    </body>
    <script>
        /**
         * @author yyq
         * @blogger myf
         */
        var imgBox = document.querySelector('.img-box');
        var imgSelect = document.querySelector('.img-select');
        var imgTempBox = document.querySelector('.img-temp-box');
        var img1 = document.querySelector('.img1');
        var img2 = document.querySelector('.img2');

        // 鼠标移入
        imgBox.onmouseenter = function() {
            imgSelect.style.display = 'block';
            imgTempBox.style.display = 'block';
            var img = getElementOffset(imgBox);
            imgTempBox.style.left = (img.left + 400) + "px";
            imgTempBox.style.top = (img.top - 50) + "px";
            img2.src = img1.src;
            console.log("移入")
        }

        // 鼠标移除
        imgBox.onmouseleave = function() {
            imgSelect.style.display = 'none';
            imgTempBox.style.display = 'none';
            console.log("移除")
        }

        // 鼠标放上
        imgBox.onmousemove = function() {

            var img = getElementOffset(imgBox);

            var x = event.clientX - img.left;
            var y = event.clientY - img.top;

            console.log("xy轴:", x, '-----', y);

            var imgSelectX = x - imgSelect.offsetWidth / 2
            var imgSelectY = y - imgSelect.offsetHeight / 2

            if(imgSelectX < 0) {
                imgSelectX = 0;
            } else if(imgSelectX > imgBox.offsetWidth - imgSelect.offsetWidth) {
                imgSelectX = imgBox.offsetWidth - imgSelect.offsetWidth
            }

            if(imgSelectY < 0) {
                imgSelectY = 0;
            } else if(imgSelectY > imgBox.offsetHeight - imgSelect.offsetHeight) {
                imgSelectY = imgBox.offsetHeight - imgSelect.offsetHeight
            }

            // 小图里的小框
            imgSelect.style.left = imgSelectX + 'px';
            imgSelect.style.top = imgSelectY + 'px';

            var b = (img2.offsetHeight - imgTempBox.offsetHeight) / (imgBox.offsetHeight - imgSelect.offsetHeight)

            // 临时框里的大图片
            img2.style.left = -imgSelectX * b + "px"
            img2.style.top = -imgSelectY * b + "px"

        }

        /**
         * 返回元素距离浏览器边框的位置(防止元素位置被父级限制)
         * @param {Object} element
         */
        function getElementOffset(element) {    
            var left = element.offsetLeft; // 当前元素左边距
            var top = element.offsetTop; // 当前元素上边距
            var parent = element.offsetParent; // 当前元素的父级元素
            while(parent !== null) {      
                left += parent.offsetLeft; // 累加左边距
                top += parent.offsetTop; // 累加上边距
                parent = parent.offsetParent; // 依次获取父元素
            }
            return {
                'left': left,
                'top': top
            };  
        }
    </script>

</html>

感谢:https://blog.csdn.net/weixin_43992507/article/details/132205410

posted @ 2024-11-08 10:11  蜗牛的礼物  阅读(44)  评论()    收藏  举报