我想創建一個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 –