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

折疊后的效果

先放全部代碼,使用的時候只需要把自己需要展現的文字{{designer.des}}替換成自己所在路由器所需要綁定的數據即可
.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里面展現這個插件,既我可以這樣 <textfold></textfold>也可以這樣<div textfold="Wsscat"></div>的形式來復用該組件 后面我們使用link定義一個函數
var height;var maxheight;
這兩個變量一個是此時折疊完后的高度(這個是在展開變成折疊的過程中不斷發生變化的,最后折疊后就是等于minheight),一個文字完全展開時候獲取的高度
function textfold() { height = angular.element('#textfold').height(); maxheight = angular.element('#textfold').height(); } scope.$watch('designer.des', function(data) { if (data) { textfold(); scope.more(); } })這兩句其實很重要的,當我們獲取文字的高度時候,是必須要捕獲文字的變化(文字完全渲染后的高度),所以我們用scope.$watch來捕獲designer.des的變化,當data不為空,即文字已渲染后
if (data) { textfold(); }再去執行回調函數textfold來獲取當前文字的高度,暫時試過這種方法可以獲取到文字渲染后的高度
如果順序執行而不是回調執行
angular.element('#textfold').height() 只會拿到span標簽的默認高度而已
這里還可以添加個小技巧,增加一句scope.more();
if (data) { textfold(); scope.more(); }這樣就可以讓它頁面渲染完后先展開,然后再折疊,那么我們就在進來頁面的時候默認是折疊的狀態了,在程序里面,寫這種效果,一般是先讓它文字展開獲取到高度再返回成折疊狀態,來達到進來頁面就是折疊的文字狀態效果
var isExtend = true;這句是讓下面的scope.more進入第一個分支折疊狀態
setTimeout(function() { scope.more(); }, 1);這句是一句遞歸,其實就相當于實現jQuery的animate的文字框伸縮動畫,只是這里用了一個遞歸來實現不斷進來判斷狀態從而改變height值
然后通過改變scope.isMore改變它是查看更多…還是折疊
由于這里是用DOM操作
document.getElementById('textfold').style.height = height + "px"; 下面這里最好加多一句
scope.$apply();
來獲取DOM的變化
其實大概思路就是很簡單的,其他一些地方也是很容易理解~有什么好的方法歡迎推薦,或者文中有什么錯漏或者不足還請多多留言告知,以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。
新聞熱點
疑難解答