JS原生制作--京东页面放大镜

 

 

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        *
         {
            margin: 0px;
            padding: 0px;
        }

        .box
         {
            position: relative;
            width: 450px;
            margin: 30px;
        }

        .fangda
         {
            position: absolute;
            left: 0;
            top: 0;
            width: 225px;
            height: 225px;
            background-color: orange;
            opacity: 0.5;
            cursor: move;
            display: none;
        }

        .box2
         {
            position: absolute;
            left: 700px;
            top: 20px;
            width: 400px;
            height: 400px;
            background: url("images/big.jpg") no-repeat;
            display: none;
        }

        img
         {
            display: block;
        }
    </style>
</head>

<body>
    <div class="box">
        <img src="images/small.jpg" alt="">
        <div class="fangda"></div>
    </div>
    <div class="box2">

    </div>
    <script>
        var box = document.querySelector(".box");
        var fangda = document.querySelector(".fangda");
        var box2 = document.querySelector(".box2");

        //鼠标移上的时候
        box.onmousemove = function (event)
         {
            fangda.style.display = "block";
            box2.style.display = "block";

            var x = event.pageX - box.offsetLeft;
            var y = event.pageY - box.offsetTop;

            //鼠标不可以移动,但是放大镜可以移动
            var left = x - 225 / 2;
            var top = y - 225 / 2;

            if (left < 0)
             {
                left = 0;
            }
            else if (left > 225)
             {
                left = 225;
            }
            if (top < 0)
             {
                top = 0;
            } else if (top > 225)
             {
                top = 225;
            }

            fangda.style.left = left + "px";
            fangda.style.top = top + "px";

            box2.style.backgroundPosition = - left * (400 / 225) + "px " + -top * (400 / 225) + "px";
        };

        //鼠标移除的时候
        box.onmouseleave = function ()
         {
            fangda.style.display = "none";
            box2.style.display = "none";
        };
    </script>
</body>

</html>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

丸子哥哥

你的鼓励将是我创作的最大动力!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值