CSS比例尺和可滚动容器中的中心图像而无需裁剪

发布于 2025-01-22 17:28:46 字数 579 浏览 2 评论 0原文

我想缩放图像,以便用户可以在变焦后滚动。 以下示例效果很好,但我必须将原点设置为左上角,否则图像被裁剪到顶部和左上。无论如何,是否可以扩展到图像的中心(就像我省略转换原素时发生的那样),但没有裁剪?

请注意,我使用比例尺,因为图像将具有关联的图像映射,因此我不能仅设置宽度和高度。

#container {
    width: 500px;
    height: 500px;
    overflow: auto;
}

#pic {
    transform: scale(2);
    transform-origin: top left;
}
<div id="container">
  <img id="pic" src="https://picsum.photos/id/237/500/500" >
</div>

I'd like to zoom an image so that the user can scroll around it after the zoom.
The following example works well but I have to set the origin to top left as otherwise the image is cropped to the top and left. Is there anyway to scale to the centre of the image (as happens when I omit the transform-origin) but without cropping?

Please note I'm using scale because the image will have an associated image map so I can't just set the width and height.

#container {
    width: 500px;
    height: 500px;
    overflow: auto;
}

#pic {
    transform: scale(2);
    transform-origin: top left;
}
<div id="container">
  <img id="pic" src="https://picsum.photos/id/237/500/500" >
</div>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

累赘 2025-01-29 17:28:46

此代码在这里,修复程序是右上角,位置:绝对;

#container {
    display:block;
    margin:auto;
    width: 500px;
    height: 500px;
    overflow: auto;
}

#pic {
    transform: scale(2);
    transform-origin: top left;
}
<div id="container">
  <img id="pic" src="https://picsum.photos/id/237/500/500" >
</div>
<div id="container">
  <img id="pic" src="https://picsum.photos/id/237/500/500" >
</div>

This code right here, fixes is to top right, with position:absolute;

#container {
    display:block;
    margin:auto;
    width: 500px;
    height: 500px;
    overflow: auto;
}

#pic {
    transform: scale(2);
    transform-origin: top left;
}
<div id="container">
  <img id="pic" src="https://picsum.photos/id/237/500/500" >
</div>
<div id="container">
  <img id="pic" src="https://picsum.photos/id/237/500/500" >
</div>

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文