京东图片的放大移动案例

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

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .container {
            display: flex;
        }
        
        .left {
            position: relative;
            width: 400px;
            height: 400px;
            border: 1px solid grey;
        }
        
        .left img {
            width: 100%;
            height: 100%;
        }
        
        .left .yy {
            display: none;
            position: absolute;
            top: 0px;
            left: 0px;
            width: 200px;
            height: 200px;
            background-color: greenyellow;
            opacity: .4;
        }
        
        .right {
            position: relative;
            display: none;
            width: 600px;
            height: 600px;
            margin-left: 200px;
            /* border: 1px solid grey; */
        }
        
        .right img {
            position: absolute;
            top: 0px;
            left: 0px;
            width: 500px;
            height: 500px
        }
    </style>

    <body>
        <div class="container">
            <div class="left">
                <img src="img/b3.png" alt="">
                <div class="yy"></div>
            </div>
            <div class="right">
                <img src="img/big.jpg" alt="">
            </div>
        </div>
        <script>
            var left = document.querySelector('.left')
            var yy = left.children[1]
            var right = document.querySelector('.right')
            var bigimg = right.children[0]
            console.log(bigimg)
                // 1显示与隐藏
            left.addEventListener('mouseover', () => {
                yy.style.display = 'block'
                right.style.display = 'block'
            })
            left.addEventListener('mouseout', () => {
                    yy.style.display = 'none'
                    right.style.display = 'none'
                })
                // 2 阴影随着鼠标移动
            left.addEventListener('mousemove', (e) => {
                // console.log(e.pageX, e.pageY)
                // console.log(left.offsetLeft, left.offsetTop)
                var xyy = e.pageX - left.offsetLeft - yy.offsetWidth / 2
                var yyy = e.pageY - left.offsetTop - yy.offsetHeight / 2

                if (xyy < 0) {
                    xyy = 0
                } else if (xyy > left.offsetWidth - yy.offsetWidth) {
                    xyy = left.offsetWidth - yy.offsetWidth
                }
                if (yyy < 0) {
                    yyy = 0
                } else if (yyy > left.offsetHeight - yy.offsetHeight) {
                    yyy = left.offsetWidth - yy.offsetWidth
                }
                yy.style.left = xyy + 'px'
                yy.style.top = yyy + 'px'

                var yymax = left.offsetWidth - yy.offsetWidth
                var bigmax = right.offsetWidth - bigimg.offsetWidth
                console.log(yymax, bigmax)
                var bx = xyy * bigmax / yymax
                var by = yyy * bigmax / yymax
                console.log(bx, by)
                bigimg.style.left = -bx + 'px'
                bigimg.style.top = -by + 'px'

            })
        </script>

    </body>

</html>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值