JQuery implements text expansion and contraction Effects

Source: Internet
Author: User

JQuery implements text expansion and contraction Effects

There is only one small area on the webpage, but the descriptive text is very long. The following script displays the long text.

When you click expand, the text is expanded, and the text is shrunk.

Originally, you can write with the toggle () that comes with jQuery. However, when I do not work, toggle uses the click + flag to do this.

?

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

<Script language = "javascript" src = "jquery. js"> </script>

<Script language = "javascript">

Var cur_status = "less ";

$. Extend ({

Show_more_init: function (){

// Alert ("show_more_init! ");

Var charNumbers = $ (". content" character .html (). length; // total words

Var limit = 100; // display words

If (charNumbers> limit)

{

Var orgText = $ (". content" pai.html (); // Original Text

Var orgHeight = $ (". content"). height (); // original height

Var showText = orgText. substring (0, limit); // The final displayed text

$ (". Content" pai.html (showText );

Var contentHeight = $ (". content"). height (); // The height of the captured content

$ (". Switch"). click (

Function (){

If (cur_status = "less "){

$ (". Content" 2.16.height(contentheight).html (orgText). animate ({height: orgHeight}, {duration: "slow "});

((This).html ("contract ");

Cur_status = "more ";

} Else {

$ (". Content" 2.16.height(orgheight).html (showText). animate ({height: contentHeight}, {duration: "fast "});

("Expand ");

Cur_status = "less ";

}

}

);

}

Else

{

$ (". Switch"). hide ();

}

}

});

$ (Document). ready (function (){

$. Show_more_init ();

});

</Script>

<! DOCTYPE html>

<Html>

<Head>

<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8"/>

<Title> test </title>

<Style>

# Limittext {

Width: 640px;

Height: auto;

Position: relative;

Background-color: # ccc;

Color: black;

}

. Switch {

Font-size: 12px;

Text-align: center;

Cursor: pointer;

Font-family: Verdana;

Font-weight: 800;

Position: absolute;

Bottom: 0;

Width: 100%;

/* Background: url(more-bg.png) repeat-x bottom ;*/

Height: 40px;

Line-height: 80px;

}

</Style>

</Head>

<Body>

<Div id = "limittext">

<Div class = "content" style = "padding-bottom: 15px;">

This is a long text, a long text, this is a long text, a long text, this is a long text, a long text, this is a long text, a long text, this is a long text, a long text, this is a long text, a long text, this is a long text, a long text, this is a long text, a long text, this is a long text, a long text, this is a long text, a long text, this is a long text, a long text, this is a long text, a long text, this is a long text, a long text, this is a long text, a long text, this is a long text, a long text, this is a long text, a long text, this is a long text, a long text, this is a long text, a long text

</Div>

<Div class = "switch"> Expand </div>

</Div>

</Body>

</Html>

Method 2:

?

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

<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<Html xmlns = "http://www.w3.org/1999/xhtml">

<Head>

<Title> jQuery achieves DIV layer contraction and expansion </title>

<Script type = "text/javascript" src = "/images/jquery. js"> </script>

<Style>

/* Scale down and expand */

. Text {line-height: 22px; padding: 0 6px; color: #666 ;}

. Box h1 {padding-left: 10px; height: 22px; line-height: 22px; background: # f1f1f1; font-weight: bold ;}

. Box {position: relative; border: 1px solid # e7e7e7 ;}

</Style>

</Head>

<Body>

<Script type = "text/javascript">

// Hide and expand

$ (Document). ready (function (){

$ (". Box h1"). toggle (function (){

$ (This). next (". text"). animate ({height: 'toggle ', opacity: 'toggle'}, "slow ");

}, Function (){

$ (This). next (". text"). animate ({height: 'toggle ', opacity: 'toggle'}, "slow ");

});

});

</Script>

<! -- Contract and expand effect -->

<Div class = "box">

<H1> contraction and expansion effect

<Div class = "text">

1 <br/>

2 <br/>

3 <br/>

4 <br/>

5 <br/>

</Div>

</Div>

<Br/>

 

<Div class = "box">

<H1> contraction and expansion effect

<Div class = "text">

1 <br/>

2 <br/>

</Div>

</Div>

<Br>

<Font color = red> refresh the page for the first running. </Font>

</Body>

</Html>

The above is all the content of this article. I hope you will like it.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.