Code Monkey home page Code Monkey logo

Comments (4)

Wscats avatar Wscats commented on July 19, 2024
directive('textfold', function() {
        return {
            restrict: 'EA',
            template: '<p style="font-size: 14px; border-left:5px solid #dddddd; padding: 10px; padding-bottom: 10px; margin-bottom: 15px; margin-top: 15px;">' + '<span id="textfold" style="display:block; overflow:hidden;">{{designer.des}}</span>' + '<br id="br" />' + '<span id="isMore" style="color: #1bb7ac; position: relative; bottom: 10px;" ng-click="more(this)">{{isMore}}</span>' + '</p>',
            link: function(scope, element, attrs) {
                var height;
                var maxheight;
                var minheight = 23;
                function textfold() {
                    height = angular.element('#textfold').height();
                    maxheight = angular.element('#textfold').height();
                }
                scope.$watch('designer.des', function(data) {
                    if (data) {
                        document.getElementById('textP').style.display = 'block';
                        textfold();
                        scope.more();
                        scope.status();
                    }else{
                        document.getElementById('textP').style.display = 'none';
                    }
                })
                var isExtend = true;
                scope.isMore = "查看更多";
                scope.status =function(){
                    if(maxheight<=minheight){
                        //scope.isMore = "";
                        document.getElementById('isMore').style.display = 'none';
                        document.getElementById('br').style.display = 'none';
                    }
                }
                scope.more = function() {
                    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;
                            document.getElementById('textfold').style.height = "20px";
                        }
                    } 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;
                        }
                    }
                }
            }
        }
    });

这里我后来再改进一下,就是增加设置默认展示的高度
var minheight = 23;
这个是可以自己改的,我这里23px,刚好是font-size为14px,height为20px时候显示一行

from articles.

Wscats avatar Wscats commented on July 19, 2024

修复移动端的折叠文字出现的问题
主要增加{{isMore2}}来展示折叠文字,如果绑定在{{isMore}}上面在移动端会出现部分显示问题

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

                function textfold() {
                    height = angular.element('#textfold').height();
                    maxheight = angular.element('#textfold').height();
                }
                scope.$watch('designer.des', function(data) {
                    if (data) {
                        document.getElementById('textP').style.display = 'block';
                        textfold();
                        scope.more();
                        scope.status();
                    } else {
                        document.getElementById('textP').style.display = 'none';
                    }
                })
                var isExtend = false;
                scope.isMore = "查看更多...";
                scope.status = function() {
                    if (maxheight <= minheight) {
                        document.getElementById('isMore').style.display = 'none';
                        document.getElementById('br').style.display = 'none';
                        document.getElementById('isMore2').style.display = 'none';
                        document.getElementById('br').style.display = 'none';
                    }
                }
                scope.more = function() {
                    if (isExtend) {
                        if (height >= minheight) {
                            document.getElementById('textfold').style.height = height + "px";
                            setTimeout(function() {
                                scope.more();
                            }, 1);
                            height -= 10;
                        } else {
                            scope.isMore = "查看更多";
                            scope.isMore2 = "";
                            if (scope.$root.$$phase != '$apply' && scope.$root.$$phase != '$digest') {
                                scope.$apply();
                            }
                            isExtend = !isExtend;
                            height += 10;
                            document.getElementById('textfold').style.height = "20px";
                        }
                    } else {
                        if (height <= maxheight) {
                            document.getElementById('textfold').style.height = height + "px";
                            setTimeout(function() {
                                scope.more();
                            }, 1);
                            height += 10;
                        } else {
                            scope.isMore = "";
                            scope.isMore2 = "折叠";
                            if (scope.$root.$$phase != '$apply' && scope.$root.$$phase != '$digest') {
                                scope.$apply();
                            }
                            isExtend = !isExtend;
                            height -= 10;
                        }
                    }
                }
            }
        }
    });

from articles.

qq452261789 avatar qq452261789 commented on July 19, 2024

这逼高大上

from articles.

pengril avatar pengril commented on July 19, 2024

厉害了

from articles.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.