vue滑动tabber居中显示

1.html

<div class="scroll-title">
   <div class="list" ref="viewBox">
    <ul>
      <li :class="{'select-tab' : tabIndex == classIndex}" v-for="(classItem, classIndex) in productClassList" :key="classIndex" @click="typeClick(classItem.product_class_id, classIndex)">{{classItem.name}}</li>
    </ul>
   </div>
  <div class="line"></div>
  <div class="mine-course-wrap" @click="mineCourse()">我的课程</div>
</div>

2.初始化bar显示

let that = this;
setTimeout(()=> {
   that.$refs.viewBox.scrollLeft = that.$refs.viewBox.scrollWidth / this.productClassList.length * (that.tabIndex - 1);
}, 500);
this.$refs.viewBox.addEventListener('scroll', this.scroll);

3.点击事件

scroll() {
   this.scrollLeft = document.documentElement.scrollLeft || document.body.scrollLeft;
},
// 点击type
typeClick(e, index) {
  this.tabIndex = index;
  store.set({'tab_index': this.tabIndex})
  this.productClassId = e;
  this.getProductListFun(this.productClassId)
  if (index == 0 || index == 1) {
    this.$refs.viewBox.scrollLeft = 0;
  }
  if (index >= 2) {
    this.$refs.viewBox.scrollLeft = this.$refs.viewBox.scrollWidth / this.productClassList.length * (index - 1);
  } 
},

4.css

.scroll-title {
    width: 7rem;
    height: 0.8rem;
    padding: 0 0.25rem; 
    background-color: #FFFFFF;
    position: -webkit-sticky;
    position: sticky;
    top: 0.87rem;
    padding-bottom: 1px; 
    border-bottom: 0.01rem solid #ddd;
  }
  .list {
    width: 5.5rem;
    height: 0.8rem;
    overflow-x: scroll;
    overflow-y: hidden;
    float: left;
    box-sizing: border-box;
    -webkit-overflow-scrolling : touch;
    -ms-overflow-style: none;
    overflow: -moz-scrollbars-none; 
    padding-bottom: 0.9rem;
    ::-webkit-scrollbar { width: 0 !important }
    ul {
      font-size: 0.3rem;
      list-style: none;
      height: 0.8rem;
      line-height: 0.8rem;
      white-space: nowrap;
      li {
        padding: 0 0.2rem;
        display: inline-block;
      }
    }
  }
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 1、通过CocoaPods安装项目名称项目信息 AFNetworking网络请求组件 FMDB本地数据库组件 SD...
    阳明AI阅读 16,056评论 3 119
  • 很长一段时间来,我总是会跟不太了解我的人介绍自己说:我的学业和事业相对较顺,小学保送初中,初中保送高中,大学保送研...
    Vivian爱生活爱分享阅读 1,828评论 0 0
  • 站在秋的路口, 依偎秋的怀抱, 看一片落叶渲染了秋色, 看一季落花沧桑了流年。 冷秋、冷晨、 冷霜、冷落叶; 暖情...
    DreamOn_d8cf阅读 5,323评论 1 3

友情链接更多精彩内容