一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

node.js|vue.js|jquery|angularjs|React|json|js教程|

服務(wù)器之家 - 編程語言 - JavaScript - js教程 - JS實(shí)現(xiàn)選項(xiàng)卡插件的兩種寫法(jQuery和class)

JS實(shí)現(xiàn)選項(xiàng)卡插件的兩種寫法(jQuery和class)

2021-12-22 16:17南柯Seven js教程

這篇文章主要為大家詳細(xì)介紹了JS實(shí)現(xiàn)選項(xiàng)卡插件的兩種寫法:jQuery和class,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了JS實(shí)現(xiàn)選項(xiàng)卡插件的2種寫法,供大家參考,具體內(nèi)容如下

實(shí)現(xiàn)插件的幾個注意點(diǎn):

(1)定義一個固定的html結(jié)構(gòu),比如選項(xiàng)卡的標(biāo)題的標(biāo)簽為為li,每個選項(xiàng)卡的內(nèi)容的標(biāo)簽為div等等;
(2)選中時的樣式提前確定;
(3)最好先用簡單的JS實(shí)現(xiàn)選項(xiàng)卡的功能,再改為插件的模式。

html結(jié)構(gòu)如下:

?
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
<style>
 * {
 margin: 0;
 padding: 0;
 }
 
 ul {
 list-style: none;
 }
 
 #tabBox {
 box-sizing: border-box;
 margin: 20px auto;
 width: 500px;
 }
 
 .navBox {
 display: flex;
 position: relative;
 top: 1px;
 }
 
 .navBox li {
 box-sizing: border-box;
 margin-right: 10px;
 padding: 0 10px;
 line-height: 35px;
 border: 1px solid #999;
 cursor: pointer;
 }
 
 .navBox li.active {
 border-bottom-color: #FFF;
 }
 
 #tabBox>div {
 display: none;
 box-sizing: border-box;
 padding: 10px;
 height: 150px;
 border: 1px solid #999;
 }
 
 #tabBox>div.active {
 display: block;
 }
 </style>
 
 <div id="tabBox">
 <ul class="navBox">
 <li class="active">編程</li>
 <li>讀書</li>
 <li>運(yùn)動</li>
 </ul>
 <div class="active">編程使我快樂</div>
 <div>讀書使我幸福</div>
 <div>運(yùn)動使我健康</div>
</div>

先用JS實(shí)現(xiàn)選項(xiàng)卡的功能:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
let len = liList.length;
 for(let i = 0; i < len; i++) {
  liList[i].index = i;
  liList[i].onclick = function() {
  for(let j = 0; j < len; j++) {
   if(j === this.index) {
   liList[j].className = 'active';
   contentList[j].className = 'active';
   }
   else{
   liList[j].className = '';
   contentList[j].className = '';
   }
  }
 };
}

實(shí)現(xiàn)插件的第一種方法:jQuery

利用$.fn.extend方法,在jQuery上擴(kuò)展一個選項(xiàng)卡功能的方法:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
(function($){
 function clickLi() {
 let $this = this,
  $navBox = $this.find('.navBox'),
  $liList = $navBox.find('li'),
  $contentList = $this.find('div');
 
 $liList.click(function(){
  let $this = $(this),
  index = $this.index();
  $this.addClass('active').siblings().removeClass('active');
  $contentList.eq(index).addClass('active').siblings().removeClass('active');
 });
 }
 
 $.fn.extend({
 tabClick: clickLi
 });
})(jQuery);

使用方法:

?
1
2
let $tabBox = $('#tabBox');
$tabBox.tabClick();

實(shí)現(xiàn)插件的第二種方法:class

利用ES6中的class類,創(chuàng)建一個選項(xiàng)卡類Tab,并添加屬性和方法,并且可以多參數(shù)傳遞實(shí)現(xiàn)選項(xiàng)卡:

?
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
(function(){
 class Tab {
 constructor(selector, options) {
  // 處理第一個參數(shù)
  if(!selector)
  throw new ReferenceError('The first selector parameter must be passed~~');
  if(typeof selector === 'string')
  this.container = document.querySelector(selector);
  else if(selector.nodeType)
  this.container = selector;
 
  this.initialParams(options);
 
  this.navBox = this.container.querySelector('.navBox'),
  this.liList = this.navBox.querySelectorAll('li'),
  this.contentList = this.container.querySelectorAll('div'),
  this.count = this.liList.length;
  
  this.change();
  this.handleLi();
 }
 
 // 初始化參數(shù)
 initialParams(options) {
  let _default = {
  showIndex: 0,
  triggerEvent: 'click'
  };
 
  for(let key in options) {
  if (!options.hasOwnProperty(key)) break;
  _default[key] = options[key];
  }
 
  // 把信息掛載到實(shí)例上
  for (let key in _default) {
 if (!_default.hasOwnProperty(key)) break;
 this[key] = _default[key];
 }
 }
 
 // 切換標(biāo)題
 change() {
  [].forEach.call(this.liList, (item, index) => {
  if(index === this.showIndex) {
   this.liList[index].className = 'active';
   this.contentList[index].className = 'active';
   return;
  }
  this.liList[index].className = '';
  this.contentList[index].className = '';
  });
 }
 
 // 綁定標(biāo)題對應(yīng)的事件
 handleLi() {
  [].forEach.call(this.liList, (item, index) => {
  item.addEventListener(this.triggerEvent, () => {
   this.showIndex = index;
   this.change();
  });
  });
 }
 }
 window.Tab = Tab;
})();

使用方法:

?
1
2
3
4
new Tab('#tabBox', {
 showIndex: 2,
 triggerEvent: 'mouseenter'
});

第二種方法是現(xiàn)在常用的方法,因?yàn)樗梢詡鬟f很多參數(shù)。可以根據(jù)需求,設(shè)置默認(rèn)要傳遞的參數(shù),將這個選項(xiàng)卡插件做的更完善。

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。

原文鏈接:https://blog.csdn.net/m0_37922443/article/details/111884939

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 亚洲精品第三页 | 魔兽官方小说 | 91大神在线观看精品一区 | 免费网址在线观看入口推荐 | 99在线资源| 动漫美女隐私尿口图片 | 短篇艳妇系列 | 国产一区二区精品久久91 | 欧美在线视频 一区二区 | 加勒比一本大道在线 | 亚洲v成人天堂影视 | 天天射寡妇射 | 国产伦码精品一区二区 | 精品9e精品视频在线观看 | 97导航| 男人午夜剧场 | ai换脸杨颖被啪在线观看 | 日韩精品一区二区三区毛片 | 深夜激情网 | 亚洲品质自拍网站 | 国产精品边做边接电话在线观看 | 青青99| 99在线观看视频免费 | 国产欧美国产精品第一区 | gogort99人体专业网站 | 亚洲卡一卡2卡三卡4卡无卡三 | 亚洲国产精品久久精品怡红院 | 爱草视频| 肉蒲在线观看 | 91制片厂制作果冻传媒123 | 久久精品亚洲国产AV涩情 | 免费精品在线视频 | 日本人做受全过程视频 | 男人天堂a | 猛h辣h高h文湿重口 门房秦大爷在线阅读 | 国产日韩欧美精品在线 | 国产在视频线精品视频 | 好男人免费高清在线观看2019 | 沉香如屑西瓜视频免费观看完整版 | 四虎影视永久免费视频观看 | 亚洲视频免费在线观看 |