<p class="text">
<p class="desc">目木家居旗舰店,专注家居二十年。我们彰显个性,具有浓郁现代感现代风格家居设计的特色是,其设计的元素、材料都很单一,这种设计风格已经成为越来越多时尚潮人装修的首选,现代风格家居设计从整体到局部、从空间到室内陈设塑造,精雕细琢,给人一丝不苟的印象</p>
<a href="javascript:void(0);" class="more">更多</a>
</p>
How to achieve such an effect, the collapse and expansion arrows will change
I tried writing it, but the effect seems not ideal, because the height written here is fixed, but the amount of this text is uncertain
demo
Directly wrap a layer on the outer layer of p, hide the outer overflow, and get the height setting of p;
Pay attention to the default margin of p. In the following example, use the padding of the parent element to offset it;
It’s actually quite simple:
If you want to expand or shrink based on the number of text, then you set the judgment condition based on the
length
of the text. When the length is less thanformat
a copy ofhtml
, thishtml
There is no icon on it. When the length is greater than the specified length, when shrinking, all text strings are intercepted and then spliced into an expanded icon. There are many on thisfont-awesome
, which corresponds to them and is displayed on the page, and then expanded as well. The same routine, but instead of intercepting the strings, just splice all the strings into a shrunken icon, and then bind events to the two icons respectively. I have given below a specific example of my previous implementation, but the code is incomplete. , it should be enough to provide you with ideas and implementationFor animation, try this:
demo
I got some inspiration after reading @aunt’s answer (here) a few days ago.
The key point is: to expand without a fixed height, first set the height to auto, then use
getComputedStyle
to get the height, then set the height to 0, and use the obtained height to animate.