html 如何防止元素被剪裁?

goucqfw6  于 5个月前  发布在  其他
关注(0)|答案(1)|浏览(34)

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

box.component.html

<div class="box">
    <button class="decrease-temp" (click)="onDecreaseClick()">-</button>
    <button class="increase-temp" (click)="onIncreaseClick()">+</button>
</div>

字符串

box.component.css

.box {
  width: 150px;
  height: 350px;
  background-color: darkblue;
  border: 1px solid black;
  position: relative;
  z-index: 1;
}

.box:hover {
  z-index: 2;
}

.box:hover button {
  visibility: visible;
}

button {
  height: 30px;
  width: 30px;
  outline: none;
  border: none;
  background-color: thistle;
  border: 1px solid #000;
  visibility: hidden;
  z-index: 101;
}

button:hover {
  height: 30px;
  width: 30px;
  outline: none;
  border: none;
  background-color: rgb(253, 139, 253);
  border: 1px solid #000;
  z-index: 101;
}

.decrease-temp {
  position: absolute;
  top: 25px;
  left: -15px;
}

.increase-temp {
  position: absolute;
  top: 25px;
  left: 133px;
}

box.component.ts

import { Component, Output, EventEmitter } from '@angular/core';

@Component({
  standalone: true,
  selector: 'app-box',
  templateUrl: './box.component.html',
  styleUrls: ['./box.component.css']
})
export class BoxComponent {
  @Output() increase = new EventEmitter<void>();
  @Output() decrease = new EventEmitter<void>();

  onIncreaseClick() {
    this.increase.emit();
  }

  onDecreaseClick() {
    this.decrease.emit();
  }
}


下面是box容器的代码:

box-test.component.html

<div class="container">
    <div class="config-wrapper">
        <div class="config">
            <app-box *ngFor="let box of boxes" (increase)="addBox()" (decrease)="removeBox()"></app-box>
        </div>
    </div>
</div>

box-test.component.css

.container {
  max-width: 600px;
  height: 350px;
  overflow: auto;
}

.config-wrapper {
  display: flex;
  background-color: gray;
  overflow: hidden;
}

.config {
  position: relative;
  display: flex;
}

box-test.component.ts

import { Component } from '@angular/core';
import { BoxComponent } from '../box/box.component';
import { CommonModule } from '@angular/common';

@Component({
  standalone: true,
  imports: [ BoxComponent, CommonModule ],
  selector: 'app-box-test',
  templateUrl: './box-test.component.html',
  styleUrls: ['./box-test.component.css']
})
export class BoxTestComponent {
  boxes = [1]

  addBox() {
    this.boxes.push(this.boxes.length + 1);
  }

  removeBox() {
    this.boxes.pop();
  }
}

lvmkulzt

lvmkulzt1#

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

import { CommonModule } from '@angular/common';
import { Component, ElementRef, ViewChild } from '@angular/core';
import { bootstrapApplication } from '@angular/platform-browser';
import { Subject } from 'rxjs';
import { debounceTime } from 'rxjs/operators';
import 'zone.js';
import { BoxComponent } from './box/box.component';

@Component({
  selector: 'app-root',
  standalone: true,
  imports: [BoxComponent, CommonModule],
  template: `
    <div class="container">
        <div class="config-wrapper">
            <div class="config">
                <app-box *ngFor="let box of boxes;let index = index" (hover)="onHover($event, index)" (blur)="onBlur($event)"></app-box>
            </div>
        </div>
        <div class="button-wrapper" [hidden]="!showButtons" #buttons
  (mouseover)="onMouseOverButtons($event)" 
  (mouseout)="onMouseOutButtons($event)" [style.left]="position * 150 + 'px'">
          <button class="decrease-temp" (click)="removeBox()">-</button>
          <button class="increase-temp" (click)="addBox()">+</button>
        </div>
    </div>
    {{buttonsHover}} || {{position}}
  `,
  styles: [
    `
  .container {
  max-width: 600px;
  height: 352px;
  overflow: visible;
  position: relative;
}

.config-wrapper {
  display: flex;
  background-color: gray;
  overflow: hidden;
}

.config {
  position: relative;
  display: flex;
}
.button-wrapper {
  width: 150px;
  height:50px;
  position: absolute;
  z-index: 5;
  top:30px;
  left: 0px;
}
/* 
.box:hover button {
  visibility: visible;
} */

button {
  height: 30px;
  width: 30px;
  outline: none;
  border: none;
  background-color: thistle;
  border: 1px solid #000;
  z-index: 101;
  cursor: pointer;
}

button:hover {
  height: 30px;
  width: 30px;
  outline: none;
  border: none;
  background-color: rgb(253, 139, 253);
  border: 1px solid #000;
  z-index: 101;
}

.decrease-temp {
  position: absolute;
  top: 25px;
  left: -15px;
}

.increase-temp {
  position: absolute;
  top: 25px;
  left: 133px;
}

  `,
  ],
})
export class App {
  @ViewChild('buttons') buttons: ElementRef<any> | null = null;
  boxes = [1];
  showButtons = false;
  buttonsHover = false;
  position = 0;
  subject = new Subject<boolean>();

  ngOnInit() {
    this.subject.pipe(debounceTime(500)).subscribe((data: boolean) => {
      if (!this.buttonsHover) {
        this.showButtons = data;
      }
    });
  }

  onHover(event: Event, index: number) {
    console.log(event);
    this.position = index;
    //if (event.target)
    this.subject.next(true);
  }

  onBlur(event: Event) {
    console.log(event);
    this.subject.next(false);
  }

  onMouseOverButtons(event: Event) {
    console.log(event);
    //if (event.target)
    this.buttonsHover = true;
    this.subject.next(false);
  }

  onMouseOutButtons(event: Event) {
    console.log(event);
    this.buttonsHover = false;
    this.subject.next(false);
  }

  addBox() {
    this.boxes.push(this.boxes.length + 1);
  }

  removeBox() {
    this.boxes.pop();
  }
}

bootstrapApplication(App);

字符串
框html

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


Box CSS

.box {
  width: 150px;
  height: 350px;
  background-color: darkblue;
  border: 1px solid black;
  position: relative;
  z-index: 1;
}

.box:hover {
  z-index: 2;
}


箱ts

import { Component, Output, EventEmitter, HostListener } from '@angular/core';

@Component({
  standalone: true,
  selector: 'app-box',
  templateUrl: './box.component.html',
  styleUrls: ['./box.component.css'],
})
export class BoxComponent {
  @Output() hover = new EventEmitter<Event>();
  @Output() blur = new EventEmitter<Event>();

  @HostListener('mouseover', ['$event.target'])
  onHover(e: Event) {
    console.log('hover');
    this.hover.emit(e);
  }

  @HostListener('mouseout', ['$event.target'])
  onBlur(e: Event) {
    console.log('blur');
    this.blur.emit(e);
  }
}


stackblitz

相关问题