Angularjs ui bootstrap:如何垂直居中模态组件? [英] Angularjs ui bootstrap: how to vertical center modal component?

查看:23
本文介绍了Angularjs ui bootstrap:如何垂直居中模态组件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

ui 引导模态组件

1.index.html

 <html ng-app="ui.bootstrap.demo"><头><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="样式表"><身体><div ng-controller="ModalDemoCtrl"><script type="text/ng-template" id="myModalContent.html"><div class="modal-header"><h3 class="modal-title">我是模态!</h3>

<div class="modal-body">这是模态体

<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()">取消</button>

<button type="button" class="btn btn-default" ng-click="open()">打开我!</button>

</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 = 函数(大小){var modalInstance = $uibModal.open({动画:$scope.animationsEnabled,templateUrl: 'myModalContent.html',控制器:'ModalInstanceCtrl',尺寸:尺寸,解决: {项目:函数(){返回 $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');};});

解决方案

如果我正确理解您的问题,您只需使用 CSS 即可实现垂直居中对齐.添加以下 CSS:

.modal {文本对齐:居中;填充:0!重要;}.modal::before {内容: '';显示:内联块;高度:100%;垂直对齐:中间;边距右:-4px;}.modal-dialog {显示:内联块;文本对齐:左;垂直对齐:中间;}

我已经设置了一个 Plunker 从你的分叉出来进行演示.

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

  1. Bootstrap 3 模态垂直位置中心
  2. Codepen Emaple

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

Recently I am learning angularjs. I have used bootstrap before. With jquery, I can easily change the position of the modal component position to make it vertical align. Now with angularjs, it seems not very easy to do that. Here is a plunker link of ui bootstrap modal, Does anyone know how to make it vertical align?

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

解决方案

If I understand your problem correctly, you can acheive the vertical center alignment just by using CSS. Add following 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;
}

I have setup a Plunker forked from yours to make a demonstration.

You can find following links Helpful

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

Hope this helps. Cheers !!

这篇关于Angularjs ui bootstrap:如何垂直居中模态组件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
其他开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆