图片查看的放大镜效果(js)

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>放大镜</title>
    <style type="text/css">
        .smallBox,.bigBox{
            position: absolute;
            left: 100px;
            top: 50px;
            width: 300px;
            height: 300px;
        }
        .smallBox img{
            width: 300px;
            height: 300px;
        }
        .bigBox{
            left: 420px;
            overflow: hidden;
        }
        .bigBox img{
            position: absolute;
            left: 0;
            top: 0;
            display: none;
        }
        .contorl{
            position: absolute;
            left: 0;
            top: 0;
            background: #000;
            opacity: 0.3;
            filter: alpha(opacity=30);
            display: none;
        }
    </style>
    <script>
        window.onload = function() {
            var smallBox = document.getElementById("smallBox");
            var bigImg = document.getElementById("bigImg");
            var contorl = document.getElementById("contorl");
            var scale = 3;
            //当鼠标进入小图时
            smallBox.onmouseenter = function(){
                contorl.style.display = "block";
                bigImg.style.display = "block";
                contorl.style.width = smallBox.offsetWidth / scale + "px";
                contorl.style.height = smallBox.offsetHeight / scale + "px";
                bigImg.style.width = smallBox.offsetWidth * scale + "px";
                bigImg.style.height = smallBox.offsetHeight * scale + "px";
            };
            //鼠标移动
            smallBox.onmousemove = function(ev){
                ev = ev || event;


                var left = ev.clientX - smallBox.offsetLeft - contorl.offsetWidth / 2;
                var top = ev.clientY - smallBox.offsetTop - contorl.offsetHeight / 2;
                if(left < 0){
                    left=0;
                }
                if(left >= smallBox.offsetWidth - contorl.offsetWidth){
                    left = smallBox.offsetWidth - contorl.offsetWidth;
                }
                if(top < 0){
                    top=0;
                }
                if(top >= smallBox.offsetHeight - contorl.offsetHeight){
                    top = smallBox.offsetHeight - contorl.offsetHeight;
                }
                contorl.style.left = left + "px";
                contorl.style.top = top + "px";
                bigImg.style.left = -scale * left + "px";
                bigImg.style.top = -scale * top + "px";
            };
            //鼠标离开
            smallBox.onmouseleave = function(){
                contorl.style.display = "none";
                bigImg.style.display = "none";
            };
        }
    </script>
</head>
<body>
<div id="smallBox" class="smallBox">
    <img src="images/5.jpg" />
    <div id="contorl" class="contorl"></div>
</div>
<div class="bigBox" >
    <img id="bigImg" src="images/5.jpg" />
</div>
</body>
</html>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值