我想创建一个CSS动画来移动点击按钮(实际上是它包含的整个DIV)到中心。每个按钮都在Div中,我无法弄清楚如何使用一个CSS类来获取任何按钮以移动到中心。如何使用CSS和/或Angular点击时将DIV移动到中心位置?
我已经尝试了各种各样的方式在CSS中,但尚未能够做到这一点,其他按钮不移动。
codepen是here。
如果您运行代码并单击“DIV0”按钮,它将移动到中心位置。 (再次单击该按钮重置。)当您单击其他任何按钮时,我希望它们与“DIV0”按钮所在的位置相同。
更新:我需要以编程方式设置DIV /按钮(将“divs”数组传递给HTML,以便ng-repeat构建布局)。 “divs”数组的大小可能会有所不同,因此使用此方法可自动构建布局。此外,div需要占用宽度和高度的50%,以便在屏幕上排列出2-divs的矩阵。每个div都将充满图像和文字。我试图将div的全部内容移到中心,而不仅仅是按钮。
所以我的布局可能看起来像:
DIV0 DIV1
DIV2 DIV3
或者只是:
DIV0 DIV1
DIV2
这是另一个codepen。 http://codepen.io/furnaceX/pen/BKjyEp/ 这一个获取所有的按钮到正确的位置,但居中不动画。有任何想法吗?
angular.module('myApp', [])
.controller("ctrl", ['$scope', function($scope) {
$scope.divs = [{id: 0, center: false}, {id: 1, center: false},
{id: 2, center: false}, {id: 3, center: false}];
$scope.fade = false;
$scope.go = function(div) {
if (!$scope.fade) {
div.center = true;
console.log("click again to reset");
$scope.fade = true;
} else {
div.center = false;
$scope.fade = false;
}
}
}])
html, body {
height: 100%;
width: 100%;
}
.block {
text-align: center;
width: 50%;
height: 50%;
float: left;
}
.center {
transition:0.5s linear all;
transform: translate(50%,50%);
top: 50%;
left: 50%;
//position: relative;
}
.fadeout {
transition:0.5s linear all;
opacity: 0 !important;
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp">
<div class="container" ng-controller="ctrl">
<div ng-repeat="div in divs" class="block">
<div ng-class="{center: div.center, fadeout: fade*!div.center}">
<button ng-click="go(div)">DIV{{ div.id }}</button>
</div>
</div>
</div>
</div>
退房“为中心的div更新“http://stackoverflow.com/questions/114543/horizontally-center-a-div-in-another-div-with-width-100 –