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

淺談Angular文字折疊展開組件的原理分析

 更新時間:2017年11月24日 10:22:45   作者:Wsscat  
本篇文章主要介紹了淺談Angular文字折疊展開組件的原理分析,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

自己寫了個Angular的文字折疊組件,這種組件其實很多地方都能用到效果如下

展開后的效果


折疊后的效果


先放全部代碼,使用的時候只需要把自己需要展現(xiàn)的文字{{designer.des}}替換成自己所在路由器所需要綁定的數(shù)據(jù)即可

.directive('textfold', function() {
    return {
      restrict: 'EA',
      template: '<p style="font-size: 14px; border-left:5px solid #dddddd; padding: 15px; padding-bottom: 10px; margin-bottom: 15px; margin-top: 15px;">' + '<span id="textfold" style="display:block; overflow:hidden;">{{designer.des}}</span>' + '<br />' + '<span style="color: #1bb7ac; position: relative; bottom: 10px;" ng-click="more(this)">{{isMore}}</span>' + '</p>',
      link: function(scope, element, attrs) {
        var height;
        var maxheight;
        function textfold() {
          height = angular.element('#textfold').height();
          maxheight = angular.element('#textfold').height();
        }
        scope.$watch('designer.des', function(data) {
          if (data) {
            textfold();
          }
        })
        var isExtend = true;
        scope.isMore = "折疊";
        scope.more = function() {
          var minheight = 23;
          if (isExtend) {
            if (height >= minheight) {
              document.getElementById('textfold').style.height = height + "px";
              setTimeout(function() {
                scope.more();
              }, 1);
              height -= 10;
            } else {
              scope.isMore = "查看更多...";
              scope.$apply();
              isExtend = !isExtend;
              height += 10;
            }
          } else {
            if (height <= maxheight) {
              document.getElementById('textfold').style.height = height + "px";
              setTimeout(function() {
                scope.more();
              }, 1);
              height += 10;
            } else {
              scope.isMore = "折疊";
              scope.$apply();
              isExtend = !isExtend;
              height -= 10;
            }
          }
        }
      }
    }
  })

下面我一句句的分析這個組件的思路

首先肯定是定義好Angular該組件化的模板和使用模式

restrict: 'EA',
template: '<p style="font-size: 14px; border-left:5px solid #dddddd; padding: 15px; padding-bottom: 10px; margin-bottom: 15px; margin-top: 15px;">' + '<span id="textfold" style="display:block; overflow:hidden;">{{designer.des}}</span>' + '<br />' + '<span style="color: #1bb7ac; position: relative; bottom: 10px;" ng-click="more(this)">{{isMore}}</span>' + '</p>',

EA為,使用元素和屬性的方法都可以在DOM里面展現(xiàn)這個插件,既我可以這樣 <textfold></textfold>也可以這樣<div textfold="Wsscat"></div>的形式來復(fù)用該組件 后面我們使用link定義一個函數(shù)

var height;
var maxheight;

這兩個變量一個是此時折疊完后的高度(這個是在展開變成折疊的過程中不斷發(fā)生變化的,最后折疊后就是等于minheight),一個文字完全展開時候獲取的高度

function textfold() {
          height = angular.element('#textfold').height();
          maxheight = angular.element('#textfold').height();
        }
        scope.$watch('designer.des', function(data) {
          if (data) {
            textfold();
            scope.more();
          }
        })

這兩句其實很重要的,當(dāng)我們獲取文字的高度時候,是必須要捕獲文字的變化(文字完全渲染后的高度),所以我們用scope.$watch來捕獲designer.des的變化,當(dāng)data不為空,即文字已渲染后

if (data) {
            textfold();
          }

再去執(zhí)行回調(diào)函數(shù)textfold來獲取當(dāng)前文字的高度,暫時試過這種方法可以獲取到文字渲染后的高度

如果順序執(zhí)行而不是回調(diào)執(zhí)行

angular.element('#textfold').height() 

只會拿到span標(biāo)簽的默認高度而已

這里還可以添加個小技巧,增加一句scope.more();

if (data) {
            textfold();
            scope.more();
          }

這樣就可以讓它頁面渲染完后先展開,然后再折疊,那么我們就在進來頁面的時候默認是折疊的狀態(tài)了,在程序里面,寫這種效果,一般是先讓它文字展開獲取到高度再返回成折疊狀態(tài),來達到進來頁面就是折疊的文字狀態(tài)效果

var isExtend = true;這句是讓下面的scope.more進入第一個分支折疊狀態(tài)

setTimeout(function() {
                scope.more();
              }, 1);

這句是一句遞歸,其實就相當(dāng)于實現(xiàn)jQuery的animate的文字框伸縮動畫,只是這里用了一個遞歸來實現(xiàn)不斷進來判斷狀態(tài)從而改變height值

然后通過改變scope.isMore改變它是查看更多…還是折疊

由于這里是用DOM操作

document.getElementById('textfold').style.height = height + "px"; 

下面這里最好加多一句

scope.$apply(); 

來獲取DOM的變化

其實大概思路就是很簡單的,其他一些地方也是很容易理解~有什么好的方法歡迎推薦,或者文中有什么錯漏或者不足還請多多留言告知,以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論