ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

放大镜效果

2022-01-12 01:36:05  阅读:188  来源: 互联网

标签:效果 放大镜 big top mask height width small


<!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>
        .container {
            position: relative;
        }

        .small {
            position: relative;
            width: 200px;
            height: 200px;
            border: 1px solid #ccc;
            /* 测试 */
            margin-top: 201px;
            margin-left: 263px;
        }

        .small>img {
            width: 100%;
            height: 100%;
        }

        .big {
            display: none;
            position: absolute;
            left: 522px;
            top: 0;
            width: 400px;
            height: 400px;
            border: 1px solid #ccc;
            overflow: hidden;
        }

        .big>img {
            position: absolute;
            width: 800px;
            height: 800px;
        }

        .mask {
            display: none;
            position: absolute;
            left: 0;
            top: 0;
            width: 100px;
            height: 100px;
            background-color: orange;
            opacity: 0.5;
        }
    </style>
</head>

<body>
    <div class="container">
        <div class="small">
            <img src="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fimg.defanli.com%2Fi3%2F1637289231%2FO1CN01Ldp9fc2I3qkCMF8H5_%21%211637289231.jpg_q90.jpg&refer=http%3A%2F%2Fimg.defanli.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1644506916&t=6e9b0c11f89b561cccbfbeb74b76ae89"
                alt="">
            <div class="mask"></div>
        </div>
        <div class="big">
            <img src="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fimg.defanli.com%2Fi3%2F1637289231%2FO1CN01Ldp9fc2I3qkCMF8H5_%21%211637289231.jpg_q90.jpg&refer=http%3A%2F%2Fimg.defanli.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1644506916&t=6e9b0c11f89b561cccbfbeb74b76ae89"
                alt="">
        </div>
    </div>
    <script src="./jquery-1.12.4.js"></script>
    <script>
        $(function () {
            const small = $('.small')
            const mask = $('.mask')
            const big = $('.big')

            small.hover(function () {
                mask.css("display", "block");
                big.css("display", "block");
            }, function () {
                mask.css("display", "none");
                big.css("display", "none");
            })

            small.on("mousemove", function (ev) {
                let x = ev.pageX - (small.offset().left - small.scrollLeft()) - mask.width() / 2
                let y = ev.pageY - (small.offset().top - small.scrollTop()) - mask.height() / 2
                if (x < 0) {
                    x = 0;
                }
                if (y < 0) {
                    y = 0;
                }
                if (x > small.width() - mask.width()) {
                    x = small.width() - mask.width();
                }
                if (y > small.height() - mask.height()) {
                    y = small.height() - mask.height();
                }
                mask.css({ left: x, top: y });
                // 放大倍数
                rateX = 4;
                $('.big>img').css({ left: -rateX * x, top: -rateX * y });
            })
        })
    </script>
</body>

</html>

 

标签:效果,放大镜,big,top,mask,height,width,small
来源: https://www.cnblogs.com/lv77/p/15790847.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有