div中的toastr通知

时间:2013-04-06 15:12:32

标签: asp.net css toastr

我试图让我的toastr notification出现在div的中间。我已经看到了一些建议,比如this one,但它是基于整个窗口的中心,而不是div。

当toastr对表单上的元素一无所知时,是否可以在表单元素中居中toast通知?

我最近的尝试是将页面中的吐司大致居中,但我想将其置于某个div中。那可能吗?如果是这样,怎么样?

这是我的中心尝试:

.toast-center {
    top: 50%;
    left: 40%;
}

enter image description here

4 个答案:

答案 0 :(得分:3)

您可以尝试创建一个新div并将其放置在预期div的中心。

然后你可以使用toastr的positionClass选项,这是弹出通知的位置

toastr.options: {
    "positionClass": "your-newly-created-div-class"
}

答案 1 :(得分:1)

在toastr.css中,添加:

.toast-top-center {
    top: 12px;
    left:50%;
    margin:0 0 0 -150px;
}

在JavaScript中,请使用:

toastr.options = {
    positionClass: 'toast-top-center'
};

如果你的toast div有其他宽度,你可以将上面的CSS修改为它的半宽。

margin:0 0 0 (here caculate the -width/2 px for yourself)

答案 2 :(得分:0)

你可以用jquery

来做

在toastr.css中,添加:

.toast-top-center { 
   top: 12px;   
   margin: 0 auto;  
   left: 50%;   
   margin-left: -150px;
 }

在js文件中添加以下内容:

toastr.options = {
    positionClass: 'toast-top-center'
};

toastr.success("Your Message Header", "Your Message");

var $notifyContainer = $('#toast-container').closest('.toast-top-center');
if ($notifyContainer) {
   // align center
   var windowHeight = $(window).height() - 90;
   $notifyContainer.css("margin-top", windowHeight / 2);
}

答案 3 :(得分:0)

以下代码段在 angular ~9.1.0 中使用 "ngx-toastr": "^12.1.0" 进行了测试。

component-name.ts

import { ToastrService } from 'ngx-toastr';

@Component({
  selector: 'app-component-name',
  templateUrl: './component-name.component.html',
  styleUrls: ['./component-name.component.scss']
})
export class ComponentName implements OnInit {
 
    constructor(private readonly toastrService: ToastrService) { }
    
      ngOnInit(): void {
        this.toastrService.toastrConfig.positionClass = 'toast-top-center';
      }
}

组件名称.scss

.toast-top-center { 
    top: 12px;   
    margin: 0 auto;  
    left: 50%;   
    margin-left: -150px;
}