js实现商品详情页的放大镜

这是京东官网的效果图

这是我实现的简易版的放大镜

下面我们就简单的实现一下放大镜的效果吧!

html代码

    <div id="zoomBox">

      <div id="midArea">

        <img src="img/m01.jpg" />

        <div id="zoom"></div>

      </div>

      <div id="bigArea">

        <img src="img/m01.jpg" />

      </div>

      <div id="smallArea">

        <img src="img/m01.jpg" />

        <img src="img/m02.jpg" />

      </div>

    </div>

css代码

<style>

      img {

        display: block;

      }

      #zoomBox {

        position: relative;

        width: 400px;

        border: 1px solid #cecece;

      }

      #midArea img {

        width: 400px;

        height: 400px;

      }

      #zoom {

        display: none;

        position: absolute;

        width: 200px;

        height: 200px;

        background-color: yellow;

        opacity: 0.5;

        cursor: move;

      }

      #bigArea {

        display: none;

        position: absolute;

        width: 400px;

        height: 400px;

        overflow: hidden;

        border: 1px solid #cecece;

        left: 400px;

        top: -1px;

      }

      #bigArea img {

        position: absolute;

        width: 800px;

        height: 800px;

      }

      #smallArea {

        height: 50px;

        padding: 10px;

        border-top: 1px solid #cecece;

      }

      #smallArea img {

        float: left;

        width: 50px;

        height: 50px;

        margin-right: 10px;

      }

    </style>

js代码

    <script>

      function $(id) {

        return document.getElementById(id);

      }

      class Zoom {

        constructor() {

          this.zoomBox = $("zoomBox");

          this.midArea = $("midArea");

          this.midImg = this.midArea.children[0]; //中图

          this.zoom = $("zoom"); //放大镜

          this.bigArea = $("bigArea");

          this.bigImg = this.bigArea.children[0]; //大图

          this.smallArea = $("smallArea");

          this.smallImgs = this.smallArea.children;

          this.move();

        }

        move() {

          this.midArea.onmouseover = () => {

            this.zoom.style.display = "block"; //放大镜显示

            this.bigArea.style.display = "block"; //大图显示

          };

          this.midArea.onmouseout = () => {

            this.zoom.style.display = "none";

            this.bigArea.style.display = "none";

          };

          this.midArea.onmousemove = (e) => {

            let ml = this.midArea.offsetWidth - this.zoom.offsetWidth; //放大镜最大left值,限定范围

            let mt = this.midArea.offsetHeight - this.zoom.offsetHeight;

            //放大镜跟着鼠标走,鼠标在放大镜中间

            let l =

              e.pageX - this.zoomBox.offsetLeft - this.zoom.offsetWidth / 2;

            let t =

              e.pageY - this.zoomBox.offsetTop - this.zoom.offsetHeight / 2;

            l = l <= 0 ? 0 : l >= ml ? ml : l;

            t = t <= 0 ? 0 : t >= mt ? mt : t;

            this.zoom.style.left = l + "px";

            this.zoom.style.top = t + "px";

            this.bigImg.style.left =

              (-this.midArea.offsetWidth / this.zoom.offsetWidth) * l + "px";

            this.bigImg.style.top =

              (-this.midArea.offsetHeight / this.zoom.offsetHeight) * t + "px";

          };

          //图片切换

          for (let i = 0; i < this.smallImgs.length; i++) {

            this.smallImgs[i].onmouseover = () => {

              this.midImg.src = this.bigImg.src = this.smallImgs[i].src;

            };

          }

        }

      }

      new Zoom();

    </script>

上面是我的代码,希望对大家可以有所帮助!

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
商品放大镜是一种常见的电商网站交互效果,可以让用户在鼠标悬停在商品图片上时,放大图片的局部细节,提高用户购买体验。下面是一个基于JavaScript实现商品放大镜示例: HTML: ```html <div class="product-image"> <img src="product.jpg" alt="Product"> <div class="zoom-window"></div> </div> ``` CSS: ```css .product-image { position: relative; width: 400px; height: 400px; } .product-image img { display: block; max-width: 100%; max-height: 100%; } .zoom-window { position: absolute; top: 0; left: 100%; width: 200px; height: 200px; border: 1px solid #ccc; display: none; overflow: hidden; } ``` JavaScript: ```javascript const productImage = document.querySelector('.product-image'); const zoomWindow = document.querySelector('.zoom-window'); productImage.addEventListener('mouseenter', () => { zoomWindow.style.display = 'block'; }); productImage.addEventListener('mousemove', (event) => { const clientX = event.clientX - productImage.offsetLeft; const clientY = event.clientY - productImage.offsetTop; const zoomX = clientX / productImage.clientWidth * 100; const zoomY = clientY / productImage.clientHeight * 100; zoomWindow.style.backgroundImage = `url(product.jpg)`; zoomWindow.style.backgroundSize = `${productImage.clientWidth * 2}px ${productImage.clientHeight * 2}px`; zoomWindow.style.backgroundPosition = `-${zoomX}% -${zoomY}%`; zoomWindow.style.left = `${clientX + 10}px`; zoomWindow.style.top = `${clientY + 10}px`; }); productImage.addEventListener('mouseleave', () => { zoomWindow.style.display = 'none'; }); ``` 解释一下代码: 首先,我们通过document.querySelector()方法获取了HTML中的商品图片和放大镜容器元素,分别是.product-image和.zoom-window。 然后,我们给商品图片添加了三个事件监听器:mouseenter、mousemove和mouseleave。当鼠标进入商品图片区域时,我们显示放大镜容器;当鼠标在商品图片上移动时,我们根据鼠标位置计算出放大镜容器内的背景图像位置和偏移值,并动态设置.zoom-window的CSS样式;当鼠标离开商品图片区域时,我们隐藏放大镜容器。 注意,我们在mousemove事件处理程序中用到了event.clientX和event.clientY来获取鼠标在视口中的位置,然后减去productImage.offsetLeft和productImage.offsetTop来获取鼠标相对于商品图片左上角的位置。然后,我们计算出放大镜容器内的背景图像位置和偏移值,并动态设置.zoom-window的CSS样式。最后,我们将放大镜容器的位置设置为鼠标位置加上10像素,以便放大镜容器不会遮挡住原始图片。 这就是一个简单的基于JavaScript实现商品放大镜。当然,你可以根据自己的需求进行修改和扩展。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值