Angular Material 修复了工具栏和粘性页脚
Angular Material fixed toolbar AND sticky footer
一段时间以来,我一直在为这个问题苦思冥想,并想出了一个解决方案。我想要一个固定的工具栏(导航栏)以及一个粘性(浮动)页脚。页脚应浮动在主要部分的底部,但在没有内容时粘在底部。看来我可以做其中之一,但不能同时做。使用这种方法,工具栏是固定的,但页脚不是粘性的。当主要部分为空时,它会紧贴工具栏。
<body ng-controller="MainCtrl" layout="row">
<div layout="column" flex>
<md-toolbar class="md-medium-tall">
<div class="md-toolbar-tools">
<span>HEADER</span>
<span flex></span>
<md-button class="md-raised" ng-click="toggleContent(!displayContent)">onOff</md-button>
<span flex></span>
<md-button class="md-raised" ng-click="toggleNum()">half/full</md-button>
</div>
</md-toolbar>
<md-content>
<div layout="column" flex>
<div ng-if="displayContent" style="background-color:SteelBlue;color:white;" ng-repeat="card in cards|limitTo: displayLim">body {{card.title}}</div>
<div style="background-color: red;" flex></div>
<div style="background-color:orange;color:white;" >footer item</div>
</div>
</md-content>
</div>
</body>
下面的代码用作粘性页脚,但随后工具栏也会滚动。
<body ng-controller="MainCtrl" layout="row">
<div layout="column" flex>
<md-toolbar class="md-medium-tall">
<div class="md-toolbar-tools">
<span>HEADER</span>
<span flex></span>
<md-button class="md-raised" ng-click="toggleContent(!displayContent)">onOff</md-button>
<span flex></span>
<md-button class="md-raised" ng-click="toggleNum()">half/full</md-button>
</div>
</md-toolbar>
<div layout="column" flex>
<div ng-if="displayContent" style="background-color:SteelBlue;color:white;" ng-repeat="card in cards|limitTo: displayLim">body {{card.title}}</div>
<div style="background-color: red;" flex></div>
<div style="background-color:orange;color:white;" >footer item</div>
</div>
</div>
</body>
这似乎是完成我想要做的事情的正确灵活方式,但我无法做到完美。
除了这种方法,我还使用了一种更传统的方法,即使用从 calc(100vh - header - footer)
计算的主要部分高度来实现粘性页脚。当 BAM.. angular-material 决定让他们的工具栏大小随视口大小而变化时,我几乎想通了。我可能会提出更改请求,以便我可以在 md-content
部分中使用空白填充 <div flex></div>
,但我想先看看是否有人有更好的解决方案。
也许这段代码可以提供帮助:
angular
.module('myApp', ['ngMaterial'])
.controller('MainCtrl', function($scope) {
console.log('MainCtrl');
$scope.cards = [{
text: 'Bla bla bla bla bla bla bla ',
title: 'Bla'
}, {
text: 'Bla bla bla bla bla bla bla ',
title: 'Bla'
}, {
text: 'Bla bla bla bla bla bla bla ',
title: 'Bla'
}, {
text: 'Bla bla bla bla bla bla bla ',
title: 'Bla'
}, {
text: 'Bla bla bla bla bla bla bla ',
title: 'Bla'
}, {
text: 'Bla bla bla bla bla bla bla ',
title: 'Bla'
}, {
text: 'Bla bla bla bla bla bla bla ',
title: 'Bla'
}, {
text: 'Bla bla bla bla bla bla bla ',
title: 'Bla'
}, {
text: 'Bla bla bla bla bla bla bla ',
title: 'Bla'
}, {
text: 'Bla bla bla bla bla bla bla ',
title: 'Bla'
}, {
text: 'Bla bla bla bla bla bla bla ',
title: 'Bla'
}];
$scope.displayContent = true;
$scope.displayLim = 100;
$scope.toggleContent = function(showContent) {
$scope.displayContent = showContent;
};
});
<head>
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.3.2/angular.min.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.3.2/angular-animate.min.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.3.2/angular-aria.min.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/angular_material/0.8.3/angular-material.min.js"></script>
<link rel="stylesheet" href="//ajax.googleapis.com/ajax/libs/angular_material/0.8.3/angular-material.min.css">
</head>
<body ng-app="myApp" ng-controller="MainCtrl" layout="row">
<div layout="column" flex>
<md-toolbar class="md-medium-tall">
<div class="md-toolbar-tools">
<span>HEADER</span>
<span flex></span>
<md-button class="md-raised" ng-click="toggleContent(!displayContent)">onOff</md-button>
<span flex></span>
<md-button class="md-raised" ng-click="toggleNum()">half/full</md-button>
</div>
</md-toolbar>
<md-content layout="row" flex>
<div layout="column" flex>
<div ng-if="displayContent" style="background-color:SteelBlue;color:white;" ng-repeat="card in cards|limitTo: displayLim">body {{card.title}}</div>
<div style="background-color: red;" flex></div>
</div>
</md-content>
<div layout="row" class="footer" layout-align="center center">
<h2>My Footer</h2>
</div>
</div>
</body>
您应该使用 md-content
作为滚动包装,将您的内容放入 flex
,将页脚放入 flex="none"
。它将始终粘在 md-content
容器的底部,因为它有一个 CSS overflow: auto
。 angular-material layout children
<md-toolbar class="md-medium-tall">
<div class="md-toolbar-tools">
<span>HEADER</span>
<span flex></span>
<md-button class="md-raised" ng-click="toggleContent(!displayContent)">onOff</md-button>
<span flex></span>
<md-button class="md-raised" ng-click="toggleNum()">half/full</md-button>
</div>
</md-toolbar>
<md-content layout="column" flex>
<div flex layout="column">
<div ng-if="displayContent" style="background-color:SteelBlue;color:white;" ng-repeat="card in cards|limitTo: displayLim">body {{card.title}}</div>
</div>
<footer flex="none" style="background-color:orange;color:white;">
<div>footer item</div>
</footer>
</md-content>
我终于明白问题出在哪里了。在 md-content
的主要内容部分下嵌套 divs 时,Safari 出现问题。我通过将 flex="none"
添加到顶层 div.
来修复它
这仅适用于 Chrome:
<md-content layout="column" flex>
<div flex layout="column">
<section>
<div ng-if="displayContent" style="min-height:20px;background-color:SteelBlue;color:white;" ng-repeat="card in cards|limitTo: displayLim track by $index">{{card.title}}
</div>
</section>
<div flex></div>
<footer flex="none" style="background-color:orange;color:white;">
<div>footer item</div>
</footer>
</div>
</md-content>
这适用于 Chrome 和 Safari:
<md-content layout="column" flex>
<div flex layout="column">
<section flex="none">
<div ng-if="displayContent" style="min-height:20px;background-color:SteelBlue;color:white;" ng-repeat="card in cards|limitTo: displayLim track by $index">{{card.title}}
</div>
</section>
<div flex></div>
<footer flex="none" style="background-color:orange;color:white;">
<div>footer item</div>
</footer>
</div>
</md-content>
一段时间以来,我一直在为这个问题苦思冥想,并想出了一个解决方案。我想要一个固定的工具栏(导航栏)以及一个粘性(浮动)页脚。页脚应浮动在主要部分的底部,但在没有内容时粘在底部。看来我可以做其中之一,但不能同时做。使用这种方法,工具栏是固定的,但页脚不是粘性的。当主要部分为空时,它会紧贴工具栏。
<body ng-controller="MainCtrl" layout="row">
<div layout="column" flex>
<md-toolbar class="md-medium-tall">
<div class="md-toolbar-tools">
<span>HEADER</span>
<span flex></span>
<md-button class="md-raised" ng-click="toggleContent(!displayContent)">onOff</md-button>
<span flex></span>
<md-button class="md-raised" ng-click="toggleNum()">half/full</md-button>
</div>
</md-toolbar>
<md-content>
<div layout="column" flex>
<div ng-if="displayContent" style="background-color:SteelBlue;color:white;" ng-repeat="card in cards|limitTo: displayLim">body {{card.title}}</div>
<div style="background-color: red;" flex></div>
<div style="background-color:orange;color:white;" >footer item</div>
</div>
</md-content>
</div>
</body>
下面的代码用作粘性页脚,但随后工具栏也会滚动。
<body ng-controller="MainCtrl" layout="row">
<div layout="column" flex>
<md-toolbar class="md-medium-tall">
<div class="md-toolbar-tools">
<span>HEADER</span>
<span flex></span>
<md-button class="md-raised" ng-click="toggleContent(!displayContent)">onOff</md-button>
<span flex></span>
<md-button class="md-raised" ng-click="toggleNum()">half/full</md-button>
</div>
</md-toolbar>
<div layout="column" flex>
<div ng-if="displayContent" style="background-color:SteelBlue;color:white;" ng-repeat="card in cards|limitTo: displayLim">body {{card.title}}</div>
<div style="background-color: red;" flex></div>
<div style="background-color:orange;color:white;" >footer item</div>
</div>
</div>
</body>
这似乎是完成我想要做的事情的正确灵活方式,但我无法做到完美。
除了这种方法,我还使用了一种更传统的方法,即使用从 calc(100vh - header - footer)
计算的主要部分高度来实现粘性页脚。当 BAM.. angular-material 决定让他们的工具栏大小随视口大小而变化时,我几乎想通了。我可能会提出更改请求,以便我可以在 md-content
部分中使用空白填充 <div flex></div>
,但我想先看看是否有人有更好的解决方案。
也许这段代码可以提供帮助:
angular
.module('myApp', ['ngMaterial'])
.controller('MainCtrl', function($scope) {
console.log('MainCtrl');
$scope.cards = [{
text: 'Bla bla bla bla bla bla bla ',
title: 'Bla'
}, {
text: 'Bla bla bla bla bla bla bla ',
title: 'Bla'
}, {
text: 'Bla bla bla bla bla bla bla ',
title: 'Bla'
}, {
text: 'Bla bla bla bla bla bla bla ',
title: 'Bla'
}, {
text: 'Bla bla bla bla bla bla bla ',
title: 'Bla'
}, {
text: 'Bla bla bla bla bla bla bla ',
title: 'Bla'
}, {
text: 'Bla bla bla bla bla bla bla ',
title: 'Bla'
}, {
text: 'Bla bla bla bla bla bla bla ',
title: 'Bla'
}, {
text: 'Bla bla bla bla bla bla bla ',
title: 'Bla'
}, {
text: 'Bla bla bla bla bla bla bla ',
title: 'Bla'
}, {
text: 'Bla bla bla bla bla bla bla ',
title: 'Bla'
}];
$scope.displayContent = true;
$scope.displayLim = 100;
$scope.toggleContent = function(showContent) {
$scope.displayContent = showContent;
};
});
<head>
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.3.2/angular.min.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.3.2/angular-animate.min.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.3.2/angular-aria.min.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/angular_material/0.8.3/angular-material.min.js"></script>
<link rel="stylesheet" href="//ajax.googleapis.com/ajax/libs/angular_material/0.8.3/angular-material.min.css">
</head>
<body ng-app="myApp" ng-controller="MainCtrl" layout="row">
<div layout="column" flex>
<md-toolbar class="md-medium-tall">
<div class="md-toolbar-tools">
<span>HEADER</span>
<span flex></span>
<md-button class="md-raised" ng-click="toggleContent(!displayContent)">onOff</md-button>
<span flex></span>
<md-button class="md-raised" ng-click="toggleNum()">half/full</md-button>
</div>
</md-toolbar>
<md-content layout="row" flex>
<div layout="column" flex>
<div ng-if="displayContent" style="background-color:SteelBlue;color:white;" ng-repeat="card in cards|limitTo: displayLim">body {{card.title}}</div>
<div style="background-color: red;" flex></div>
</div>
</md-content>
<div layout="row" class="footer" layout-align="center center">
<h2>My Footer</h2>
</div>
</div>
</body>
您应该使用 md-content
作为滚动包装,将您的内容放入 flex
,将页脚放入 flex="none"
。它将始终粘在 md-content
容器的底部,因为它有一个 CSS overflow: auto
。 angular-material layout children
<md-toolbar class="md-medium-tall">
<div class="md-toolbar-tools">
<span>HEADER</span>
<span flex></span>
<md-button class="md-raised" ng-click="toggleContent(!displayContent)">onOff</md-button>
<span flex></span>
<md-button class="md-raised" ng-click="toggleNum()">half/full</md-button>
</div>
</md-toolbar>
<md-content layout="column" flex>
<div flex layout="column">
<div ng-if="displayContent" style="background-color:SteelBlue;color:white;" ng-repeat="card in cards|limitTo: displayLim">body {{card.title}}</div>
</div>
<footer flex="none" style="background-color:orange;color:white;">
<div>footer item</div>
</footer>
</md-content>
我终于明白问题出在哪里了。在 md-content
的主要内容部分下嵌套 divs 时,Safari 出现问题。我通过将 flex="none"
添加到顶层 div.
这仅适用于 Chrome:
<md-content layout="column" flex>
<div flex layout="column">
<section>
<div ng-if="displayContent" style="min-height:20px;background-color:SteelBlue;color:white;" ng-repeat="card in cards|limitTo: displayLim track by $index">{{card.title}}
</div>
</section>
<div flex></div>
<footer flex="none" style="background-color:orange;color:white;">
<div>footer item</div>
</footer>
</div>
</md-content>
这适用于 Chrome 和 Safari:
<md-content layout="column" flex>
<div flex layout="column">
<section flex="none">
<div ng-if="displayContent" style="min-height:20px;background-color:SteelBlue;color:white;" ng-repeat="card in cards|limitTo: displayLim track by $index">{{card.title}}
</div>
</section>
<div flex></div>
<footer flex="none" style="background-color:orange;color:white;">
<div>footer item</div>
</footer>
</div>
</md-content>