• js 鼠标放到图片上放大某一部分效果


    动图效果:

     代码:

    <!DOCTYPE html>
    <html>
        <head>
            <meta charset="utf-8">
            <title></title>
            <style type="text/css">
                * {
                    margin: 0;
                    padding: 0;
                }
    
                .container,
                img,
                .bigger {
                    width: 200px;
                    height: 200px;
                }
    
                .container {
                    display: inline-block;
                    position: relative;
                    background: url(images/1.png) center;
                    background-size: contain;
                }
    
                img {
                    position: absolute;
                    top: 0;
                    left: 0;
                }
    
                .box {
                    width: 100px;
                    height: 100px;
                    background-color: #ffff7f;
                    opacity: 0.7;
                    position: absolute;
                }
    
                .bigger {
                    position: relative;
                    display: inline-block;
                    overflow: hidden;
                }
    
                .biggerImg {
                    position: absolute;
                    transform: scale(2);
                    transform-origin: top left;
                }
            </style>
        </head>
        <body>
            <div class="container">
                <div class="box" style="top: 0px;left: 0px;"></div>
            </div>
            <div class="bigger">
                <img class="biggerImg" src="./images/1.png" style="display: none;" />
            </div>
        </body>
    
        <script type="text/javascript">
            var container = document.querySelector(".container");
            var box = document.querySelector(".box");
            var bigger = document.querySelector(".biggerImg");
            var width = box.parentNode.offsetWidth;
            var height = box.parentNode.offsetHeight;
    
    
            container.addEventListener("mousedown", function() {
                bigger.style.display = "block";
                container.addEventListener("mousemove", mouseMove);
                container.addEventListener("mouseup", function() {
                    bigger.style.display = "none";
                    container.removeEventListener("mousemove", mouseMove);
                })
            });
    
            function mouseMove(e) {
                if (e.clientY - 50 < 0) {
                    box.style.top = 0;
                    bigger.style.top = 0;
                } else if (e.clientY - 50 > 100) {
                    box.style.top = 100;
                    bigger.style.top = 100;
                } else {
                    box.style.top = e.clientY - 50 + "px";
                    bigger.style.top = -2 * (e.clientY - 50) + "px";
                }
    
                if (e.clientX - 50 < 0) {
                    box.style.left = 0;
                    bigger.style.left = 0;
                } else if (e.clientX - 50 > 100) {
                    box.style.left = 100;
                    bigger.style.left = 100;
                } else {
                    box.style.left = e.clientX - 50 + "px";
                    bigger.style.left = -2 * (e.clientX - 50) + "px";
                }
            }
        </script>
    </html>
  • 相关阅读:
    android升级gradle到3.4.1
    django1.10.3下admin后台管理老是显示object
    django 1.10.3 admin后台管理设置显示中文
    Linux查看版本信息
    Centos下查看mysql的版本
    Linux centos6.7网卡配置
    Linux centos 防火墙篇
    MYSQL ERROR 1045 (28000) Access denied for user (using password YES)问题的解决
    centos7中的网卡名称相关知识
    redis编译和安装出现错误
  • 原文地址:https://www.cnblogs.com/sunshine233/p/15628599.html
Copyright © 2020-2023  润新知