原生js和jQuery寫的網頁選項卡特效對比

來源:互聯網
上載者:User

原生js和jQuery寫的網頁選項卡特效對比

  原生js和jQuery寫的網頁選項卡特效對比

 總的來說思路比較簡單,就是先擷取節點,然後對節點進行相應的處理,下面是完整的頁面代碼:

原生js:

?

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

<!DOCTYPE html>

<html>

<head>

<meta charset="utf-8">

<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">

<title>原生js tab</title>

<style type="text/css">

.tab{

margin:10px auto;

position:relative;

width:300px;

}

ul,li{

list-style-type:none;

padding:0;

margin:0;

font:13px/20px SimSun,arial;

color:#333;

text-align:center;

}

.tabTltle ul li{

float:left;

position:relative;

background:#fefefe;

background:-webkit-gradient(linear,left top,left bottom, from(#fefefe), to(#ededed));

padding:7px 15px;

border:1px #ddd solid;

margin-right:-1px;

cursor:pointer;

 

}

.tabTltle ul li.active{

background:#fff;

font-weight: bold;

}

.clearfix{

}

.clearfix:after{

display:block;

clear:both;

overflow:hidden;

content:"";

}

.tabConn{

border:1px #eee solid;

position:relative;

height:100px

}

.tabConn div{

position:absolute;

opacity:0;

filter:alpha(opacity=0);

padding:5px;

text-align:center;

width:100%;

}

.tabConn div.current{

opacity:1;

filter:alpha(opacity=100);

}

</style>

</head>

<body>

<div id="tab" class="tab">

<div class="tabTltle">

<ul class="clearfix">

<li class="active">標題一</li>

<li>標題二</li>

<li>標題三</li>

<li>標題四</li>

</ul>

</div>

<div class="tabConn">

<div class="current">aaaaaaaaaaaaaaa</div>

<div>bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb</div>

<div>cccccccccccccccccccccccccccccccc</div>

<div>ddddddddddddddddddddddddddddd</div>

</div>

</div>

<script type="text/javascript">

(function(){

var tab = document.getElementById("tab");

var tabList = tab.getElementsByTagName("div")[0].getElementsByTagName("li");

var tabConn = tab.getElementsByTagName("div")[1].getElementsByTagName("div");for(var i=0;i<tabList.length;i++){

tabList[i].index = i;

tabList[i].onclick = function(){

showConn(this.index);

}

}

function showConn(_index){

var index = _index;for(var j=0;j<tabList.length;j++){

tabList[j].className = "";

tabConn[j].className = "";

tabConn[j].style.opacity=0;

}

tabConn[index].className="current";

tabList[index].className="active";

}

})();

</script>

</body>

</html>

下面我們來看一下jQuery寫的(css共用,需要引進jQuery庫):

?

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

<!DOCTYPE html>

<html>

<head>

<meta charset="utf-8">

<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">

<title>jQuery tab</title>

<script type="text/javascript" src="js/jquery-1.8.1.min.js"></script>

<style type="text/css">

.tab{

margin:10px auto;

position:relative;

width:300px;

}

ul,li{

list-style-type:none;

padding:0;

margin:0;

font:13px/20px SimSun,arial;

color:#333;

text-align:center;

}

.tabTltle ul li{

float:left;

position:relative;

background:#fefefe;

background:-webkit-gradient(linear,left top,left bottom, from(#fefefe), to(#ededed));

padding:7px 15px;

border:1px #ddd solid;

margin-right:-1px;

cursor:pointer;

 

}

.tabTltle ul li.active{

background:#fff;

font-weight: bold;

}

.clearfix{

}

.clearfix:after{

display:block;

clear:both;

overflow:hidden;

content:"";

}

.tabConn{

border:1px #eee solid;

position:relative;

height:100px

}

.tabConn div{

position:absolute;

opacity:0;

filter:alpha(opacity=0);

padding:5px;

text-align:center;

width:100%;

}

.tabConn div.current{

opacity:1;

filter:alpha(opacity=100);

}

</style>

</head>

<body>

<h3>jQuery寫的選項卡:</h3>

<div id="tab2" class="tab">

<div class="tabTltle tab-title">

<ul class="clearfix">

<li class="active">標題一</li>

<li>標題二</li>

<li>標題三</li>

<li>標題四</li>

</ul>

</div>

<div class="tabConn tab-conn">

<div class="current">aaaaaaaaaaaaaaa</div>

<div>bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb</div>

<div>cccccccccccccccccccccccccccccccc</div>

<div>ddddddddddddddddddddddddddddd</div>

</div>

</div>

<script type="text/javascript">

$(document).ready(function(){

var $tabTitle = $('.tab-title').find('li');

var $tabList = $('.tab-conn > div');

$tabTitle.click(function(){

$tabTitle.each(function(){

$tabTitle.removeClass('active');

});

var index = $tabTitle.index(this);

$(this).addClass('active');

$tabList.eq(index).addClass('current').siblings().removeClass('current');

});

});

</script>

</body>

</html>

是不是簡單了好多!

以上所述就是本文的全部內容了,希望大家能夠喜歡。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.