Angularjs ui bootstrap: 如何垂直居中模态组件?
Angularjs ui bootstrap: how to vertical center modal component?
最近在学习angularjs。我以前用过bootstrap。使用 jquery,我可以轻松更改模态组件位置,使其垂直对齐。现在有了angularjs,要做到这一点似乎不太容易。这是ui bootstrap模态的plunker link,有谁知道如何使它垂直对齐?
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');
};
});
如果我对你的问题理解正确,你可以通过使用 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 来进行演示。
您可以找到以下有用的链接
- Bootstrap 3 modal vertical position center
- Codepen Emaple
希望这对您有所帮助。干杯!!
以上解决方案将适用于所有模态框。如果您想应用于选择性模态,请遵循以下给出的解决方案。
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"
});
您可以在 open
方法的对象参数中使用 windowTopClass
属性。
https://angular-ui.github.io/bootstrap/
$uibModal.open({
...
...
windowTopClass: "modal-center-override"
});
与相应的 CSS class 覆盖
.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;
}
有时以模态对话框为中心 class 在 ng-template 中不工作。
在Angular中是模态垂直居中的简单方法。
调用 modalService.open.
时,只需在 side open 函数中使用 centered: true
点赞
导入 NgbModal :
import { NgbModal } from '@ng-bootstrap/ng-bootstrap';
constructor(
private modalService: NgbModal
) {}
this.modalService.open(content, { centered: true });
使用这个我希望它有效
最近在学习angularjs。我以前用过bootstrap。使用 jquery,我可以轻松更改模态组件位置,使其垂直对齐。现在有了angularjs,要做到这一点似乎不太容易。这是ui bootstrap模态的plunker link,有谁知道如何使它垂直对齐?
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');
};
});
如果我对你的问题理解正确,你可以通过使用 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 来进行演示。
您可以找到以下有用的链接
- Bootstrap 3 modal vertical position center
- Codepen Emaple
希望这对您有所帮助。干杯!!
以上解决方案将适用于所有模态框。如果您想应用于选择性模态,请遵循以下给出的解决方案。
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"
});
您可以在 open
方法的对象参数中使用 windowTopClass
属性。
https://angular-ui.github.io/bootstrap/
$uibModal.open({
...
...
windowTopClass: "modal-center-override"
});
与相应的 CSS class 覆盖
.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;
}
有时以模态对话框为中心 class 在 ng-template 中不工作。
在Angular中是模态垂直居中的简单方法。 调用 modalService.open.
时,只需在 side open 函数中使用 centered: true点赞
导入 NgbModal :
import { NgbModal } from '@ng-bootstrap/ng-bootstrap';
constructor(
private modalService: NgbModal
) {}
this.modalService.open(content, { centered: true });
使用这个我希望它有效