我有一个场景,我需要点击div
,这会给我另一个div
进行一些转换,但问题是点击的div
在转换时隐藏了div
。我需要点击的div
与转化div
一起滑动并具有相同的转换效果。以下是我的尝试:
HTML:
<div id='outerdiv' ng-controller="MyCtrl" >
<div ng-click="myValue=!myValue">RIGHT</div>
<div id="one" class='animate-hide' ng-hide="myValue">
this is just a sample div
</div>
{{myValue}}
</div>
JS:
var app = angular.module("myApp1", ["ngAnimate"]);
app.controller("MyCtrl", function ($scope) {
$scope.myValue=true;
});
CSS:
.animate-hide {
-webkit-transition:all cubic-bezier(0.250, 0.460, 0.450, 0.940) 2s;
-moz-transition:all cubic-bezier(0.250, 0.460, 0.450, 0.940) 2s;
-o-transition:all cubic-bezier(0.250, 0.460, 0.450, 0.940) 2s;
transition:all cubic-bezier(0.250, 0.460, 0.450, 0.940) 2s;
line-height:20px;
opacity:1;
padding:10px;
border:1px solid black;
background:white;
position: absolute;
left: 0;
top: 10px;
}
.animate-hide.ng-hide {
left: -100%;
opacity:0;
padding:0 10px;
}
任何帮助都将受到高度赞赏。
答案 0 :(得分:0)
将HTML修改为以下内容:
<body ng-app="myApp1">
<div id='outerdiv' ng-controller="MyCtrl" >
<div id="one" class='animate-hide' ng-hide="myValue">
this is just a sample div
</div>
<div id="main" style="width:50px" ng-click="myValue=!myValue">RIGHT
{{myValue}}
</div>
</div>
</body>
我还会为具有ng-click属性的div
添加转换持续时间。而不是将方向转换设置为left,
,而是将其更改为margin-left
。同时将float:left
添加到main
div和one
div:
.animate-hide {
-webkit-transition:all cubic-bezier(0.250, 0.460, 0.450, 0.940) 2s;
-moz-transition:all cubic-bezier(0.250, 0.460, 0.450, 0.940) 2s;
-o-transition:all cubic-bezier(0.250, 0.460, 0.450, 0.940) 2s;
transition:all cubic-bezier(0.250, 0.460, 0.450, 0.940) 2s;
line-height:20px;
opacity:1;
padding:10px;
border:1px solid black;
background:white;
margin-left:0;
}
.animate-hide.ng-hide {
margin-left:-100%;
opacity:0;
padding:0 10px;
}
#one{
float:left;
width:100px;
transition-duration:.5s;
}
#main{
float:left;
width:100px;
transition-duration:.5s;
}
注意:我还为动画所需的两个元素添加了设置宽度。
这是一个小提琴:http://jsfiddle.net/dvpdnjps/636/
另一种解决方案
如果您需要将隐藏的div保持绝对位置,则可以对JS代码进行以下更改:
var app = angular.module("myApp1", ["ngAnimate"]);
app.controller("MyCtrl", function ($scope) {
$scope.myValue = true;
$scope.changeVal = function(){
var mainDiv = document.getElementById('main');
$scope.myValue = $scope.myValue == true ? false : true;
if(!mainDiv.classList.contains('moved'))
{
mainDiv.classList.add("moved");
} else {
mainDiv.classList.remove('moved');
}
}
});
然后将其添加到您的CSS:
.moved{
margin-left:120px;
transition-duration:.5s;
padding-left:15px;
}
然后你必须确保在点击时调用changeVal函数:
<body ng-app="myApp1">
<div id='outerdiv' ng-controller="MyCtrl" >
<div id="one" class='animate-hide' ng-hide="myValue">
this is just a sample div
</div>
<div id="main" style="width:50px" ng-click="changeVal();">RIGHT
{{myValue}}
</div>
</div>
</body>
这里我正在移动“RIGHT”div以及隐藏的div。通过这种方式,您可以将隐藏的div保持绝对定位。
答案 1 :(得分:0)