【问题标题】:Fitting image to screen and allow default zoom behavior使图像适合屏幕并允许默认缩放行为
【发布时间】:2020-11-04 01:17:49
【问题描述】:

我想使图像适合视口,保持纵横比和浏览器的默认缩放行为。基本上,想要的行为就像用 Chrome 或 Firefox 打开本地图像一样。 这是我的项目的详细信息:

  • 我有一个比例为 4896x3264 的巨大 Unsplash 图像
  • 我想创建一种“Waldo 在哪里”游戏,用户必须放大以检查图像的每个细节。默认情况下,我希望图像适合屏幕(在这种情况下,最大高度为 100%)。然后用户可以使用 ctrl + 滚动(或任何其他快捷键)放大/缩小。
  • 它显示在 Angular 9 应用程序中,组件的 html 简单地是: <img src="../../assets/img/Background.webp"/> 我用我的<router-outlet> 访问这个 html。

因此,现在当导航到此页面时,图像会以其原始尺寸 4896x3264 显示,这对于常规屏幕来说太高了。使用 CSS,我可以设置高度和宽度以相应地适应视口(并将其居中),如下所示:

   img {
      max-height: 100%;
      display: block;
    }

但是,使用此解决方案,我无法再放大查看图像的细节。即使我放大到 500%,图像也​​会保持合适(在我的屏幕上为 1536 x 722)。

在浏览器(Chrome 或 Firefox)中打开本地图像时,我获得了所需的确切行为: 默认情况下(100% 缩放)它适合 1017x678。我可以通过单击放大到 4896x3264。我可以用 ctrl + scroll 逐渐缩放。 在检查控制台时,我发现当我放大和缩小时宽度和高度会自动改变: <img style="-webkit-user-select: none;margin: auto;cursor: zoom-in;"src="file:///D:/Videotec/Background.webp" width="1083" height="722">

有没有办法在我的应用程序中获得这种行为?我发现了很多关于将带有 css 的图像拟合到视口的主题,但我找不到像我的项目这样的东西。由于英语不是我的母语,我可能无法很好地在网上搜索,如果已经解决了类似问题,请告诉我。

实际行为截图:https://ibb.co/DLChrtp

想要的默认行为截图:https://ibb.co/p0hLDxn

提前感谢您的帮助。

编辑: 我不确定我是否全面描述了想要的行为(我的词汇限制了我)。查看@NVRM 答案了解更多详情

编辑解决方法: 因为我没有找到开箱即用的东西,所以我用 Typescript + CSS 重新创建了这个行为。我的想法只是将 ngClass 从响应式类(最大高度:100%)切换到没有宽度和高度限制的原始类。在我的游戏中,我必须检索点击的确切位置,我也将它与 scrollTo 一起使用,以便将视图置于所需坐标的中心。
我不得不使用lifecycleHook AfterViewChecked,因为我需要在切换类后滚动视图。 我必须使用滚动标志(布尔值),否则当我单击scrollOut 时也会触发scrollTo(x,y)。 我知道这不是一个干净的解决方案(我实际上有比下面更多的代码,处理 dblclick 等其他东西,所以某些部分对你来说可能不是最佳的)但它确实是想要的行为,所以我现在会坚持下去.

在.html中

<div #videotecContainer class="videotec-container">
  <img
    #videotecImage
    src="../../assets/img/Videotec.webp"
    [ngClass]="zoomed ? 'zoomedIn' : 'zoomedOut'"
    (click)="onClick($event)"
  />
</div>

在 .ts 中:

zoomed: boolean = false;
scrolled: boolean = false;
xPosition: number;
yPosition: number;
@ViewChild("videotecImage", { static: true }) videotecImage: ElementRef;
@ViewChild("videotecContainer", { static: true }) videotecContainer: ElementRef;

onClick(event: any) {
     const coordinates = this.getPixelPositions(event);
     this.xPosition = coordinates.px - coordinates.clientWidth / 2;
     this.yPosition = coordinates.py - coordinates.clientHeight / 2;
     this.zoomImage();
 }

zoomImage() {
    if (this.zoomed && this.scrolled) this.scrolled = false;
    this.zoomed = !this.zoomed;
  }

ngAfterViewChecked() {
    if (this.zoomed && !this.scrolled) {
      this.videotecContainer.nativeElement.scrollTo(
        this.xPosition,
        this.yPosition
      );
      this.scrolled = true;
    }

getPixelPositions(event) {
  //do some calculation based on Wolfgang Fahl answer here : 
  //questions/34867066
}

在 .css 中:

.zoomedOut {
  max-height: 100%;
  max-width: 100%;
  display: block;
  margin: auto;
}

.zoomedIn {
  display: block;
}

#videotecImage {
  overflow: scroll;
}
   
.videotec-container {
  height: 100vh;
  overflow: scroll;
  background-color: black;
}

【问题讨论】:

标签: javascript html css angular image


【解决方案1】:

不确定,如果这是您所追求的?但基本上,使用a not responsive absolute unit (like px) 的 div 背景和固定宽度几乎是缩放时的默认行为,它也会缩放图像吗?

点击«整页»查看它的缩放效果。

div {
  height: 100vh;
  background-image: url("https://i.imgur.com/p2dQlq7.jpg");
  background-size: 500px auto;
  background-repeat: no-repeat;
}
<body>
 <div></div>
</body>

【讨论】:

  • #1 感谢您的回复。好吧,这不是我想要的那种行为。在您的解决方案中,如果我放大图像被裁剪并且没有滚动条(水平或垂直)可以在缩放的图像周围爬行。想要的行为就像您直接在浏览器中打开图像一样:i.imgur.com/p2dQlq7.jpg 当您去那里时,图像适合您的屏幕并且您的光标变成放大镜。如果您在光标为“+”时单击,图像将变为全尺寸,您可以水平或垂直滚动​​。
  • #2 如果您在光标为“-”时单击此图像,则此图像会在 100% 高度处恢复到合适的大小。除了光标行为之外,您还可以通过使用 ctrl + 滚动更改浏览器显示大小来放大/缩小(从 25% 变为 500%)。这似乎是浏览器在打开本地图像时的默认行为,这就是为什么我想知道他们如何处理它,是 javascript 吗?我可以在控制台中看到缩放时高度和宽度值发生变化
  • #3 当我将 CSS 与 background-image ou img 一起使用时,它不会动态更改高度和宽度的值
猜你喜欢
  • 1970-01-01
  • 2021-02-19
  • 2012-05-10
  • 2016-08-09
  • 1970-01-01
  • 2012-05-19
  • 1970-01-01
  • 2016-01-05
  • 1970-01-01
相关资源
最近更新 更多