如何使页脚位于页面底部和AngularJS中的内容下方?

时间:2018-09-10 12:27:47

标签: css angularjs footer

我试图通过各种方法使页脚位于某些内容之下,但似乎解决方案在AngularJS的动态内容方面表现不佳。

  • 当页面大部分为空白时,我希望页脚位于页面底部

  • 页面扩展时(或总体上来说只是很大),我希望页脚被推到内容下方(对页面本身不粘

    • 重定向页面时应遵循相同的逻辑

这是我制作的一个小演示,用来说明我的尝试。我尝试将position: absolute;用于页脚,但是一旦内容扩展,我不知道如何更改(或使用替代项)。

Plunker

var app = angular.module('myApp', ['ui.router']);
app.config(function($stateProvider, $urlRouterProvider) {
  $stateProvider.
  state("main", {
    url: "/",
    templateUrl: "main.html",
    controller: "mainCtrl"
  }).
  state("small", {
    url: "/other/small",
    templateUrl: "other_small.html",
    controller: "smallCtrl"
  }).
  state("big", {
    url: "/other/big",
    templateUrl: "other_big.html",
    controller: "bigCtrl"
  })
  $urlRouterProvider.otherwise('/');
});

app.controller('mainCtrl', function($scope) {
  $scope.small_text = "aaa";
  $scope.big_text = new Array(100).fill("AAA");
});
app.controller('smallCtrl', function($scope) {
  $scope.small_text = "aaa";
});
app.controller('bigCtrl', function($scope) {
  $scope.big_text = new Array(100).fill("AAA");
});
body {
  height: 100%;
  margin: 0;
}

footer {
  background-color: #999999;
  color: #F1F1F1;
  text-align: center;
}

.footer {
  position: absolute;
  bottom: 0;
  height: 60px;
  width: 100%;
}

.main-view {
  background-color: #F1F1F1;
}
<!DOCTYPE html>
<html ng-app="myApp">
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular-ui-router/1.0.20/angular-ui-router.min.js"></script>

<body>
  <div>

    <header>
      <h4>Responsive header</h4>
      <a ui-sref="main">Main page</a>
      <a ui-sref="small">Small page</a>
      <a ui-sref="big">Big page</a>
      <hr>
    </header>


    <div class="main-view" ui-view></div>

    <footer class="footer">
      <h4>
        <span>demo website</span> 2018 &copy;
      </h4>
    </footer>

  </div>
  
  <!-- Assume: separate files -->
  <script type="text/ng-template" id="main.html">
    {{::small_text}}
    <div>
      <button ng-click="show_big_text=!show_big_text">
      {{show_big_text ? 'Hide' : 'Show'}} big text
    </button>
      <div ng-show="show_big_text" ng-repeat="text in ::big_text track by $index">
        {{text}}
      </div>
    </div>
  </script>
  <script type="text/ng-template" id="other_small.html">
    {{::small_text}}
  </script>
  <script type="text/ng-template" id="other_big.html">
    <div ng-repeat="text in ::big_text track by $index">
      {{text}}
    </div>
  </script>

</body>
</html>

2 个答案:

答案 0 :(得分:-1)

无需使用position: absolute。只需给出一个margin-top,然后将页脚放在DOM的末尾即可。 然后,无论您的身体内容结束到哪里,页脚都会自动开始

var app = angular.module('myApp', ['ui.router']);
app.config(function($stateProvider, $urlRouterProvider) {
  $stateProvider.
  state("main", {
    url: "/",
    templateUrl: "main.html",
    controller: "mainCtrl"
  }).
  state("small", {
    url: "/other/small",
    templateUrl: "other_small.html",
    controller: "smallCtrl"
  }).
  state("big", {
    url: "/other/big",
    templateUrl: "other_big.html",
    controller: "bigCtrl"
  })
  $urlRouterProvider.otherwise('/');
});

app.controller('mainCtrl', function($scope) {
  $scope.small_text = "aaa";
  $scope.big_text = new Array(100).fill("AAA");
});
app.controller('smallCtrl', function($scope) {
  $scope.small_text = "aaa";
});
app.controller('bigCtrl', function($scope) {
  $scope.big_text = new Array(100).fill("AAA");
});
body {
  height: 100%;
  margin: 0;
}

footer {
  background-color: #999999;
  color: #F1F1F1;
  text-align: center;
}

.footer {
  margin-top: 20px;
  width: 100%;
}

.main-view {
  background-color: #F1F1F1;
}
<!DOCTYPE html>
<html ng-app="myApp">
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular-ui-router/1.0.20/angular-ui-router.min.js"></script>

<body>
  <div>

    <header>
      <h4>Responsive header</h4>
      <a ui-sref="main">Main page</a>
      <a ui-sref="small">Small page</a>
      <a ui-sref="big">Big page</a>
      <hr>
    </header>


    <div class="main-view" ui-view></div>

    <footer class="footer">
      <h4>
        <span>demo website</span> 2018 &copy;
      </h4>
    </footer>

  </div>
  
  <!-- Assume: separate files -->
  <script type="text/ng-template" id="main.html">
    {{::small_text}}
    <div>
      <button ng-click="show_big_text=!show_big_text">
      {{show_big_text ? 'Hide' : 'Show'}} big text
    </button>
      <div ng-show="show_big_text" ng-repeat="text in ::big_text track by $index">
        {{text}}
      </div>
    </div>
  </script>
  <script type="text/ng-template" id="other_small.html">
    {{::small_text}}
  </script>
  <script type="text/ng-template" id="other_big.html">
    <div ng-repeat="text in ::big_text track by $index">
      {{text}}
    </div>
  </script>

</body>
</html>

答案 1 :(得分:-1)

您可以尝试以下操作:

<div style="min-height: 100vh;position: relative;padding-bottom: 123px">
  ... // content
  <div style="position: absolute; bottom: 0; height: 123px">
  </div>
</div>