小南爱吃汉堡 2024-04-22 16:16 采纳率: 60%
浏览 11

放大镜js无法显示效果

#wrap {
  width: 100%;
  height: 100%;
}
#wrap .container {
  width: 1210px;
  margin: 0 auto;
  position: relative;
}
#wrap .container .small-wrap {
  position: relative;
  width: 325px;
  height: 325px;
  border: 1px solid #eee;
  position: relative;
}
#wrap .container .small-wrap img {
  width: 100%;
  height: 100%;
}
#wrap .container .small-wrap .yellow-box {
  width: 200px;
  height: 200px;
  border: 1px solid #aaa;
  background: rgba(255, 0, 0, 0.3);
  position: absolute;
  left: 0;

![img](  top: 0;
  overflow: hidden;
  display: none;
  cursor: move;
}
#wrap .container .big-wrap {
  width: 470px;
  height: 470px;
  border: 1px solid #e4e4e4;
  position: absolute;
  top: 0;
  left: 325px;
  overflow: hidden;
  display: none;
}
#wrap .container .big-wrap img {
  width: 100%;
  height: 100%;
}
index.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">
    <!-- <meta name="referrer" content="no-referrer"> -->
    <title>放大镜</title>
    <link rel="stylesheet" href="./reset.css">
    <link rel="stylesheet" href="./index.css">
</head>
<body>
    <div id="wrap">
        <div class="container">
            <div class="small-wrap">
                <div class="yellow-box"></div>
                <img src="" alt="">
            </div>
            <div class="big-wrap">
                <img src="" alt="">
            </div>
        </div>
    </div>
</body>
<script>
    var smallWrap = document.querySelector(".small-wrap");
    var yellowBox = document.querySelector(".yellow-box");
    var bigWrap = document.querySelector(".big-wrap");
    var bigImg = document.querySelector(".big-wrap img");

    yellowBox.style.width = smallWrap.clientWidth / (bigImg.clientWidth / bigWrap.clientWidth) + 'px';
    yellowBox.style.height = smallWrap.clientHeight / (bigImg.clientHeight / bigWrap.clientHeight) + 'px';
    bigWrap.style.display = 'none';

    smallWrap.addEventListener('mouseover', function () {
        yellowBox.style.display = 'block';
        bigWrap.style.display = 'block';
    });

    smallWrap.addEventListener('mouseout', function () {
        yellowBox.style.display = 'none';
        bigWrap.style.display = 'none';
    });

    smallWrap.addEventListener('mousemove', function (e) {
        var x = e.pageX - this.parentNode.offsetLeft - (yellowBox.offsetWidth / 2);
        var y = e.pageY - this.parentNode.offsetTop - (yellowBox.offsetHeight / 2);
        var maxMove = smallWrap.offsetWidth - yellowBox.offsetWidth;
        if (x < 0) {
            x = 0;
        } else if (x > maxMove) {
            x = maxMove;
        }
        if (y < 0) {
            y = 0;
        } else if (y > maxMove) {
            y = maxMove;
        }
        yellowBox.style.left = x + 'px';
        yellowBox.style.top = y + 'px';
        var bigMaxMove = bigImg.offsetHeight - bigWrap.offsetHeight;
        bigImg.style.marginLeft = -(x / maxMove) * bigMaxMove + 'px';
        bigImg.style.marginTop = -(y / maxMove) * bigMaxMove + 'px';
    });
</script>
</html>


我的屏幕尺寸比较小,这个代码是按照b站上一个程序员媛恩予写的。但是现在出现点问题如下:大图没有放大镜效果,求解!

img

  • 写回答

5条回答 默认 最新

  • 专家-赤兔[在线] 优质创作者: 编程框架技术领域 2024-04-22 16:16
    关注

    引自免费微信小程序:皆我百晓生

    看起来你想要实现的是一个鼠标悬停在小图片上时,通过放大镜查看器预览大图的部分区域。目前的问题是大图并没有随着鼠标移动进行缩放和定位。根据提供的HTML、CSS和JavaScript代码,我发现了几个可能的问题。

    首先,在style属性中,你需要将换行符替换为逗号分隔的样式。然后,确保你正确地设置了src属性,以便加载实际的图像文件。另外,为了简化计算,我们可以让黄色框始终位于小图片的中心,而不是跟随鼠标移动。这样可以避免计算复杂性,并且用户仍然可以在小图片上看到放大区域。

    这里是修改后的代码:

    index.html

    <!DOCTYPE html>
    <html lang="en">
    <head>
        ...
    </head>
    <body>
        <div id="wrap">
            <div class="container">
                <div class="small-wrap">
                    <div class="yellow-box"></div>
                    <img id="small-img" src="path/to/your/small/image.jpg" alt=""> <!-- 替换为你的小图路径 -->
                </div>
                <div class="big-wrap">
                    <img id="big-img" src="path/to/your/big/image.jpg" alt=""> <!-- 替换为你的大图路径 -->
                </div>
            </div>
        </div>
    </body>
    <script>
        var smallWrap = document.querySelector(".small-wrap");
        var yellowBox = document.querySelector(".yellow-box");
        var bigWrap = document.querySelector(".big-wrap");
        var bigImg = document.querySelector("#big-img"); // 使用id选择器
    
        var smallWidth = smallWrap.clientWidth;
        var smallHeight = smallWrap.clientHeight;
        var bigWidth = bigImg.naturalWidth;
        var bigHeight = bigImg.naturalHeight;
        var ratio = Math.min(smallWidth / bigWidth, smallHeight / bigHeight);
    
        yellowBox.style.width = ratio * bigWidth + "px";
        yellowBox.style.height = ratio * bigHeight + "px";
        yellowBox.style.left = (smallWidth - yellowBox.offsetWidth) / 2 + "px";
        yellowBox.style.top = (smallHeight - yellowBox.offsetHeight) / 2 + "px";
    
        bigWrap.style.display = 'none';
    
        smallWrap.addEventListener('mouseover', function () {
            yellowBox.style.display = 'block';
            bigWrap.style.display = 'block';
        });
    
        smallWrap.addEventListener('mouseout', function () {
            yellowBox.style.display = 'none';
            bigWrap.style.display = 'none';
        });
    
        smallWrap.addEventListener('mousemove', function (e) {
            var x = e.pageX - this.offsetLeft - (yellowBox.offsetWidth / 2);
            var y = e.pageY - this.offsetTop - (yellowBox.offsetHeight / 2);
            
            var maxMove = smallWidth - yellowBox.offsetWidth;
            if (x < 0) {
                x = 0;
            } else if (x > maxMove) {
                x = maxMove;
            }
            if (y < 0) {
                y = 0;
            } else if (y > maxMove) {
                y = maxMove;
            }
    
            var bigMaxMove = bigHeight - bigWrap.offsetHeight;
            bigImg.style.transform = `translate(-${x / maxMove * bigMaxMove}px, -${y / maxMove * bigMaxMove}px) scale(${ratio})`;
        });
    </script>
    </html>
    

    在这个版本中,我们使用了自然宽度和高度(naturalWidthnaturalHeight)来获取原始图像大小,而不是依赖于元素的宽度和高度。此外,我们还优化了放大镜的位置设置,使其始终保持在小图片的中心。最后,我们更新了大图的缩放和移动方式,使用CSS的transform属性来实现更平滑的效果。

    确保替换成正确的图片路径,这应该会解决你的问题。如果你还有其他疑问,请随时告诉我。

    评论 编辑记录

报告相同问题?

问题事件

  • 创建了问题 4月22日