Home Web Front-end JS Tutorial Simple implementation of Angular text folding and expansion effect

Simple implementation of Angular text folding and expansion effect

Dec 18, 2017 am 11:43 AM
angular fold letter

The Angular text folding and expanding effect is also a very interesting function. This article mainly introduces the principle analysis of the Angular text folding and expanding component. The editor thinks it is quite good. Now I will share it with you and give you a reference. I hope it can Help everyone.

I wrote an Angular text folding component. This component can actually be used in many places. The effect is as follows

The effect after expansion


The effect after folding


Put all the code first. When using it, you only need to add the code you need. Just replace the displayed text {{designer.des}} with the data that needs to be bound to your router

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

I will analyze this component sentence by sentence. The idea

The first thing to do is to define the Angular component template and usage mode

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

EA is to use elements and Attribute methods can display this plug-in in the DOM. I can reuse the component in the form of or in the form of

Later we use link to define a function

var height;
var maxheight;
Copy after login

. One of these two variables is the height after folding at this time (this is during the process of expanding into folding Constantly changing, the last folded is equal to minheight), the height obtained when a text is fully expanded

function textfold() {
          height = angular.element(&#39;#textfold&#39;).height();
          maxheight = angular.element(&#39;#textfold&#39;).height();
        }
        scope.$watch(&#39;designer.des&#39;, function(data) {
          if (data) {
            textfold();
            scope.more();
          }
        })
Copy after login

These two sentences are actually very important. When we When obtaining the height of the text, it is necessary to capture the change of the text (the height after the text is fully rendered), so we use scope.$watch to capture the change of designer.des. When data is not empty, that is, after the text has been rendered

if (data) {
            textfold();
          }
Copy after login

Then execute the callback function textfold to get the height of the current text. I have temporarily tried this method to get the height of the text after rendering

If executed sequentially instead of callback

angular.element(&#39;#textfold&#39;).height()
Copy after login

will only get the default height of the span tag

You can also add a little trick here, add the sentence scope.more();

if (data) {
            textfold();
            scope.more();
          }
Copy after login

This way, you can expand the page after it is rendered, and then Folding, then when we enter the page, it will be in the folded state by default. In the program, to write this effect, we usually let the text expand to obtain the height and then return to the folded state, so that the incoming page is the folded text. Status effect

var isExtend = true;This sentence is to let the following scope.more enter the first branch folding state

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

This sentence It's a recursion, which is actually equivalent to implementing jQuery's animate's text box expansion animation. It's just that a recursion is used here to continuously judge the status and change the height value

and then change it by changing scope.isMore Is it view more...or collapse?

Since this is a DOM operation

document.getElementById(&#39;textfold&#39;).style.height = height + "px";
Copy after login

it’s best to add it below One more sentence

scope.$apply();
Copy after login

to get the DOM changes

In fact, the general idea is very simple, and other places are also easy to understand. , you can try it yourself.

Related recommendations:

css2D special effects transform--text folding effect_html/css_WEB-ITnose

JavaScript compatible with IE6 close Implementation cases of folding and unfolding effects

Recommended articles about folding

The above is the detailed content of Simple implementation of Angular text folding and expansion effect. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to make round pictures and text in ppt How to make round pictures and text in ppt Mar 26, 2024 am 10:23 AM

First, draw a circle in PPT, then insert a text box and enter text content. Finally, set the fill and outline of the text box to None to complete the production of circular pictures and text.

How to add dots to text in word? How to add dots to text in word? Mar 19, 2024 pm 08:04 PM

When we create Word documents on a daily basis, we sometimes need to add dots under certain words in the document, especially when there are test questions. To highlight this part of the content, the editor will share with you the tips on how to add dots to text in Word. I hope it can help you. 1. Open a blank word document. 2. For example, add dots under the words &quot;How to add dots to text&quot;. 3. We first select the words &quot;How to add dots to text&quot; with the left mouse button. Note that if you want to add dots to that word in the future, you must first use the left button of the mouse to select which word. Today we are adding dots to these words, so we have chosen several words. Select these words, right-click, and click Font in the pop-up function box. 4. Then something like this will appear

Google Pixel 9 Pro Fold phone case exposed: 6.4-inch outer screen, 8.02-inch inner screen Google Pixel 9 Pro Fold phone case exposed: 6.4-inch outer screen, 8.02-inch inner screen Jun 25, 2024 pm 02:35 PM

According to news on June 25, the source ytechb published a blog post yesterday (June 24), sharing a rendering of the Google Pixel 9 Pro Fold mobile phone case, once again showing the design of the back of this folding screen. According to previous news, Google will release the Pixel 9 series of mobile phones in October this year. In addition to the three phones in the Pixel 9 series, Pixel Fold will also be included in the Pixel 9 series and will be officially named Pixel 9 Pro Fold. The phone case exposed this time comes from accessory manufacturer Torro. The company's UK and US online stores have listed the product phone case and disclosed the design and display size of the phone. The page shows a large number of Pixel 9 Pro Fold phone case renderings

How to install Angular on Ubuntu 24.04 How to install Angular on Ubuntu 24.04 Mar 23, 2024 pm 12:20 PM

Angular.js is a freely accessible JavaScript platform for creating dynamic applications. It allows you to express various aspects of your application quickly and clearly by extending the syntax of HTML as a template language. Angular.js provides a range of tools to help you write, update and test your code. Additionally, it provides many features such as routing and form management. This guide will discuss how to install Angular on Ubuntu24. First, you need to install Node.js. Node.js is a JavaScript running environment based on the ChromeV8 engine that allows you to run JavaScript code on the server side. To be in Ub

Samsung's 10,000-yuan foldable phone W25 revealed: 5-megapixel under-screen front camera and thinner body Samsung's 10,000-yuan foldable phone W25 revealed: 5-megapixel under-screen front camera and thinner body Aug 23, 2024 pm 12:43 PM

According to news on August 23, Samsung is about to launch a new folding mobile phone W25, which is expected to be unveiled at the end of September. It will make corresponding improvements in the under-screen front camera and body thickness. According to reports, Samsung W25, codenamed Q6A, will be equipped with a 5-megapixel under-screen camera, which is an improvement over the 4-megapixel camera of the Galaxy Z Fold series. In addition, the W25’s external-screen front camera and ultra-wide-angle camera are expected to be 10 million and 12 million pixels respectively. In terms of design, the W25 is about 10 mm thick in the folded state, which is about 2 mm thinner than the standard Galaxy Z Fold 6. In terms of screen, the W25 has an external screen of 6.5 inches and an internal screen of 8 inches, while the Galaxy Z Fold6 has an external screen of 6.3 inches and an internal screen of 8 inches.

An article exploring server-side rendering (SSR) in Angular An article exploring server-side rendering (SSR) in Angular Dec 27, 2022 pm 07:24 PM

Do you know Angular Universal? It can help the website provide better SEO support!

How to use PHP and Angular for front-end development How to use PHP and Angular for front-end development May 11, 2023 pm 04:04 PM

With the rapid development of the Internet, front-end development technology is also constantly improving and iterating. PHP and Angular are two technologies widely used in front-end development. PHP is a server-side scripting language that can handle tasks such as processing forms, generating dynamic pages, and managing access permissions. Angular is a JavaScript framework that can be used to develop single-page applications and build componentized web applications. This article will introduce how to use PHP and Angular for front-end development, and how to combine them

Samsung Galaxy Z Flip 6 model first revealed: narrower bezels, creases still present Samsung Galaxy Z Flip 6 model first revealed: narrower bezels, creases still present Jun 22, 2024 am 03:28 AM

According to news on June 21, foreign media recently released model photos of Samsung Galaxy Z Flip 6 on the Internet. According to the picture, it can be understood that the borders of Samsung Galaxy Z Flip 6 will be further narrowed, which means that the width of the phone may be reduced in the folded state, and it will also provide a more comfortable grip and portability. Moreover, compared with the previous generation ZFlip5, the model of Galaxy ZFlip6 is more square and the camera module on the back is more prominent. It is expected to use a new camera sensor. However, from the front, the creases of the phone are still relatively obvious, but considering that the leaked model is a model phone, there may be some differences with the real phone, so it is for reference only. In terms of performance configuration, Galaxy

See all articles