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

        #miniBox {
            height: 200px;
            width: 400px;
            border: 1px solid #000;
            position: relative;
            margin-left: 50px;
        }

        #miniBox img {
            width: 400px;
            height: 100%;

        }

        .mask {
            width: 100px;
            height: 100px;
            border: 1px solid #000;
            background-color: yellow;
            opacity: 0.4;
            position: absolute;
            left: 0;
            top: 0;
            display: none;

        }

        #bigBox {
            height: 300px;
            width: 600px;
            border: 1px solid #000;
            position: absolute;
            top: 0;
            left: 550px;
            overflow: hidden;
            display: none;
        }

        .bigimg {
            position: absolute;
            left: 0;
            top: 0;
        }
    </style>
</head>

<body>
    <div id="miniBox">
        <img src="./img/11.jpg" alt="">
        <div class="mask"></div>
    </div>

    <div id="bigBox">
        <img src="./img/11.jpg" class="bigimg" alt="">
    </div>



</body>
<script>
    var miniBox = document.getElementById("miniBox")
    var bigBox = document.getElementById("bigBox")
    var mask = miniBox.getElementsByClassName("mask")[0]

    miniBox.addEventListener("mouseover", function (e) {
        mask.style.display = "block"
        bigBox.style.display = "block"
    })
    miniBox.addEventListener("mouseout", function (e) {
        mask.style.display = "none"
        bigBox.style.display = "none"
    })

    miniBox.addEventListener("mousemove", function (e) {

        //鼠标在minibox中的位置;pageX是鼠标在屏幕中的位置;
        var mouseX = e.pageX - this.offsetLeft;
        var mouseY = e.pageY - this.offsetTop;
        console.log(mouseX, mouseY)
        //鼠标在minibox移动的位置X轴
        var moveX = mouseX - mask.offsetWidth / 2
        //mask的X移动距离
        var moveMaxX = miniBox.offsetWidth - mask.offsetWidth
        if (moveX <= 0) {
            moveX = 0
        } else if (
            moveX >= moveMaxX
        ) {
            moveX = moveMaxX
        }
        //鼠标在minibox移动的位置Y轴
        var moveY = mouseY - mask.offsetHeight / 2
        //mask的最大Y移动距离
        var moveMaxY = miniBox.offsetHeight - mask.offsetHeight

        if (moveY <= 0) {
            moveY = 0
        } else if (
            moveY >= moveMaxY
        ) {
            moveY = moveMaxY
        }
        //mask的移动位置
        mask.style.left = moveX + "px"
        mask.style.top = moveY + "px"

        var bigImg = document.querySelector(".bigimg")//大图
        //大图片最大移动距离
        var bigImgMax = bigImg.offsetWidth - bigBox.offsetWidth
        //大图片移动距离xy
        var bigX = moveX * bigImgMax / moveMaxX
        var bigY = moveY * bigImgMax / moveMaxY
        //移动
        bigImg.style.left = -bigX + "px"
        bigImg.style.top = -bigY + "px"
    })


</script>

</html>

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的JavaScript商品放大镜的实现方法: 1. HTML结构 ```html <div class="product-image"> <img src="product-image.jpg" alt="Product Image"> <div class="zoom-container"></div> </div> ``` 2. CSS样式 ```css .product-image { position: relative; width: 400px; height: 400px; } .product-image img { width: 100%; height: 100%; object-fit: cover; } .zoom-container { position: absolute; top: 0; right: -400px; width: 400px; height: 400px; border: 1px solid #ccc; overflow: hidden; display: none; } .zoom-container img { position: absolute; top: 0; left: 0; width: 800px; height: 800px; } ``` 3. JavaScript代码 ```javascript const productImage = document.querySelector('.product-image'); const zoomContainer = document.querySelector('.zoom-container'); const zoomImage = document.createElement('img'); zoomImage.src = 'product-image.jpg'; zoomContainer.appendChild(zoomImage); productImage.addEventListener('mousemove', function(e) { const x = e.pageX - this.offsetLeft; const y = e.pageY - this.offsetTop; const zoomX = x / this.offsetWidth * zoomImage.offsetWidth - zoomContainer.offsetWidth / 2; const zoomY = y / this.offsetHeight * zoomImage.offsetHeight - zoomContainer.offsetHeight / 2; zoomImage.style.transform = `translate(${-zoomX}px, ${-zoomY}px)`; zoomContainer.style.display = 'block'; }); productImage.addEventListener('mouseleave', function() { zoomContainer.style.display = 'none'; }); ``` 以上代码实现了一个简单的商品放大镜效果,当鼠标在商品图片上移动时,会在旁边显示一个放大的镜头,镜头中显示的是鼠标所在位置的放大图像。当鼠标离开商品图片时,放大镜头会消失。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值