JavaScript:京东放大镜效果

目标效果: 

 HTML:

    <div class="bigg">
        <img src="DSC_1929.jpg" alt="">
        <div class="small"></div>
        <div class="biggr">
            <img src="DSC_1929 copy.jpg" alt="" class="imgg">
        </div>
    </div>

small是阴影区   biggr是右边的放大区

CSS:

    <style>
        * {
            padding: 0;
            margin: 0;
        }

        .bigg {
            position: relative;
            width: 500px;
            height: 700px;
            background-color: red;


            margin: 100px auto;

        }

        .bigg>img {
            width: 500px;
            height: 700px;
        }

        .small {
            position: absolute;
            display: none;
            top: 0;
            left: 0;
            width: 200px;
            height: 200px;
            background-color: rgba(0, 0, 0, 0.6);
        }

        .biggr {
            position: absolute;
            /* display: none; */
            top: 0;
            left: 550px;
            width: 800px;
            height: 800px;
            overflow: hidden;
            
        }

        .biggr .imgg {
            position: absolute;

            top: 0;
            left: 0;
        }
    </style>

JavaScript:

    <script>
        window.addEventListener('load', function () {
            var bigg = document.querySelector('.bigg');

            var small = document.querySelector('.small');
            var biggr = document.querySelector('.biggr');
            var imgg = biggr.querySelector('.imgg')

            //鼠标进入时  相关元素显示
            bigg.addEventListener('mouseover', function (e) {
                small.style.display = 'block';
                biggr.style.display = 'block';
                console.log(2);

            })

            //鼠标离开时  相关元素显示
            bigg.addEventListener('mouseout', function () {

                small.style.display = 'none';
                biggr.style.display = 'none';
            })

            bigg.addEventListener('mousemove', function (e) {
                //获取阴影区的移动距离
                var xxori = e.pageX - bigg.offsetLeft - small.offsetWidth / 2;
                var yyori = e.pageY - bigg.offsetTop - small.offsetHeight / 2;

                //给阴影区的移动距离设置限制,使其不至于超出边界

                if(xxori<0){
                    xxori = 0;
                }
                else if(xxori>bigg.offsetWidth -small.offsetWidth){
                    xxori = bigg.offsetWidth -small.offsetWidth
                }

                if(yyori<0){
                    yyori = 0;
                }
                else if(yyori>bigg.offsetHeight -small.offsetHeight){
                    yyori = bigg.offsetHeight -small.offsetHeight
                }

                //给阴影区设置移动距离
                small.style.top = yyori + 'px';
                small.style.left = xxori + 'px';

                //给右边大图片设置移动距离
 

                imgg.style.top = -yyori*(imgg.offsetHeight - biggr.offsetHeight)/ (bigg.offsetHeight -small.offsetHeight)+ 'px';
                imgg.style.left = -xxori*(imgg.offsetHeight - biggr.offsetHeight)/ (bigg.offsetHeight -small.offsetHeight)+ 'px';
            })

        })



    </script>

值得注意的是,最后 右边大盒子的移动距离采用的是比例的计算方法,且方向相反  即

= -阴影区移动距离*大图片最大移动距离/阴影区最大移动距离

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 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、付费专栏及课程。

余额充值