html 如何防止元素被剪裁?

goucqfw6  于 2024-01-04  发布在  其他
关注(0)|答案(1)|浏览(138)

我目前正在开发一个盒子容器组件,用户可以在盒子容器中添加尽可能多的盒子组件。每个盒子都有一个150pxwidth350pxheight,容器有一个600pxmax-width,所以它可以容纳多达4个盒子组件。本质上,如果用户要添加第5个盒子,内容将被隐藏,因为overflow: hidden设置到容器。
我面临的问题是,最后一个box组件被box-container裁剪,这样按钮就被截断了,如下所示。
我知道overflow: hidden在某种程度上做的工作严格,它只是剪辑任何元素沿着的方式,但我正在寻找一个变通办法,我可以显示按钮,如下图,同时保留滚动overflow: hidden功能。
我想显示按钮,而内容溢出是隐藏的。
下面是我的box组件的代码:

box.component.html

  1. <div class="box">
  2. <button class="decrease-temp" (click)="onDecreaseClick()">-</button>
  3. <button class="increase-temp" (click)="onIncreaseClick()">+</button>
  4. </div>

字符串

box.component.css

  1. .box {
  2. width: 150px;
  3. height: 350px;
  4. background-color: darkblue;
  5. border: 1px solid black;
  6. position: relative;
  7. z-index: 1;
  8. }
  9. .box:hover {
  10. z-index: 2;
  11. }
  12. .box:hover button {
  13. visibility: visible;
  14. }
  15. button {
  16. height: 30px;
  17. width: 30px;
  18. outline: none;
  19. border: none;
  20. background-color: thistle;
  21. border: 1px solid #000;
  22. visibility: hidden;
  23. z-index: 101;
  24. }
  25. button:hover {
  26. height: 30px;
  27. width: 30px;
  28. outline: none;
  29. border: none;
  30. background-color: rgb(253, 139, 253);
  31. border: 1px solid #000;
  32. z-index: 101;
  33. }
  34. .decrease-temp {
  35. position: absolute;
  36. top: 25px;
  37. left: -15px;
  38. }
  39. .increase-temp {
  40. position: absolute;
  41. top: 25px;
  42. left: 133px;
  43. }

box.component.ts

  1. import { Component, Output, EventEmitter } from '@angular/core';
  2. @Component({
  3. standalone: true,
  4. selector: 'app-box',
  5. templateUrl: './box.component.html',
  6. styleUrls: ['./box.component.css']
  7. })
  8. export class BoxComponent {
  9. @Output() increase = new EventEmitter<void>();
  10. @Output() decrease = new EventEmitter<void>();
  11. onIncreaseClick() {
  12. this.increase.emit();
  13. }
  14. onDecreaseClick() {
  15. this.decrease.emit();
  16. }
  17. }


下面是box容器的代码:

box-test.component.html

  1. <div class="container">
  2. <div class="config-wrapper">
  3. <div class="config">
  4. <app-box *ngFor="let box of boxes" (increase)="addBox()" (decrease)="removeBox()"></app-box>
  5. </div>
  6. </div>
  7. </div>

box-test.component.css

  1. .container {
  2. max-width: 600px;
  3. height: 350px;
  4. overflow: auto;
  5. }
  6. .config-wrapper {
  7. display: flex;
  8. background-color: gray;
  9. overflow: hidden;
  10. }
  11. .config {
  12. position: relative;
  13. display: flex;
  14. }

box-test.component.ts

  1. import { Component } from '@angular/core';
  2. import { BoxComponent } from '../box/box.component';
  3. import { CommonModule } from '@angular/common';
  4. @Component({
  5. standalone: true,
  6. imports: [ BoxComponent, CommonModule ],
  7. selector: 'app-box-test',
  8. templateUrl: './box-test.component.html',
  9. styleUrls: ['./box-test.component.css']
  10. })
  11. export class BoxTestComponent {
  12. boxes = [1]
  13. addBox() {
  14. this.boxes.push(this.boxes.length + 1);
  15. }
  16. removeBox() {
  17. this.boxes.pop();
  18. }
  19. }

lvmkulzt

lvmkulzt1#

不幸的是,据我所知,不可能为overflow: hidden提供一个例外,因为父节点只对特定的子节点,所以你需要将按钮带到与父节点相同的级别,我使用了多个概念来让它工作,请通过代码,让我知道如果有任何疑问!
主ts

  1. import { CommonModule } from '@angular/common';
  2. import { Component, ElementRef, ViewChild } from '@angular/core';
  3. import { bootstrapApplication } from '@angular/platform-browser';
  4. import { Subject } from 'rxjs';
  5. import { debounceTime } from 'rxjs/operators';
  6. import 'zone.js';
  7. import { BoxComponent } from './box/box.component';
  8. @Component({
  9. selector: 'app-root',
  10. standalone: true,
  11. imports: [BoxComponent, CommonModule],
  12. template: `
  13. <div class="container">
  14. <div class="config-wrapper">
  15. <div class="config">
  16. <app-box *ngFor="let box of boxes;let index = index" (hover)="onHover($event, index)" (blur)="onBlur($event)"></app-box>
  17. </div>
  18. </div>
  19. <div class="button-wrapper" [hidden]="!showButtons" #buttons
  20. (mouseover)="onMouseOverButtons($event)"
  21. (mouseout)="onMouseOutButtons($event)" [style.left]="position * 150 + 'px'">
  22. <button class="decrease-temp" (click)="removeBox()">-</button>
  23. <button class="increase-temp" (click)="addBox()">+</button>
  24. </div>
  25. </div>
  26. {{buttonsHover}} || {{position}}
  27. `,
  28. styles: [
  29. `
  30. .container {
  31. max-width: 600px;
  32. height: 352px;
  33. overflow: visible;
  34. position: relative;
  35. }
  36. .config-wrapper {
  37. display: flex;
  38. background-color: gray;
  39. overflow: hidden;
  40. }
  41. .config {
  42. position: relative;
  43. display: flex;
  44. }
  45. .button-wrapper {
  46. width: 150px;
  47. height:50px;
  48. position: absolute;
  49. z-index: 5;
  50. top:30px;
  51. left: 0px;
  52. }
  53. /*
  54. .box:hover button {
  55. visibility: visible;
  56. } */
  57. button {
  58. height: 30px;
  59. width: 30px;
  60. outline: none;
  61. border: none;
  62. background-color: thistle;
  63. border: 1px solid #000;
  64. z-index: 101;
  65. cursor: pointer;
  66. }
  67. button:hover {
  68. height: 30px;
  69. width: 30px;
  70. outline: none;
  71. border: none;
  72. background-color: rgb(253, 139, 253);
  73. border: 1px solid #000;
  74. z-index: 101;
  75. }
  76. .decrease-temp {
  77. position: absolute;
  78. top: 25px;
  79. left: -15px;
  80. }
  81. .increase-temp {
  82. position: absolute;
  83. top: 25px;
  84. left: 133px;
  85. }
  86. `,
  87. ],
  88. })
  89. export class App {
  90. @ViewChild('buttons') buttons: ElementRef<any> | null = null;
  91. boxes = [1];
  92. showButtons = false;
  93. buttonsHover = false;
  94. position = 0;
  95. subject = new Subject<boolean>();
  96. ngOnInit() {
  97. this.subject.pipe(debounceTime(500)).subscribe((data: boolean) => {
  98. if (!this.buttonsHover) {
  99. this.showButtons = data;
  100. }
  101. });
  102. }
  103. onHover(event: Event, index: number) {
  104. console.log(event);
  105. this.position = index;
  106. //if (event.target)
  107. this.subject.next(true);
  108. }
  109. onBlur(event: Event) {
  110. console.log(event);
  111. this.subject.next(false);
  112. }
  113. onMouseOverButtons(event: Event) {
  114. console.log(event);
  115. //if (event.target)
  116. this.buttonsHover = true;
  117. this.subject.next(false);
  118. }
  119. onMouseOutButtons(event: Event) {
  120. console.log(event);
  121. this.buttonsHover = false;
  122. this.subject.next(false);
  123. }
  124. addBox() {
  125. this.boxes.push(this.boxes.length + 1);
  126. }
  127. removeBox() {
  128. this.boxes.pop();
  129. }
  130. }
  131. bootstrapApplication(App);

字符串
框html

  1. <div class="box"></div>


Box CSS

  1. .box {
  2. width: 150px;
  3. height: 350px;
  4. background-color: darkblue;
  5. border: 1px solid black;
  6. position: relative;
  7. z-index: 1;
  8. }
  9. .box:hover {
  10. z-index: 2;
  11. }


箱ts

  1. import { Component, Output, EventEmitter, HostListener } from '@angular/core';
  2. @Component({
  3. standalone: true,
  4. selector: 'app-box',
  5. templateUrl: './box.component.html',
  6. styleUrls: ['./box.component.css'],
  7. })
  8. export class BoxComponent {
  9. @Output() hover = new EventEmitter<Event>();
  10. @Output() blur = new EventEmitter<Event>();
  11. @HostListener('mouseover', ['$event.target'])
  12. onHover(e: Event) {
  13. console.log('hover');
  14. this.hover.emit(e);
  15. }
  16. @HostListener('mouseout', ['$event.target'])
  17. onBlur(e: Event) {
  18. console.log('blur');
  19. this.blur.emit(e);
  20. }
  21. }


stackblitz

展开查看全部

相关问题