JavaScript小案例-tab栏切换(可移除item)

gif效果图:
在这里插入图片描述

代码:

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>tab栏切换(可移除item)</title>
  <style>
    * {
      margin: 0;
      padding: 0;
      box-sizing: border-box;
      font-size: 14px;
      color: white;
    }

    body {
      background-color: #333;
    }

    .tab {
      margin: 20px auto;
      list-style: none;
    }

    .item {
      position: relative;
      float: left;
      padding: 10px;
      background-color: #222;
      border-top: 1px solid black;
      border-bottom: 1px solid black;
      cursor: pointer;
    }

    .item:nth-child(1) {
      border-left: 1px solid black;
    }

    .item:nth-last-child(1) {
      border-right: 1px solid black;
    }

    .active {
      border-bottom: 0;
      background-color: #333;
      border-bottom-left-radius: -4px;
      border-bottom-right-radius: -4px;
    }

    .close {
      margin-left: 10px;
      opacity: .3;
    }

    .close:hover {
      background-color: #aaa7a7;
    }
  </style>
</head>

<body>
  <ul class="tab">
    <li class="item">菜单<span class="close">X</span></li>
    <li class="item">菜单<span class="close">X</span></li>
    <li class="item">菜单<span class="close">X</span></li>
    <li class="item">菜单<span class="close">X</span></li>
    <li class="item">菜单<span class="close">X</span></li>
    <li class="item">菜单<span class="close">X</span></li>
    <li class="item">菜单<span class="close">X</span></li>
    <li class="item">菜单<span class="close">X</span></li>
    <li class="item">菜单<span class="close">X</span></li>
    <li class="item">菜单<span class="close">X</span></li>
    <li class="item">菜单<span class="close">X</span></li>
  </ul>
  <script>
    const tab = document.querySelector('.tab')
    tab.addEventListener('click', ({ target }) => {
      // 当前活跃tab
      if (target.tagName === 'LI') {
        let activeOld = document.querySelector('.active')
        if (activeOld) {
          activeOld.classList.remove('active')
          let r = activeOld.nextElementSibling
          if (r) r.style.borderBottomLeftRadius = '0px'
          let l = activeOld.previousElementSibling
          if (l) l.style.borderBottomRightRadius = '0px'
        }
        target.classList.add('active')
        let nr = target.nextElementSibling
        if (nr) nr.style.borderBottomLeftRadius = '5px'
        let nl = target.previousElementSibling
        if (nl) nl.style.borderBottomRightRadius = '5px'
      }
      // 移除tab
      if (target.classList.contains('close')) {
        const parent = target.parentElement
        if (parent.classList.contains('active')) {
          let r = parent.nextElementSibling
          if (r) r.style.borderBottomLeftRadius = '0px'
          let l = parent.previousElementSibling
          if (l) l.style.borderBottomRightRadius = '0px'
        }
        parent.remove()
      }
    })
  </script>
</body>

</html>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

bitDesigner

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值