图片放大效果

实现效果图:
在这里插入图片描述
当鼠标移入左边图片区域时,出现一个200px的正方形阴影区域,同时在右侧出现该区域的两倍放大效果图。

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <meta http-equiv="X-UA-Compatible" content="ie=edge">
        <title>Document</title>

        <style>
            #small{width: 300px; height: 560px; position: absolute; left: 100px; top: 100px}
            #small img{width: 100%; height: 100%}
            #mark{display: none; width: 200px; height: 200px; background-color: white; opacity: 0.5; filter: alpha(opacity=50); position: absolute; left: 0px; top: 0px}
            #big{display: none; width: 400px; height: 400px; border: 1px solid black; position: absolute; left: 500px; top: 100px; overflow: hidden;}
            #big img{width: 600px; height: 1120px; position: absolute; left: 0px; top: 0px}
        </style>
        
        <script>
            window.onload = function(){
                var oSmall = document.getElementById("small");
                var oBig = document.getElementById("big");
                var oMark = document.getElementById("mark");
                var oBigImg = oBig.getElementsByTagName("img")[0]

                oSmall.onmouseover = function(){
                    oMark.style.display = 'block';
                    oBig.style.display = 'block';
                }
                oSmall.onmouseout = function(){
                    oMark.style.display = 'none';
                    oBig.style.display = 'none';
                }
                oSmall.onmousemove = function(ev){
                    var e = e || window.event;
                    var l = e.clientX - oSmall.offsetLeft - 100;
                    var t = e.clientY - oSmall.offsetTop - 100;
                    if(l <= 0){
                        l = 0;
                    }
                    if(l >= 100){
                        l = 100;
                    }
                    if(t <= 0){
                        t = 0;
                    }
                    if(t >= 360){
                        t = 360;
                    }
                    oMark.style.left = l + 'px';
                    oMark.style.top = t + 'px';


                    /* 右边图片的移动方式,左边遮罩层如何移动,有变图片反方向对应倍数移动 */
                    oBigImg.style.left = l * -2 + 'px';
                    oBigImg.style.top = t * -2 + 'px';
                    
                }
            }
        </script>

    </head>
    <body>
        <div id="small">
            <img src="img/timg.jpeg" alt="">
            <div id="mark"></div>
        </div>
        <div id="big">
                <img src="img/timg.jpeg" alt="">
        </div>
    </body>
</html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值