You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何放大并准确定位精灵图(Sprite)中的图片?

精灵图点击放大并保留注释的实现问题

我将多张文档图片合并为一张精灵图(Sprite),目前可显示带有注释的小尺寸图片。但希望实现:当用户点击该小图时,将图片放大至更易阅读的尺寸,同时仍保留注释可见。

尝试多种方法均未成功实现图片的放大与准确定位,以下是三次尝试的代码及对应效果:

尝试1

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>

figure {display: block;
        border-width:thin;
}
figcaption {background-color:yellow;}

#img1, #img2, #img3 {
    object-fit: none;
    object-position: 0 0;
    width:  816px; // full size 3264
    height: 612px; // full size 2448
}
</style>
</head>
<body>

<figure id="fig1">
    <figcaption>Camberley Mail</figcaption>
        <img id="img1" src="bates-sprite.jpeg"
             style="height: 100vh;
                    transform: scale(2,2);
                    position: "absolute";
                    top: 206px;
                    left: 48px;">

    aa<p>Text to go with picture.</p>

</figure>
<div style="position: absolute; top: 206px; left: 48px; color: white;
            background-color: green;">====== 206px ======</div>

</body>
</html>

尝试1效果

尝试2

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>

figure {display: block;
        border-width:thin;
}
figcaption {background-color:yellow;}

#img1, #img2, #img3 {
  background: url("bates-sprite.jpeg");
  background-repeat: no-repeat;
  object-fit: none;
  object-position: 0 0;
  width:  816px; // full size 3264
  height: 612px; // full size 2448
}
</style>
</head>
<body>

<figure id="fig1">
    <figcaption>Camberley Mail</figcaption>
    <img id="img1" src="bates-sprite.jpeg"
         style="height: 100vh;
                transform: scale(2,2);">
    <p>Text to go with picture.</p>

</figure>
<div style="position: absolute; top: 206px; left: 48px; color: white;
            background-color: green;">====== 206px ======</div>
    <script>
    "use strict";
    const img = document.getElementById ('img1');
    img.style.position = 'absolute';
    img.style.left = "48px";
    img.style.top = "206px";
    </script>
</body>
</html>

尝试2效果

尝试3

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>

figure {display: block;
        border-width:thin;
}
figcaption {background-color:yellow;}

#img1, #img2, #img3 {
  object-fit: none;
  object-position: 0 0;
  width:  816px; // full size 3264
  height: 612px; // full size 2448
}
</style>
</head>
<body>

<figure id="fig1">
    <figcaption>Camberley Mail</figcaption>
    <img id="img1" src="bates-sprite.jpeg"
         style="height: 100vh;
                transform: scale(2,2) translate(48px,206px);">

    <p>Text to go with picture.</p>

</figure>
<div style="position: absolute; top: 206px; left: 48px; color: white;
            background-color: green;">====== 206px ======</div>

</body>
</html>

尝试3效果


内容的提问来源于stack exchange,提问作者Hobbyist

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.20 10:42:23