商品放大镜效果

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>放大镜效果</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }
        img {
            vertical-align: top;
        }
        .box {
            width: 350px;
            height: 350px;
            position: relative;
            border:1px solid #ccc;
            margin: 100px;
        }
        .productImg-smallBox {
            position: relative;
        }
        .productImg-bigBox {
            width: 450px;
            height: 450px;
            border:1px solid #ccc;
            overflow: hidden;
            position: absolute;
            top: 0;
            left: 360px;
            display: none;
        }
       .big{
           position: absolute;
           top: 0;
           left: 0;
        }
        .mask {
            width: 80px;
            height: 80px;
            position: absolute;
            top:0;
            left:0;
            background-color: rgba(255,0,255,.4);
            cursor: move;
            transition: opacity 1s;
            opacity: 0;
        }
    </style>
</head>
<body>
<div class="box">
    <div class="productImg-smallBox">
        <img src="images/001.jpg" class="small">
        <div class="mask"></div>
    </div>
    <div class="productImg-bigBox">
        <img src="images/0001.jpg" class="big">
    </div>
</div>

<script>
    window.onload = function(){
        var smallBox = document.querySelector(".productImg-smallBox");
        var bigBox = document.querySelector(".productImg-bigBox");
        var bigImg = document.querySelector(".big");
        var mask = document.querySelector(".mask");
        smallBox.onmouseenter = function(){
            mask.style.opacity = 1;
            bigBox.style.display = "block";
        }
        smallBox.onmouseleave = function(){
            mask.style.opacity = 0;
            bigBox.style.display = "none";
        }
        var x = 0, y = 0;
        smallBox.onmousemove = function(event){
            var e = event || window.event;
            x = e.clientX - this.offsetParent.offsetLeft - mask.offsetWidth / 2;
            y = e.clientY - this.offsetParent.offsetTop - mask.offsetHeight / 2;
            if(x <= 0){
                x = 0
            }
            else if(x > smallBox.offsetWidth - mask.offsetWidth){
              x = smallBox.offsetWidth - mask.offsetWidth;
            }
            if(y <= 0){
                y = 0
            }
            else if(y > smallBox.offsetHeight - mask.offsetHeight){
                y = smallBox.offsetHeight - mask.offsetHeight;
            }
            mask.style.left = x + "px";
            mask.style.top = y + "px";
            bigImg.style.left = -x * smallBox.offsetWidth / bigBox.offsetWidth + "px";
            bigImg.style.top = -y * smallBox.offsetHeight / bigBox.offsetHeight + "px";
        }
    }
</script>
</body>
</html>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值