欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

angularjs 頁(yè)面自適應(yīng)高度的方法

 更新時(shí)間:2018年01月17日 09:57:42   作者:liubinwyzbt  
本篇文章主要介紹了angularjs 頁(yè)面自適應(yīng)高度的方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來(lái)看看吧

需求

在angularjs構(gòu)建的業(yè)務(wù)系統(tǒng)中,通過ui-view路由實(shí)現(xiàn)頁(yè)面跳轉(zhuǎn),初始化進(jìn)入系統(tǒng)后,右側(cè)內(nèi)容區(qū)域需要自適應(yīng)瀏覽器高度。

實(shí)現(xiàn)方案

  1. 在ui-view所在的Div添加directive,directive中通過element.css初始化計(jì)算div的高度,動(dòng)態(tài)更新div高度
  2. directive監(jiān)聽($$watch)angular的$digest,實(shí)時(shí)獲取body高度,動(dòng)態(tài)賦值model或element.css改變

方案1:添加directive和element.css自適應(yīng)高度

1.創(chuàng)建directive

define([ "app" ], function(app) {
  app.directive('autoHeight',function ($window) {
    return {
      restrict : 'A',
      scope : {},
      link : function($scope, element, attrs) {
        var winowHeight = $window.innerHeight; //獲取窗口高度
        var headerHeight = 80;
        var footerHeight = 20;
        element.css('min-height',
            (winowHeight - headerHeight - footerHeight) + 'px');
      }
    };
  });
  return app;
});

2.div元素添加directive

<div ui-view auto-height></div>

3.效果圖

原界面:右側(cè)區(qū)域的高度為自適應(yīng)內(nèi)容,導(dǎo)致下方存在黑色的背景色

調(diào)整后:右側(cè)區(qū)域的高度自適應(yīng)瀏覽器

方案2:$watch監(jiān)聽body高度,賦值改變高度

1.創(chuàng)建resize directive

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

function AppController($scope) {
  /* Logic goes here */
}

app.directive('resize', function ($window) {
  return function (scope, element) {
    var w = angular.element($window);
    scope.getWindowDimensions = function () {
      return { 'h': w.height(), 'w': w.width() };
    };
    scope.$watch(scope.getWindowDimensions, function (newValue, oldValue) {
      scope.windowHeight = newValue.h;
      scope.windowWidth = newValue.w;

      scope.style = function () {
        return { 
          'height': (newValue.h - 100) + 'px',
          'width': (newValue.w - 100) + 'px' 
        };
      };

    }, true);

    w.bind('resize', function () {
      scope.$apply();
    });
  }
})

2.在div元素上增加resize directive

<div ng-app="miniapp" ng-controller="AppController" ng-style="style()" resize>
  window.height: {{windowHeight}} <br />
  window.width: {{windowWidth}} <br />
</div>

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論