asp.net 将toastr通知置于div的中心

b5buobof  于 5个月前  发布在  .NET
关注(0)|答案(6)|浏览(64)

我想让我的toastr notification显示在一个div的中间。我看到过一些建议,比如this one,但它是基于整个窗口居中,而不是在一个div内。
当toastr对表单上的元素一无所知时,是否可以将吐司通知放在表单元素的中心?
我最近的一次尝试是把吐司大致放在页面的中心,但我想把它放在某个div的中心。这可能吗?如果可能,怎么做?
这是我的中心化尝试:

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

字符串


的数据

eoigrqb6

eoigrqb61#

您可以尝试创建一个新的div,并将其放置在预期div的中心。
然后,您可以使用toastr的positionClass选项,这是您的通知弹出的位置

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

字符串

mfpqipee

mfpqipee2#

在toastr.css中,添加以下内容:

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

字符串
在JavaScript中,使用这个:

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


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

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

eqqqjvef

eqqqjvef3#

你可以用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);
}

q43xntqr

q43xntqr4#

在最新版本的ngx-toastr(^14.1.4)中有一个本机解决方案,如果你检查toast.css文件,你可以看到以下内容:

.toast-center-center {
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
 }

字符串
因此,在调用toastr通知时,您只需在.ts中使用{positionClass: 'toast-center-center' }

nlejzf6q

nlejzf6q5#

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

组件名称.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;
}

cyvaqqii

cyvaqqii6#

在css样式表中:

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

字符串
在jQuery文件中:

if (data.reminderNote !== "") {
                                toastr.options = {
                                    'debug': false,
                                    'positionClass': 'toast-top-center',
                                    'closeButton': true,
                                    'preventDuplicates': false,
                                    'showDuration': '1000',
                                    'hideDuration': '1000',
                                    'timeOut': '5000',
                                    'extendedTimeOut': '1000',
                                    'showEasing': 'swing',
                                    'hideEasing': 'linear',
                                    'showMethod': 'fadeIn',
                                    'hideMethod': 'fadeOut',
                                };
                                toastr.error(data.reminderNote);
                            }

相关问题