案例--封装--面向对象 选项卡

这篇博客介绍了一个使用JavaScript实现选项卡功能的代码示例。通过构造函数`Tabs`,创建实例并指定范围元素及触发事件类型,实现选项卡切换。在`init`方法中,为每个按钮元素绑定点击事件,动态地切换选中状态,从而达到内容展示的效果。示例还包含了HTML结构和CSS样式,用于展示选项卡布局。
摘要由CSDN通过智能技术生成

在你书写 构造函数 和 原型内容 的时候

  + 脑海中需要始终想象着未来实例的样子

  + 只要你书写了 this.xxx = yyy

    => 就是在向实例身上添加一个 xxx 成员, 值是 yyy

  + 只要你访问了 this.xxx

    => 就是在访问 实例对象 身上的 xxx


// 是否需要参数
//   至少需要一个参数, 你出现选项卡的范围元素
function Tabs(select, type = 'click') {
  // 第一个属性, 选项卡的范围元素
  this.ele = document.querySelector(select)

  // 1. 获取实现选项卡需要用到的属性
  // btns 和 boxs
  // 是整个页面范围内的吗 ? 就是和这个 this.ele 元素下的 ul 下的 li
  this.btns = this.ele.querySelectorAll('ul > li')
  this.boxs = this.ele.querySelectorAll('ol > li')
  this.type = type

  // 这个位置的 this 是谁 ?
  // 当前实例
  this.init()
}

// 2. 需要一个方法
// 作用: 给 实例对象 身上的 btns 内的每一个 li 绑定点击事件
// 既然是方法, 最好书写在 prototype 上
Tabs.prototype.init = function () {
  // 2-1. 循环遍历 this.btns
  this.btns.forEach((item, index) => {
    item.addEventListener(this.type, () => {
      // 2-2. 给 this.btns 和 this.boxs 内的每一个 li 取消类名
      for (let i = 0; i < this.btns.length; i++) {
        this.btns[i].className = this.boxs[i].className = ''
      }

      // 2-3. 给 index 对应的添加类名
      this.btns[index].className = this.boxs[index].className = 'active'
    })
  })
}

//调用
new Tabs('#box', 'click')
new Tabs('#box2', 'mouseover')
new Tabs('#box3')

<div class="box" id="box">
    <ul>
      <li class="active">1</li>
      <li>2</li>
      <li>3</li>
    </ul>
    <ol>
      <li class="active">1</li>
      <li>2</li>
      <li>3</li>
    </ol>
  </div>

  <div class="box" id="box2">
    <ul>
      <li class="active">1</li>
      <li>2</li>
      <li>3</li>
    </ul>
    <ol>
      <li class="active">1</li>
      <li>2</li>
      <li>3</li>
    </ol>
  </div>

* {
  margin: 0;
  padding: 0;
}

.box {
  width: 600px;
  height: 350px;
  border: 10px solid pink;
  margin: 30px auto;
  display: flex;
  flex-direction: column;
}

.box > ul {
  height: 80px;
  display: flex;
}

.box > ul > li {
  flex: 1;
  background-color: skyblue;
  color: #fff;
  font-size: 34px;
  display: flex;
  justify-content: center;
  align-items: center;
  cursor: pointer;
}

.box > ul > li.active {
  background-color: orange;
}

.box > ol {
  flex: 1;
  position: relative;
}

.box > ol > li {
  width: 100%;
  height: 100%;
  position: absolute;
  left: 0;
  top: 0;
  display: none;
  justify-content: center;
  align-items: center;
  background-color: purple;
  color: #fff;
  font-size: 100px;
}

.box > ol > li.active {
  display: flex;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值