javascript - Jquery/bootstrap 模态位于特定的 Angular 5 组件之上

标签 javascript jquery angular twitter-bootstrap-3

我在我的 Angular 5 项目中使用 bootstrap 模态来打开模态。但我现在这样做的方式是让模式在整个屏幕顶部打开,这是我不希望发生的。

这是我的 Angular 布局代码。

<div class="wrapper">
    <div class="sidebar" data-active-color="white" data-background-color="black" data-image="../assets/img/sidebar-1.jpg">
        <!-- <div class="sidebar" data-color="red" data-image=""> -->
        <sidebar-cmp></sidebar-cmp>
        <div class="sidebar-background" style="background-image: url(assets/img/sidebar-1.jpg)"></div>
    </div>
    <div class="main-panel">
        <navbar-cmp></navbar-cmp>
        <router-outlet></router-outlet>
        <div *ngIf="!isMap()">
            <footer-cmp></footer-cmp>
        </div>
    </div>
</div>

正如您所看到的,有 sidebar 组件和 router-outlet,模式存在于 router-outlet 组件中,但是它仍然显示在侧边栏组件的顶部。两个组件并排显示。

这就是我在 typescript 文件中显示模式的方式:

@ViewChild('DetailModal') detailRef: ElementRef;

$(this.detailRef.nativeElement);
$(this.detailRef.nativeElement).modal("show");

模态内容在组件的定义中:

<div>
   <!-- The definition of the Component that gets in the router-outlet-->

<div class=" container modal fade" #DetailModal>
    <app-detail-modal [Cert]="DCert"></app-detail-modal>
</div>

</div>

如何使模态框仅显示在其所在组件的顶部?

最佳答案

您可以通过将容器位置设置为相对位置,并将模态容器位置设置为绝对位置,使用 css 将实际模态框定位在容器内

#my-modal-container {
  height:300px;
  width: 600px;
  border: 4px solid green;
  position: relative;
}

.modal {
  position: absolute;
}

然后,您可以隐藏 Bootstrap 覆盖层并将其替换为内联覆盖层。

// Hide the default backdrop
.modal-backdrop {
   display: none;
}

// Make another backdrop with styles copied from the jQuery one, and
// place it alongside your modal component
body.modal-open .inline-modal-backdrop {
    position: absolute;
    opacity: 0.5;
    top: 0;
    right: 0;
    bottom: 0;
    left: 0;
    z-index: 1040;
    background-color: #000;
}

然后您可以将所有这些包装在一个 Angular 组件中,其中包含 bs 模态容器和新的内联背景

<div class="modal"><ng-content></ng-content></div>
<div class="inline-modal-backdrop"></div>

这是一个代码笔(没有 Angular )

https://codepen.io/anon/pen/rKvQLX

关于javascript - Jquery/bootstrap 模态位于特定的 Angular 5 组件之上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50963672/

相关文章:

angular - 我可以使用延迟加载模块创建应用程序的自定义元素吗?

javascript - 轻弹切换CSS

javascript - 去掉html注入(inject)?

c# - 从 asp.net 复选框调用 JavaScript 函数

javascript - 从 div 中完全删除元素

angular - 由 polyfills.bundle.js 发起的 API 请求

javascript - 在 fullpage.js 中使用幻灯片过渡向下滚动图像

javascript - 如何在时间戳中转换输入类型 ="date"?

javascript - 你能简化和概括这个有用的 jQuery 函数吗?

angular - 如果 Angular HTTP observable 在 n 秒内未给出响应,则在后台执行 API 调用