![](/img/trans.png)
[英]How to get element's height and then use it to set another element's height
[英]Get height of div element in angularjs and set it to another element's attribute
是否可以使用angularjs獲取元素的高度並將其放置到另一個div的屬性中?
我有一個帶有詞綴側邊欄的引導網頁,為了使其能夠在網頁頁腳之前正確停止,我必須在詞綴屬性中指定頁腳的高度。 目前,我正在手動執行此操作。
它通常看起來像這樣:
查看1:
<div class="affix-top" data-spy="affix" data-offset-bottom="300">
...
</div>
布局(主)視圖:
...
<div id="footer">
...
</div>
300是手動輸入的頁腳高度,我想將“視圖1”更改為以下形式(偽代碼):
<div class="affix-top" data-spy="affix" data-offset-bottom="{{#footer.height()}}">
...
</div>
這可能嗎? 如果在angularjs中不是那么容易,那么在可能的情況下,如果不使用單獨的javascript,我該如何使用呢?
將角度范圍內的變量初始化為默認值; 可用時將其填充。
$scope.height = 0;
$scope.updateHeight = function(){
$scope.height = document.querySelector("#wantedDiv").clientHeight;
console.log("height is now: "+$scope.height);
if(!$scope.$$phase) $scope.$apply();
};
window.onload= $scope.updateHeight;
在您的html中使用:
<div class="affix-top" data-spy="affix" data-offset-bottom="{{height}}">
...
</div>
同樣,您可以將此分配給您想要的任何其他事件。
window.onresize= $scope.updateHeight;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.