AngularJS UI Bootstrap:如何垂直居中模态框组件?

13

最近我在学习AngularJS。之前我使用过Bootstrap。在jQuery中,我可以很容易地改变模态框组件的位置,使其垂直对齐。现在在AngularJS中,似乎不太容易做到。以下是ui-bootstrap模态框的plunker链接,有人知道如何垂直对齐吗?

ui-bootstrap模态框组件

1.index.html

    <!doctype html>
    <html ng-app="ui.bootstrap.demo">
    <head>
        <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.5.0/angular.js"></script>
        <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.5.0/angular-animate.js"></script>
        <script src="//angular-ui.github.io/bootstrap/ui-bootstrap-tpls-1.2.1.js"></script>
        <script src="example.js"></script>
        <link href="//netdna.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet">
    </head>
    <body>
        <div ng-controller="ModalDemoCtrl">
            <script type="text/ng-template" id="myModalContent.html">
                <div class="modal-header">
                    <h3 class="modal-title">I'm a modal!</h3>
                </div>
                <div class="modal-body">
                    This is modal body
                </div>
                <div class="modal-footer">
                    <button class="btn btn-primary" type="button" ng-click="ok()">OK</button>
                    <button class="btn btn-warning" type="button" ng-click="cancel()">Cancel</button>
                </div>
            </script>
            <button type="button" class="btn btn-default" ng-click="open()">Open me!</button>
        </div>
    </body>
</html>

2.example.js

    angular.module('ui.bootstrap.demo', ['ngAnimate', 'ui.bootstrap']);
    angular.module('ui.bootstrap.demo').controller('ModalDemoCtrl', function($scope, $uibModal, $log) {

        $scope.items = ['item1', 'item2', 'item3'];

        $scope.animationsEnabled = true;

        $scope.open = function(size) {

            var modalInstance = $uibModal.open({
                animation: $scope.animationsEnabled,
                templateUrl: 'myModalContent.html',
                controller: 'ModalInstanceCtrl',
                size: size,
                resolve: {
                    items: function() {
                        return $scope.items;
                    }
                }
            });
        };
    });

    angular.module('ui.bootstrap.demo').controller('ModalInstanceCtrl',   function($scope, $uibModalInstance, items) {
        $scope.ok = function() {
            $uibModalInstance.close($scope.selected.item);
        };

        $scope.cancel = function() {
            $uibModalInstance.dismiss('cancel');
        };
    });
4个回答

26

如果我正确理解了您的问题,您可以通过使用CSS来实现垂直居中对齐。请添加以下CSS:

.modal {
  text-align: center;
  padding: 0!important;
}

.modal::before {
  content: '';
  display: inline-block;
  height: 100%;
  vertical-align: middle;
  margin-right: -4px;
}

.modal-dialog {
  display: inline-block;
  text-align: left;
  vertical-align: middle;
}

我已经设置了一个来自你的 Plunker ,以进行演示。

您可以找到以下链接:

  1. Bootstrap 3模态框垂直居中位置
  2. Codepen例子

希望这有所帮助。干杯!


1
谢谢,这解决了我的问题,我没想到它可以用纯CSS解决。 - ahwyX100

2
上述解决方案适用于所有模态框。如果您想应用于选择性模态框,请按照以下给出的解决方案进行操作。
CSS使用`.class-a.class-b`和`.class-a .class-b`选择器,并需要设置选项`windowClass`。
.center-modal.modal {
  text-align: center;
}

@media screen and (min-width: 768px) { 
  .center-modal.modal:before {
    display: inline-block;
    vertical-align: middle;
    content: " ";
    height: 100%;
  }
}

.center-modal .modal-dialog {
  display: inline-block;
  text-align: left;
  vertical-align: middle;
}

var modalInstance = $uibModal.open({
    templateUrl: 'modules/users/client/views/authentication/login.client.view.html',
    windowClass: "center-modal"
});

1
您可以在open方法的对象参数中使用windowTopClass属性。

https://angular-ui.github.io/bootstrap/

$uibModal.open({
  ...
  ...
  windowTopClass: "modal-center-override"
});

使用相应的 CSS 类覆盖

.modal-center-override {
  top: 50% !important;
  transform: translateY(-50%) !important;
  -webkit-transform: translateY(-50%) !important;
  -moz-transform: translateY(-50%) !important;
  -o-transform: translateY(-50%) !important;  
}

0
有时在ng-template中modal-dialog-centered类不起作用。
在Angular中,垂直居中模态框的方法很简单。 只需在调用modalService.open函数时,在open函数中使用centered: true。
像下面这样:
导入NgbModal:
import { NgbModal } from '@ng-bootstrap/ng-bootstrap';
constructor(
  private modalService: NgbModal
 ) {}
this.modalService.open(content, { centered: true });

请使用这个,希望它能正常工作。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接