用jq實(shí)現(xiàn)一個(gè)tab欄

實(shí)現(xiàn)一個(gè)簡(jiǎn)單的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, // 當(dāng)前的=位置
        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>


?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 可能相遇就是個(gè)錯(cuò)誤。當(dāng)m小姐第一次遇到徐先生時(shí),還不知道他們會(huì)有這么多故事,也是只是在ktv一次小小的邂逅。 m小...
    沫瑾裳閱讀 155評(píng)論 1 0
  • 在遙遠(yuǎn)的A國(guó)有個(gè)小男孩叫愛發(fā)明,他為什么會(huì)叫愛發(fā)明呢?是因?yàn)樗麄儑?guó)家很多的東西都是他發(fā)明的,所以他的名字叫作...
    宗有閱閱讀 315評(píng)論 1 1
  • 平凡,是生活的底色 每個(gè)人都有屬于自己獨(dú)一無二的生活。 有的人,每天無憂無慮,花不完的錢,找不完的樂子,過著榮華富...
    d6129f30134b閱讀 2,054評(píng)論 2 5
  • 關(guān)于PS冷知識(shí)的發(fā)現(xiàn)和運(yùn)用基本上都是來源于平時(shí)的使用,年頭多了也就掌握了不少(咳咳……),不過猶豫的原因是至今仍然...
    中華氣死貓o閱讀 261評(píng)論 0 5
  • 今天上午都是數(shù)學(xué)課,上到第三節(jié)下課的時(shí)候我們不想上數(shù)學(xué)課了就給數(shù)學(xué)老師說想上音樂課,數(shù)學(xué)老師答應(yīng)了,同學(xué)們都...
    溫柔了歲月_ai閱讀 194評(píng)論 0 0

友情鏈接更多精彩內(nèi)容