gpt4 book ai didi

css - 使用 AngularJS 动画动态高度

转载 作者:技术小花猫 更新时间:2023-10-29 11:51:55 25 4
gpt4 key购买 nike

所以,我一直在尝试制作类似于 Angular Accordion 的动画,但没有成功。我想出了固定高度,但不是动态的。 height: auto; 不起作用。 :(

也许你们当中有人遇到过类似的问题?

我的代码:

html:

  <head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.15/angular.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.3.15/angular-route.js"></script>
<link rel="stylesheet" href="style.css">
<script src="script.js"></script>
</head>

<body>
<section ng-app="myApp">
<div ng-controller="myCtrl as vm">
<ul ng-init="vm.tab=1">
<li ng-repeat="item in vm.data">
<a href ng-click="vm.tab = item.thingy">{{item.name}}</a>
<div ng-show="vm.tab === item.thingy">
<img ng-src="{{item.img}}" width="50px"><br>
<div class="longDiv">{{item.description}}</div>
</div>
</li>
</ul>
</div>
</section>
</body>
</html>

js:

var app = angular.module('myApp', []);

app.controller('myCtrl', ['$scope',
function($scope) {
var vm = this;

vm.data = [{
name: "First",
title: "oneTitle",
description: "Lorem ipsum dolor sit amet, consectetur adipiscing elit ipsum dolor sit amet, consectetur ipsum dolor sit amet, consectetur.",
year: "2013",
img: "http://static.hdw.eweb4.com/media/wp_400/1/5/42735.jpg",
thingy: 1
}, {
name: "third",
title: "twoTitle",
description: "Quisque pulvinar libero sed eros ornare",
year: "2014",
img: "http://static.hdw.eweb4.com/media/wp_400/1/1/8519.jpg",
thingy: 2
}, {
name: "Second",
title: "threeTitle",
description: "Cras accumsan ipsum dolor sit amet, consectetur ipsum dolor sit amet, consectetur ipsum dolor sit amet, consectetur massa vitae tortor vehicula .",
year: "2015",
img: "http://static.hdw.eweb4.com/media/wp_400/1/5/43326.jpg",
thingy: 3
}, {
name: "fourth",
title: "FourTitle",
description: "Suspendisse ipsum dolor sit amet, consectetur ipsum dolor sit amet, consectetur ipsum dolor sit amet, consectetur ipsum dolor sit amet, consectetur ipsum dolor sit amet, consectetur ipsum dolor sit amet, consectetur ipsum dolor sit amet, consectetur ipsum dolor sit amet, consectetur vitae mattis magna.",
year: "2011",
img: "http://static.hdw.eweb4.com/media/wp_400/1/5/42413.jpg",
thingy: 4
}];

}
]);

提前致谢!求助 here

最佳答案

我通过创建“折叠”指令解决了这个问题:

ngModule.directive('collapse', [function () {
return {
restrict: 'A',

link: function ($scope, ngElement, attributes) {
var element = ngElement[0];

$scope.$watch(attributes.collapse, function (collapse) {
var newHeight = collapse ? 0 : getElementAutoHeight();

element.style.height = newHeight +"px";
ngElement.toggleClass('collapsed', collapse);
});

function getElementAutoHeight() {
var currentHeight = getElementCurrentHeight();

element.style.height = 'auto';
var autoHeight = getElementCurrentHeight();

element.style.height = currentHeight +"px";
getElementCurrentHeight(); // Force the browser to recalc height after moving it back to normal

return autoHeight;
}

function getElementCurrentHeight() {
return element.offsetHeight
}
}
};
}]);

要使用该指令,您只需将它放在您的元素上并将其设置为表示是否折叠它的范围变量:

<div collapse="isCollapsed">
This will be collapsed
</div>

现在您只需在高度上应用 CSS 过渡,它就会平滑地折叠和展开。由于该指令还添加了一个类,因此您可以应用其他过渡效果,例如不透明度。

这是一个带有示例的 Codepen:http://codepen.io/KaidenR/pen/GoRJLx

关于css - 使用 AngularJS 动画动态高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31026206/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com