Js implementation method similar to Sina Weibo's homepage content for gradual display

Source: Internet
Author: User

Js implementation method similar to Sina Weibo's homepage content for gradual display

Key Aspect 1:

1

2

3

4

5

If (list_li.length> = 1 ){

List. insertBefore (li, list_li [0]);

} Else {

List. appendChild (li );

}

Insert new content from the front. If there is no new content, insert new content to the back.

Key Aspect 2:

1

2

Var height = li. offsetHeight;

Li. style. height = '0 ';

Obtain the li height, and then set the li height to 0, because the height changes from 0 to the existing height.

Key Aspect 3:

1

2

3

Startrun (li, "height", height, function (){

Startrun (li, "opacity", "100 ");

})

First, high change, then transparency change

Finally, run the following code:

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

<! DOCTYPE html>

<Html>

<Head>

<Meta charset = "gb2312"/>

<Title> untitled document </title>

<Style>

<! --

Body, ul, li {margin: 0; padding: 0; font: 12px/1.5 arial ;}

# List {width: 400px; margin: 10px auto ;}

# List li {list-style: none; padding: 5px 0;

Overflow: hidden; border-bottom: 1px dotted # ccc;

Filter: alpha (opacity: 0); opacity: 0; vertical-align: middle ;}

-->

</Style>

<Script>

<! --

Window. onload = function (){

Var btn = document. getElementById ("btn ");

Var con = document. getElementById ("con ");

Var list = document. getElementById ("list ");

Var list_li = list. getElementsByTagName ("li ");

Btn. onclick = function (){

Var li = document. createElement ("li ");

Li. innerHTML = con. value;

Con. value = '';

If (list_li.length> = 1 ){

List. insertBefore (li, list_li [0]);

} Else {

List. appendChild (li );

}

Var height = li. offsetHeight;

Li. style. height = '0 ';

Startrun (li, "height", height, function (){

Startrun (li, "opacity", "100 ");

})

}

}

Function getstyle (obj, name ){

If (obj. currentStyle ){

Return obj. currentStyle [name];

} Else {

Return getComputedStyle (obj, false) [name];

}

}

Function startrun (obj, attr, target, fn ){

ClearInterval (obj. timer );

Obj. timer = setInterval (function (){

Var cur = 0;

If (attr = "opacity "){

Cur = Math. round (parseFloat (getstyle (obj, attr) * 100 );

} Else {

Cur = parseInt (getstyle (obj, attr ));

}

Var speed = (target-cur)/8;

Speed = speed> 0? Math. ceil (speed): Math. floor (speed );

If (cur = target ){

ClearInterval (obj. timer );

If (fn ){

Fn ();

}

} Else {

If (attr = "opacity "){

Obj. style. filter = "alpha (opacity =" + (cur + speed) + ")";

Obj. style. opacity = (cur + speed)/100;

} Else {

Obj. style [attr] = cur + speed + "px ";

}

}

}, 30)

}

// -->

</Script>

</Head>

<Body>

<Textarea id = "con" cols = "50" rows = "5"> </textarea>

<Input id = "btn" name = "" type = "button" value = "publish">

<Ul id = "list">

</Ul>

</Body>

</Html>

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.