How js achieves multiple different motion effects on the same page

Source: Internet
Author: User

How js achieves multiple different motion effects on the same page

Key Aspect 1:

1

2

3

4

5

6

7

Function getstyle (obj, name ){

If (obj. currentStyle ){

Return obj. currentStyle [name];

} Else {

Return getComputedStyle (obj, false) [name];

}

}

From the style sheet, take the value based on the id and attribute name.

Key Aspect 2:

1

2

3

4

5

If (attr = "opacity "){

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

} Else {

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

}

If the transparency value is set, because it may be a decimal point, you need to use parseFloat, And then there may be decimal places, and round them to an integer using the round method.

If you set a non-transparency value, use parseInt to take only the numerical value

Other notes are mentioned in the previous sections and will not be described here.

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

81

82

<! DOCTYPE html>

<Html>

<Head>

<Meta charset = "UTF-8"/>

<Title> untitled document </title>

<Style>

Body, ul, li {margin: 0; padding: 0 ;}

# Runs li {width: 80px; height: 80px; background: # 06c;

List-style: none; margin: 10px; border: 1px solid #000;

Filter: alpha (opacity = 30); opacity: 0.3 ;}

</Style>

<Script>

Window. onload = function (){

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

Var runs_li = runs. getElementsByTagName ("li ");

Runs_li [0]. onmouseover = function (){

Startrun (this, "width", 300 );

}

Runs_li [0]. onmouseout = function (){

Startrun (this, "width", 80 );

}

Runs_li [1]. onmouseover = function (){

Startrun (this, "height", 300 );

}

Runs_li [1]. onmouseout = function (){

Startrun (this, "height", 80 );

}

Runs_li [2]. onmouseover = function (){

Startrun (this, "fontSize", 50 );

}

Runs_li [2]. onmouseout = function (){

Startrun (this, "fontSize", 14 );

}

Runs_li [3]. onmouseover = function (){

Startrun (this, "opacity", 100 );

}

Runs_li [3]. onmouseout = function (){

Startrun (this, "opacity", 30 );

}

}

Function startrun (obj, attr, target ){

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 );

} 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 );

}

Function getstyle (obj, name ){

If (obj. currentStyle ){

Return obj. currentStyle [name];

} Else {

Return getComputedStyle (obj, false) [name];

}

}

</Script>

</Head>

<Body>

<Ul id = "runs">

<Li style = "top: 0"> 1 </li>

<Li style = "top: 90px;"> 2 </li>

<Li style = "top: 180px;"> 3 </li>

<Li style = "top: 270px;"> 4 </li>

</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.