简体   繁体   English

Angular Resize Event:防止用户在父 Div 之外调整内部 Div 的大小

[英]Angular Resize Event: prevent user from resizing inner Div outside of parent Div

How to prevent user from resizing inner Div in the demo outside of Outer Div.如何防止用户在外部 Div 之外的演示中调整内部 Div 的大小。 I am using angular-resize-event.我正在使用 angular-resize-event。

https://www.npmjs.com/package/angular-resize-event https://www.npmjs.com/package/angular-resize-event

Stackblitz: https://stackblitz.com/edit/angular-ivy-mcldtj?file=src%2Fapp%2Fapp.component.html Stackblitz: https ://stackblitz.com/edit/angular-ivy-mcldtj ? file = src%2Fapp%2Fapp.component.html

app.component.ts app.component.ts

<div class="parent">
    Outer Div
    <div id="resizableContainer" >
        Inner Div
    </div>
</div>

app.module.ts----- app.module.ts-----

import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { FormsModule } from '@angular/forms';
import { AngularResizedEventModule } from 'angular-resize-event';
import { AppComponent } from './app.component';

@NgModule({
  imports:      [ BrowserModule, FormsModule ],
  declarations: [ AppComponent ],
  bootstrap:    [ AppComponent ]
})
export class AppModule { }

app.component.ts app.component.ts

import { Component, VERSION } from "@angular/core";
import { ResizedEvent } from "angular-resize-event";
@Component({
  selector: "my-app",
  templateUrl: "./app.component.html",
  styleUrls: ["./app.component.css"]
})
export class AppComponent {
  public width: number;
  public height: number;
  constructor() {}



  onResized(event) {
    console.log(event)
    this.width = event.newWidth;
    this.height = event.newHeight;
  }
}

style.css---样式.css---

div#resizableContainer{
  border: 2px solid;
  margin: 20px 0px 20px 0px;
  padding: 20px; 
  width: auto;
  height:300px;
  resize: both; 
  overflow: auto;
}

.parent{
  padding:10px;
  border:2px solid;
}

Thanks谢谢

You just need to specify a max-width on the inner resizable div so the resizing stops at the maximum width specified.您只需要在内部可调整大小的 div 上指定一个最大宽度,以便调整大小在指定的最大宽度处停止。 You can do this in CSS without the need for any Angular code.您可以在 CSS 中执行此操作,而无需任何 Angular 代码。

Here is the updated CSS:这是更新后的 CSS:

div#resizableContainer{
  border: 2px solid;
  margin: 20px 0px 20px 0px;
  padding: 20px; 
  width: auto;
  max-width: calc(100% - 44px);
  height:300px;
  resize: both; 
  overflow: auto;
}

I'm using the calc function to determine the size because you have borders, margins and paddings specified that need to be taken into account when determining the max width.我正在使用calc 函数来确定大小,因为在确定最大宽度时需要考虑指定的边框、边距和填充。 The 44px value is all the borders, margins and paddings summed up. 44px 值是所有边框、边距和填充的总和。 In your StackBlitz sample there is an 8px margin on the body element on both sides so this is 16px.在您的 StackBlitz 示例中,body 元素两侧的边距为 8px,因此为 16px。 The outer div has a 2px border and 10px margin again on both sides so this is 24px.外部 div 有一个 2px 的边框和 10px 的两边边距,所以这是 24px。 And last the inner div has a 2px border so you need to add another 4px.最后内部 div 有一个 2px 的边框,所以你需要添加另一个 4px。

Here is also a link to a fork of your StackBlitz sample where you can see it working.这里还有一个指向 StackBlitz 示例的分支的链接,您可以在其中看到它的工作情况。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM