加入收藏 | 设为首页 | 会员中心 | 我要投稿 平顶山站长网 (https://www.0375zz.cn/)- 分布式云、数据处理、媒体处理、图像分析、基础存储!
当前位置: 首页 > 综合聚焦 > 编程要点 > 语言 > 正文

JS实现选项卡插件有什么办法?哪些问题要注意?

发布时间:2022-03-24 14:18:36 所属栏目:语言 来源:互联网
导读:我们平常在浏览一些网站的时候,经常能看到选项卡展示功能,其好处就是节约页面空间,我们只要点击不同的区域,就能展现不同的内容,这样能比较合理的展示内容。那么JS是怎样实现选项卡插件的呢? 实现插件的几个注意点: (1)定义一个固定的html结构,比如
        我们平常在浏览一些网站的时候,经常能看到选项卡展示功能,其好处就是节约页面空间,我们只要点击不同的区域,就能展现不同的内容,这样能比较合理的展示内容。那么JS是怎样实现选项卡插件的呢?
 
        实现插件的几个注意点:
 
        (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'
});
        第二种方法是现在常用的方法,因为它可以传递很多参数。可以根据需求,设置默认要传递的参数,将这个选项卡插件做的更完善。

(编辑:平顶山站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    热点阅读