js选项卡切换加放大镜

<!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>
        * {
            padding: 0;
            margin: o;
        }

        .main {
            width: 800px;
        }

        .main .top {
            position: relative;
            display: flex;
        }

        .main .top .left {
            position: relative;
            ;
            width: 400px;
            height: 400px;
            background-color: bisque;
        }

        .main .top .left img {
            width: 400px;
            height: 400px;
        }

        .main .top .left .img2 {
            display: none;
        }

        .main .top .left .mask {
            position: absolute;
            top: 0;
            left: 0;
            width: 200px;
            height: 200px;
            display: none;
            background-color: rgb(223, 235, 164, 0.5);
        }

        .main .right {
            position: relative;
            display: none;
            width: 400px;
            height: 400px;
            overflow: hidden;
            border: 1p solid rgb(67, 61, 61);
        }

        .main .right img {
            position: absolute;
            top: 0;
            left: 0;
            width: 800px;
            height: 800px;
        }

        .main .right .img4 {
            display: none;
        }

        .main .bottom {
            width: 400px;
            background-color: beige;
        }

        .main .bottom .xxk {
            display: flex;
            list-style: none;
            margin-top: 10px;
        }

        .main .bottom .xxk li:hover {
            border: 1px solid lightcoral;
        }

        .main .bottom .xxk li img {
            width: 100px;
            height: 100px;
        }
    </style>
</head>

<body>
    <div class="main">
        <div class="top">
            <div class="left">
                <img src="./img/选项卡1.jpg" alt="" class="img1">
                <img src="./img/选项卡3.jpg" alt="" class="img2">
                <div class="mask"></div>
            </div>
            <div class="right">
                <img src="./img/选项卡1.jpg" alt="" class="img3">
                <img src="./img/选项卡3.jpg" alt="" class="img4">
            </div>
        </div>
        <div class="bottom">
            <ul class="xxk">
                <li><img src="./img/选项卡1.jpg" alt=""></li>
                <li><img src="./img/选项卡3.jpg" alt=""></li>
            </ul>
        </div>
    </div>


    <script>
        var oLi = document.querySelectorAll(".xxk li");
        var oImg = document.querySelectorAll(".left img");
        var oImg2 = document.querySelectorAll(".right img");
        var oLeft = document.querySelector(".left");
        var oMask = document.querySelector(".mask");
        var oRight = document.querySelector(".right");
        // console.log(oLi);
        // console.log(oImg);
        // console.log(oImg2);
        //选项卡
        for (let i = 0; i < oLi.length; i++) {
            oLi[i].onmouseover = function () {
                for (var j = 0; j < oLi.length; j++) {
                    oImg[j].style.display = "none";
                    oImg2[j].style.display = "none";
                }
                oImg[i].style.display = "block";
                oImg2[i].style.display = "block";
            }
        }

        oLeft.onmouseover = function () {
            oMask.style.display = "block";
            oMask.style.cursor = "move";
            oRight.style.display = "block";

            oLeft.onmousemove = function (ev) {
                var l = ev.clientX - oMask.offsetWidth / 2 - oLeft.parentElement.offsetLeft;
                var r = ev.clientY - oMask.offsetHeight / 2 - oLeft.parentElement.offsetTop;
                // console.log(r);
                console.log(ev.clientY);
                if (l <= 0) {
                    l = 0;
                } else if (l >= oLeft.offsetWidth - oMask.offsetWidth) {
                    l = oLeft.offsetWidth - oMask.offsetWidth;
                }
                if (r <= 0) {
                    r = 0;
                } else if (r >= oLeft.offsetHeight - oMask.offsetHeight) {
                    r = oLeft.offsetHeight - oMask.offsetHeight;
                }

                oMask.style.left = l + "px";
                oMask.style.top = r + "px";

                for (var a = 0; a < oImg2.length; a++) {
                    oImg2[a].style.left = -l * 2 + "px";
                    oImg2[a].style.top = -r * 2 + "px";
                }

            }
        }
        oLeft.onmouseout = function () {
            oMask.style.display = "none";
            oRight.style.display = "none";
        }
    </script>
</body>

</html>

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值