[英]Check if window height is greater than div#content height in AngularJS
I'd like to add css styles (position: fixed) to footer only if window.height is greater than height of div with main content. 我只想在window.height大于主要内容的div高度时,将css样式(位置:固定)添加到页脚中。
In my solution (below) the condition is always true, so it dosn't work as I expect. 在我的解决方案中(以下),条件始终为true,因此它无法按我预期的那样工作。 Moreover I'm not sure if I used $scope.$watch in right way to control window height - I don't want to press f5 every time when I change page (eg. form home page to contact page) to refresh scope and apply additional styles. 此外,我不确定是否以正确的方式使用$ scope。$ watch来控制窗口高度-我不想每次更改页面(例如,从首页到联系页面)以刷新范围和应用其他样式。
I've found similar topics (eg. Forcing footer to bottom of page, if document height is smaller than window height ) but nothing for AngularJS 我发现了类似的主题(例如,如果文档高度小于窗口高度,则将页脚强制到页面底部 ),但对于AngularJS而言则一无所有
I'm using AngularJS 1.6. 我正在使用AngularJS 1.6。 This is my code: 这是我的代码:
controllersFooter.controller( 'footer' , [ '$scope' , '$window' , function( $scope , $window ){
var $footer = angular.element(document.querySelector('#site-footer'));
$scope.windowHeight = jQuery( window ).height();
$window.onload = function() {
$scope.$watch(function(){
var contentHeight = document.getElementById('content-container').scrollHeight;
return contentHeight;
}, function(value){
var contentHeight = value;
if ( contentHeight < $scope.windowHeight ) {
$footer.css(
{
"position":"fixed",
"bottom":0,
"left": 0,
"right": 0
}
);
}
});
}; }]);
您可以在页脚中将ng-class与作用域变量一起使用,并根据页面的高度将其设置为true或false。有关ng-class的更多信息https://docs.angularjs.org/api/ng/directive/ngClass
Please make sure that the document body has a scroll. 请确保文档正文具有滚动条。
Just it can be any other div with overflow: auto
... you expect that it will be document.body, but that is not always true 只是它可以是任何其他具有overflow: auto
div overflow: auto
...您希望它将是document.body,但这并不总是正确的
I advice you to just subscribe to scroll event on element with scroll-bar, like: 我建议您只使用滚动条订阅元素上的滚动事件,例如:
jQuery( elementWithScrollBar ).scroll(function() {
$scope.fixed = calculateIfFooterIsFixed();
$scope.$digest(); // run angular digest cycle to reflect scope changes to DOM,
// most likely you will need it
});
Event on windows resize is available natively in angularjs. Windows调整大小上的事件可在angularjs中本地获得。
angular.element($window).on('resize', this.onResize);
In your case for example sth like that: 以您的情况为例:
var footerHeight = document.getElementById('side-footer').scrollHeight;
this.onResize = function() {
var contentHeight = document.getElementById('content-container').scrollHeight;
$scope.$apply(function() {
$scope.isFixed = contentHeight > $window.innerHeight - footerHeight
});
}
and use ng-class: 并使用ng-class:
<div id="side-footer" ng-class="{'fixed': isFixed}">
Position "fixed" when main content is smaller than visible window: https://jsfiddle.net/UncleGoogle/jpy4zse9/25/ (for some reason need to run twice to set fiddle footer size properly) 当主要内容小于可见窗口时,将位置“固定”: https : //jsfiddle.net/UncleGoogle/jpy4zse9/25/ (出于某种原因,需要运行两次以正确设置小提琴页脚的大小)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.