In actual projects, due to the uncertainty of the length of the text content and the fixity of the page layout, it is inevitable that the text content exceeds the p (or other tags, the same below) area. In this case, a better approach is When the text exceeds the limited p width, it is automatically displayed with an ellipsis (...). In this way, according to custom, people will know that there is text here that has been omitted. There is a property in css called text-overflow:ellipsis; for example, using css you can write like this:
{width:27em; white-space:nowrap; text-overflow:ellipsis; -o-text- overflow:ellipsis; overflow:hidden;} Only in the Firefox browser, the text overflow ellipsis representation cannot be realized. The text is clicked directly from the middle. I will not talk about how to use CSS to achieve this. The specific CSS implementation You can go to Baidu yourself. The most important thing for me here is to talk about how to use JS to implement it, and how to write a simple component through JS. I can directly call the initialization method of JS to implement it! For example, the following effect: The dots after
will prompt the user that there is more content that is not displayed to complete this effect!
Cut the nonsense first! First, let’s take a look at the demo effect I made, so that you can understand what the effect is!
If you want to see the effect, please click me! OK?
1: Let’s first look at the configuration items of the component: as follows:
targetCls
null, The required items of the container to be intercepted by the target default to null
limitLineNumber 1, The number of lines to be displayed defaults to 1 line
type '...', The type displayed exceeds the length of the container. The default is ellipsis
lineHeight 18, The line height of the dom node The default line height is 18
isShowTitle true, title Whether title is required to display all content, the default is true
isCharLimit false Based on the length of characters, limit the display of ellipses exceeding
maxLength 20 The default length is 20. After 20 characters, an ellipsis is displayed
2: Analysis
1. First, let’s talk about this component: it supports 2 ways to intercept strings. First: According to the character length to intercept, and an ellipsis will be displayed after exceeding it. For example, I call it like this:
1 2 3 4 5 6 |
|
This initialization means that isCharLimit is true, which means that the number of characters is used to intercept. The maximum length maxLength is 18, so initialization , because the code will first determine if isCharLimit is true, and then intercept directly based on the number of characters, such as the following code:
2. The second type is intercepted based on the number of lines and height, such as The row height of the default configuration item is 18. If I want to display 2 rows, that means the height h = 18*2. If the height of the container is 100, then the interception method is:
Use (100 - The length of type - 1) Is it greater than 18×2? If it is greater, continue to intercept, otherwise it will not intercept and the ellipsis effect will be displayed! The following code:
Disadvantages: However, if you use row height interception, it is ok if the data is relatively small, but if there is a lot of data, such as a height of 500 pixels or more, it will relatively affect Performance, because they have to calculate n times each time (n means there are many functions called in a loop).
All the JS codes are as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 |
|
Related recommendations:
CSS limits the number of characters displayed beyond the limit and uses ellipsis to indicate
How to display the ellipsis '...' when the title text overflows
htmlHow to use ellipsis when the text control display exceeds the number of characters
The above is the detailed content of js realizes that exceeding the text becomes an ellipsis. For more information, please follow other related articles on the PHP Chinese website!