跨 Angular 组件相对于父级父级的 CSS 定位

标签 css angular sass css-position positioning

目前我正在开发图片库和图片上传工具。在下图中,展示了我当前和我想要的画廊状态。用户可以单击缩略图并以完整尺寸查看当前缩略图行下方的大图片,相对于组件高度插入下一行。

enter image description here


images.component.html

<div class="container">
  <div class="image-box" *ngFor="let img of images$ | async">
    <img class="mat-elevation-z1" [src]="environment.img + '/images/thmb/' + img.id + '.jpg'" (click)="selected = img">
    <app-image-details *ngIf="selected?.id === img.id" [img]="img"></app-image-details>
  </div>
</div>

images.component.scss

theme-flexfix.scss 在使用 flex 时平均固定所有图像宽度。 flex-container-wrap-items() 使容器 flex 和 flex-wrap。 .image-box 元素是 flex: 1.

@import 'src/theme-flexfix.scss';

.container {
  @include flex-container-wrap-items(128px);
}

.container > .image-box {
  @include flex-wrap-fix(128px);
  padding: 4px;
  box-sizing: border-box;
  /* position: relative; */
}

img {
  width: 100%;
}

image-details.component.html

<img [src]="environment.img + '/images/' + img.id + '.jpg'">
<p>
  Lorem ipsum
</p>

image-details.component.scss

:host {
  /* position: absolute */
  /* left: 0 */
}

尝试了注释样式以得到我想要的结果,但没有推送其他内容。我认为有一个简单的解决方案,但目前我还做不到。

感谢您的帮助。

最佳答案

我假设您正在尝试复制 Google 图片搜索。

我会这样去做:

  1. 点击图片时,计算您在一行中显示了多少张图片。
  2. 在行中的最后一张图片之后插入带有较大版本图片的 DIV。

想法是始终在当前行结束后注入(inject) DIV。

这样,您的 DIV 将不会被绝对定位,而是会随 HTML 一起流动,并且不需要绝对定位黑客攻击。

关于跨 Angular 组件相对于父级父级的 CSS 定位,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48875748/

相关文章:

css - 平移背景图像动画

html - 单击导航菜单时滚动页面视差(视差滚动)

angular - 我收到错误 : Type 'Subscription' is not assignable to type 'Observable'

angular - 安装angular2 RC4路由器时找不到兼容版本: @angular/router@^2. 0.0-rc.4错误

html - 根据内容维护li宽度

java - 如何更改 RadioButton 标签的位置?

angularjs - Angular 2 Parsley 在输入无效时阻止表单提交

html - 重叠位置元素后 div 中的空白

css - 如何避免 SCSS/SASS 中的重复条目?

javascript - 有没有办法在 react 中使用 JS 动态更新 scss 变量?