用jq实现一个tab栏

实现一个简单的tab,具体过程见demo

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>

  <style>
    *{
      margin: 0;
      padding: 0;
    }
    .lbtab {
      display: -webkit-box;
      display: -ms-flexbox;
      display: flex;
      -webkit-box-pack: center;
          -ms-flex-pack: center;
              justify-content: center;
      -webkit-box-align: center;
          -ms-flex-align: center;
              align-items: center;
      background: #fff;
      height: 2.4rem;
      position: relative;
    }
    .lbtab .tabitem {
      -webkit-box-flex: 1;
          -ms-flex: 1;
              flex: 1;
      color: #8C8C8C;
      font-size: 0.56rem;
      font-weight: bold;
      text-align: center;
      position: relative;
      -webkit-transition: all 0.2s;
      transition: all 0.2s;
    }
    .lbtab .tabitem.active {
      font-size: 0.64rem;
      color: #1F1F1F;
    }
    .lbtab .tabline {
      position: absolute;
      height: 0.08rem;
      background: red;
    }
    .lbtab .tabline.backward {
      -webkit-transition: right 0.3s cubic-bezier(0.35, 0, 0.25, 1) 0.09s, left 0.3s cubic-bezier(0.35, 0, 0.25, 1);
      transition: right 0.3s cubic-bezier(0.35, 0, 0.25, 1) 0.09s, left 0.3s cubic-bezier(0.35, 0, 0.25, 1);
    }
    .lbtab .tabline.forward {
      -webkit-transition: right 0.3s cubic-bezier(0.35, 0, 0.25, 1), left 0.3s cubic-bezier(0.35, 0, 0.25, 1) 0.09s;
      transition: right 0.3s cubic-bezier(0.35, 0, 0.25, 1), left 0.3s cubic-bezier(0.35, 0, 0.25, 1) 0.09s;
    }
    .lbtab .innerLine {
      display: block;
      background-color: #FF9900;
      margin: auto;
      height: 100%;
    }
  </style>
  <script type="text/javascript">
      (function () {
          function o() { document.documentElement.style.fontSize = (document.documentElement.clientWidth > 750 ? 750 : document.documentElement.clientWidth) / 15 + "px" }
          var e = null;
          window.addEventListener("resize", function () { clearTimeout(e), e = setTimeout(o, 300) }, !1), o()
      })(window);
  </script>
</head>
<body>
  <div class="lbtab">
    <div class="tabitem active">
      全部
    </div>
    <div class="tabitem">
      已出票
    </div>
    <div class="tabitem">
      已出票
    </div>
    <div class="tabline">
      <span class="innerLine"></span>
    </div>
  </div>

  <script src="https://cdn.bootcss.com/jquery/3.4.1/jquery.min.js"></script>

  <script>
    ;(function (w,$) {
      var DEFAULT_OPTIONS = {
        container:'',
        lineHeight: 2, // 下划线高度
        activeColor: '#FF9900',
        // barPosition: 'bottom',
        barDistance: 0,
        lastIndex: 0, // 上一次的位置
        currentIndex: 0, // 当前的=位置
        customBarWidth: '',
        tabChange: function (index, oldIndex) {}
      }

      function MyTab (options) {
        this.options = $.extend({}, DEFAULT_OPTIONS,options)
        if (!this.options.container) {
          throw new Error('parent box is required')
        }
        var $container = $(this.options.container)
        this.$tabitems = $container.children('.tabitem')
        this.$tabline  = $container.find('.tabline')
        this.$innerLine =  $container.find('.innerLine')
        this.init()
      }
      MyTab.prototype.init = function () {
        this.updateIndex()
        this.initEvents()
      }
      MyTab.prototype.barLeft = function () {
        var count = this.$tabitems.length
        var left = this.options.currentIndex * (100 / count)
        return left + 10 +'%'
      }

      MyTab.prototype.barRight = function () {
        var count = this.$tabitems.length
        var right = (count - this.options.currentIndex - 1) * (100 / count)
        return right + 10 +'%'
      }
      MyTab.prototype.barStyle = function () {
        var commonStyle = {
          left: this.barLeft(),
          right: this.barRight(),
          display: 'block',
          height: this.options.lineHeight,
          bottom: this.options.barDistance
        }
        if (this.options.customBarWidth) {
          commonStyle.background = 'transparent'
          this.$innerLine.show()
        } else {
          this.$innerLine.hide()
        }
        return commonStyle
      }
      MyTab.prototype.updateIndex = function () {
        for(var i = 0; i < this.$tabitems.length; i++) {
          if($(this.$tabitems[i]).hasClass('active')) {
            this.options.lastIndex = this.options.currentIndex
            this.options.currentIndex = i
            var direction = this.options.currentIndex > this.options.lastIndex ? 'forward' : 'backward'
            var removeCls = direction == 'forward' ? 'backward' : 'forward'
            this.$tabline.css(this.barStyle()).removeClass(removeCls).addClass(direction)
            this.options.tabChange(this.options.currentIndex, this.options.lastIndex)
            // $('.innerLine').css(this.innerBarStyle())
          }
        }
      }
      MyTab.prototype.initEvents = function () {
        var that = this
        this.$tabitems.click(function () {
          $(this).addClass('active').siblings().removeClass('active')
          that.updateIndex()
        })
      }
      MyTab.prototype.innerBarStyle = function () {
        return {
          width: this.options.customBarWidth(this.options.currentIndex),
          background: this.options.activeColor
        }
      }
      w.MyTab = MyTab
    })(window,jQuery)
  </script>


  <script>
    new MyTab({
      container: '.lbtab',
      barDistance: 24 / 50+'rem',
      tabChange: function (index) {
        console.log(index)
      }
    })
  </script>


</body>
</html>


©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 可能相遇就是个错误。当m小姐第一次遇到徐先生时,还不知道他们会有这么多故事,也是只是在ktv一次小小的邂逅。 m小...
    沫瑾裳阅读 147评论 1 0
  • 在遥远的A国有个小男孩叫爱发明,他为什么会叫爱发明呢?是因为他们国家很多的东西都是他发明的,所以他的名字叫作...
    宗有阅阅读 285评论 1 1
  • 平凡,是生活的底色 每个人都有属于自己独一无二的生活。 有的人,每天无忧无虑,花不完的钱,找不完的乐子,过着荣华富...
    d6129f30134b阅读 2,005评论 2 5
  • 关于PS冷知识的发现和运用基本上都是来源于平时的使用,年头多了也就掌握了不少(咳咳……),不过犹豫的原因是至今仍然...
    中华气死猫o阅读 244评论 0 5
  • 今天上午都是数学课,上到第三节下课的时候我们不想上数学课了就给数学老师说想上音乐课,数学老师答应了,同学们都...
    温柔了岁月_ai阅读 168评论 0 0