JavaScript 设置放大镜效果

效果要求

1.鼠标移至图片上方,鼠标周围出现黄色的的正方形框,黄色矩形框会随着鼠标的移动而移动;

2.将黄色正方形框里的内容的长和宽均放大2.4倍,并在图片右边进行显示。

实现原理:

为对应的元素绑定 onmousemove 响应函数

使得鼠标在对应元素内移动时将需要的元素设置相应的display属性

并通过数学计算,调整相应元素的 topleft

用 if判断 实现对放大镜区域限制

为对应的元素绑定 onmouseleave 响应函数

代码展示

html:

<!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>
    <link rel="stylesheet" href="./index.css">
</head>
<body style="height: 2000px;">
    <div class="wrapper">
        <div class="product">
            <img src="./img/350px.jpg" alt="">
            <span class="yellow"></span>
        </div>
        <div class="grow"></div>
    </div>
    <script src="./index.js"></script>
</body>

</html>

css:

*{
    margin: 0;
    padding: 0;
}

.wrapper{
    width: 1200px;
    height: 800px;
    margin: 0 auto;
}

.product{
    position: relative;
    width: 333px;
    height: 333px;

    float: left;
}

.product img{
    display: block;
    width: 333px;
    height: 333px;

    position: absolute;
    z-index: 1;
}
.yellow{
    width: 166.5px;
    height: 166.5px;
    display: block;
    background-color: rgba(255, 255, 0, 0.4);

    position: absolute;
    z-index: 2;

    display: none;
}

.grow{
    position: absolute;
    top: 0;
    left: 500px;

    width: 397.2px;
    height: 397.2px;
    background-color: rebeccapurple;
    background: url(./img/800px.jpg) no-repeat;

    display: none;
}

js:

//获取节点
var product = document.querySelector(".product")
var yellow = document.querySelector(".yellow")
var grow = document.querySelector(".grow")

//移出时候隐藏放大镜和效果图
product.onmouseleave = function(){
    // 隐藏元素
    yellow.style.display = "none"
    grow.style.display = "none"
}

//绑定响应函数
product.onmousemove = function (e) {
    // 显示元素
    yellow.style.display = "block"
    grow.style.display = "block"

    //调整top值和left值
    var top = e.pageY - yellow.offsetHeight / 2 - product.offsetTop
    var left = e.pageX - yellow.offsetWidth / 2 - product.offsetLeft

    //限制区域
    if (top < 0) {
        top = 0
    } else if (top > 165) {
        top = 165.5
    }
    if (left < 0) {
        left = 0
    } else if (left > 165) {
        left = 165.5
    }

    // 改变定位位置
    yellow.style.top = top + "px"
    yellow.style.left = left + "px"

    // 计算相应的比例
    var ratioY = top / (product.offsetHeight - yellow.offsetHeight)
    var ratioX = left / (product.offsetWidth - yellow.offsetWidth)

    // 改变背景图片位置
    grow.style.backgroundPositionY = (- ratioY * grow.offsetHeight) + "px"
    grow.style.backgroundPositionX = (- ratioX * grow.offsetWidth) + "px"
}

效果展示:

放大镜效果

  • 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
发出的红包

打赏作者

O_oregou

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值