JS实现选项卡插件的两种写法(jQuery和class)


Posted in jQuery onDecember 30, 2020

本文实例为大家分享了JS实现选项卡插件的2种写法,供大家参考,具体内容如下

实现插件的几个注意点:

(1)定义一个固定的html结构,比如选项卡的标题的标签为为li,每个选项卡的内容的标签为div等等;
(2)选中时的样式提前确定;
(3)最好先用简单的JS实现选项卡的功能,再改为插件的模式。

html结构如下:

<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>运动</li>
 </ul>
 <div class="active">编程使我快乐</div>
 <div>读书使我幸福</div>
 <div>运动使我健康</div>
</div>

先用JS实现选项卡的功能:

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 = '';
   }
  }
 };
}

实现插件的第一种方法:jQuery

利用$.fn.extend方法,在jQuery上扩展一个选项卡功能的方法:

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

使用方法:

let $tabBox = $('#tabBox');
$tabBox.tabClick();

实现插件的第二种方法:class

利用ES6中的class类,创建一个选项卡类Tab,并添加属性和方法,并且可以多参数传递实现选项卡:

(function(){
 class Tab {
 constructor(selector, options) {
  // 处理第一个参数
  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();
 }

 // 初始化参数
 initialParams(options) {
  let _default = {
  showIndex: 0,
  triggerEvent: 'click'
  };

  for(let key in options) {
  if (!options.hasOwnProperty(key)) break;
  _default[key] = options[key];
  }

  // 把信息挂载到实例上
  for (let key in _default) {
 if (!_default.hasOwnProperty(key)) break;
 this[key] = _default[key];
 }
 }

 // 切换标题
 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 = '';
  });
 }

 // 绑定标题对应的事件
 handleLi() {
  [].forEach.call(this.liList, (item, index) => {
  item.addEventListener(this.triggerEvent, () => {
   this.showIndex = index;
   this.change();
  });
  });
 }
 }
 window.Tab = Tab;
})();

使用方法:

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

第二种方法是现在常用的方法,因为它可以传递很多参数。可以根据需求,设置默认要传递的参数,将这个选项卡插件做的更完善。

如果大家还想深入学习,可以点击两个精彩的专题:javascript选项卡操作方法汇总 jquery选项卡操作方法汇总

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

jQuery 相关文章推荐
jQuery复合事件结合toggle()方法的用法示例
Jun 10 jQuery
jQuery实现拖动效果的实例代码
Jun 25 jQuery
jQuery事件对象的属性和方法详解
Sep 09 jQuery
React中jquery引用的实现方法
Sep 12 jQuery
jQuery实现html双向绑定功能示例
Oct 09 jQuery
Django中使用jquery的ajax进行数据交互的实例代码
Oct 15 jQuery
jQuery实现动态添加节点与遍历节点功能示例
Nov 09 jQuery
jQuery 点击获取验证码按钮及倒计时功能
Sep 20 jQuery
JQuery属性操作与循环用法示例
May 15 jQuery
jQuery中使用validate插件校验表单功能
May 24 jQuery
jQuery中getJSON跨域原理的深入讲解
Sep 02 jQuery
jQuery实现简单评论区功能
Oct 26 jQuery
jQuery实现简单轮播图效果
Dec 27 #jQuery
原生jQuery实现只显示年份下拉框
Dec 24 #jQuery
jquery实现鼠标悬浮弹出气泡提示框
Dec 23 #jQuery
jquery实现图片放大镜效果
Dec 23 #jQuery
jQuery实现增删改查
Dec 22 #jQuery
jQuery实现本地存储
Dec 22 #jQuery
jQuery实现电梯导航模块
Dec 22 #jQuery
You might like
php使用curl简单抓取远程url的方法
2015/03/13 PHP
PHP实现基于mysqli的Model基类完整实例
2016/04/08 PHP
form自动提交实例讲解
2017/07/10 PHP
php闭包中使用use声明变量的作用域实例分析
2018/08/09 PHP
top.location.href 没有权限 解决方法
2008/08/05 Javascript
用javascript getComputedStyle获取和设置style的原理
2008/10/10 Javascript
复选框全选与全不选操作实现思路
2013/08/18 Javascript
基于JQuery实现的图片自动进行缩放和裁剪处理
2014/01/31 Javascript
jQuery中nextUntil()方法用法实例
2015/01/07 Javascript
JavaScript实现点击单选按钮改变输入框中文本域内容的方法
2015/08/12 Javascript
KnockoutJS 3.X API 第四章之数据控制流with绑定
2016/10/10 Javascript
js实现可输入可选择的select下拉框
2016/12/21 Javascript
vue底部加载更多的实例代码
2018/06/29 Javascript
Vue.js实现的购物车功能详解
2019/01/27 Javascript
微信小程序页面上下滚动效果
2020/11/18 Javascript
微信小程序自定义导航栏(模板化)
2019/11/15 Javascript
解决antd datepicker 获取时间默认少8个小时的问题
2020/10/29 Javascript
vue element和nuxt的使用技巧分享
2021/01/14 Vue.js
[40:53]完美世界DOTA2联赛PWL S3 Magma vs DLG 第二场 12.18
2020/12/20 DOTA
Python绘制的二项分布概率图示例
2018/08/22 Python
PyQt5 QTableView设置某一列不可编辑的方法
2019/06/25 Python
Python转换时间的图文方法
2019/07/01 Python
Python+Selenium使用Page Object实现页面自动化测试
2019/07/14 Python
浅谈Python 递归算法指归
2019/08/22 Python
Pandas操作CSV文件的读写实现方法
2019/11/13 Python
德国百年厨具品牌WMF美国站:WMF美国
2016/09/12 全球购物
捷克原创男装和女装购物网站:Bolf.cz
2018/04/28 全球购物
大三自我鉴定范文
2013/10/05 职场文书
工程资料员岗位职责
2014/03/10 职场文书
中秋客户感谢信
2015/01/22 职场文书
玄武湖导游词
2015/02/05 职场文书
2016年社区综治宣传月活动总结
2016/03/16 职场文书
MySQL的join buffer原理
2021/04/29 MySQL
html5中sharedWorker实现多页面通信的示例代码
2021/05/07 Javascript
Python Parser的用法
2021/05/12 Python
SpringBoot 拦截器妙用你真的了解吗
2021/07/01 Java/Android