【问题标题】:Angularjs ui bootstrap: how to vertical center modal component?Angularjs ui bootstrap:如何垂直居中模态组件?
【发布时间】:2016-06-12 02:50:20
【问题描述】:

最近我在学习 angularjs。我以前使用过引导程序。使用 jquery,我可以轻松更改模态组件位置的位置,使其垂直对齐。现在使用 angularjs,似乎不太容易做到这一点。这是ui bootstrap modal的plunker链接,有谁知道如何让它垂直对齐?

ui bootstrap modal component

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');
        };
    });

【问题讨论】:

    标签: angularjs angular-ui-bootstrap bootstrap-modal


    【解决方案1】:

    如果我正确理解您的问题,您只需使用 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 从你的 fork 中进行演示。

    您可以找到以下有用的链接

    1. Bootstrap 3 modal vertical position center
    2. Codepen Emaple

    希望这会有所帮助。干杯!!

    【讨论】:

    • 谢谢,这解决了我的问题,没想到纯css也能解决。
    【解决方案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"
    });
    

    【讨论】:

      【解决方案3】:

      您可以在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;  
      }
      

      【讨论】:

        【解决方案4】:

        Some Time modal-dialog-centered 类在 ng-template 中不起作用。

        在 Angular 中是模态垂直中心的简单方法。 调用 modalService.open 时,只需在侧开函数中使用 centered: true 即可。

        如下所示

        导入 NgbModal:

        import { NgbModal } from '@ng-bootstrap/ng-bootstrap';
        constructor(
          private modalService: NgbModal
         ) {}
        this.modalService.open(content, { centered: true });
        

        使用这个我希望它有效

        【讨论】:

          猜你喜欢
          • 2013-10-03
          • 2014-03-07
          • 2017-03-19
          • 1970-01-01
          • 2021-02-28
          • 2015-01-10
          • 2016-09-01
          • 1970-01-01
          • 2014-04-07
          相关资源
          最近更新 更多