How to Implement Simple tabs and automatic switch in js

Source: Internet
Author: User

How to Implement Simple tabs and automatic switch in js

Note:

Set an identifier number to 0, write an identifier + 1 every several seconds, execute the function of switching effect, and then execute.
When the length of the tag exceeds the length of the current tab, set it to 0.
Turn off the timer when you move the mouse to the tab, and turn on the timer when you move the mouse away.

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

<! DOCTYPE html>

<Html>

<Head>

<Meta charset = "gb2312"/>

<Title> untitled document </title>

<Style>

Body, ul, li {

Margin: 0;

Padding: 0;

Font: 12px/1.5 arial;

}

Ul, li {

List-style: none;

}

. Wrap {

Width: 500px;

Margin: 20px auto;

}

. Hide {

Display: none;

}

# Tab_t {

Height: 25px;

Border-bottom: 1px solid # ccc;

}

# Tab_t li {

Float: left;

Width: 80px;

Height: 24px;

Line-height: 24px;

Margin: 0 4px;

Text-align: center;

Border: 1px solid # ccc;

Border-bottom: none;

Background: # f5f5f5;

Cursor: pointer

}

# Tab_t. act {

Position: relative;

Height: 25px;

Margin-bottom:-1px;

Background: # fff;

}

# Tab_c {

Border: 1px solid # ccc;

Border-top: none;

Padding: 20px;

}

</Style>

<Script>

Window. onload = function (){

Tab ("tab_t", "li", "tab_c", "div", "onmouseover ")

Function tab (tab_t, tab_t_tag, tab_c, tag_c_tag, evt ){

Var tab_t = document. getElementById (tab_t );

Var tab_t_li = tab_t.getElementsByTagName (tab_t_tag );

Var tab_c = document. getElementById (tab_c );

Var tab_c_li = tab_c.getElementsByTagName (tag_c_tag );

Var len = tab_t_li.length;

Var I = 0;

Var timer = null;

Var num = 0;

For (I = 0; I <len; I ++ ){

Tab_t_li [I]. index = I;

Tab_t_li [I] [evt] = function (){

ClearInterval (timer );

Num = this. index;

Tab_change ()

}

Tab_t_li [I]. onmouseout = function (){

Autoplay ();

}

}

Function tab_change (){

For (I = 0; I <len; I ++ ){

Tab_t_li [I]. className = '';

Tab_c_li [I]. className = 'hide ';

}

Tab_t_li [num]. className = 'ac ';

Tab_c_li [num]. className = '';

}

Function autoplay (){

Timer = setInterval (function (){

Num ++;

If (num> = len) num = 0;

Tab_change ();

},1000 );

}

Autoplay ();

}

}

</Script>

</Head>

<Body>

<Div class = "wrap">

<Ul id = "tab_t">

<Li class = "act"> select 1 </li>

<Li> select 2 </li>

<Li> select 3 </li>

<Li> select 4 </li>

</Ul>

<Div id = "tab_c">

<Div> content 1 </div>

<Div class = "hide"> content 2 </div>

<Div class = "hide"> content 3 </div>

<Div class = "hide"> content 4 </div>

</Div>

</Div>

</Body>

</Html>

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.