vue pc导航栏实现横向拖动滚动

vue yekong 1297℃

效果图

效果图

使用插件

npm i better-scroll --save

better-scroll使用文档

代码

<template>
  <div class="tabsbody">
    <div class="horizontal-scrollbar-container">
      <div class="scroll-wrapper" ref="scroll">
        <div class="scroll-content">
          <div class="scroll-item" v-for="(item,index) in list" :key="index">
            <div class="tabsItem" @click="getactive(index)" :class="{active:active==index}">
              <p class="cur"><span>{{ item.articleName }}</span>
                <img v-if="item.child.length" class="downimg" src="../../assets/icon_down.png" alt="">
              </p>
              <div class="line"></div>
            </div>
          </div>
        </div>
      </div>
    </div>
    <div class="tabs2">
      <div class="tabsItem">
        <p class="cur">2022年</p>
        <img class="downimg" src="../../assets/icon_down.png" alt="">
      </div>
    </div>
  </div>
</template>

<script>

import {getPublicVideoType} from "@/api/api/user";
import BScroll from '@better-scroll/core'
import Scrollbar from '@better-scroll/scroll-bar'

BScroll.use(Scrollbar)
export default {
  name: "tab",
  components: {},
  props: {
    // active: {
    //   type: Number,
    //   default() {
    //     return '';
    //   }
    // }
  },
  data() {
    return {
      active: 0,
      list: [],
      flag: false,
      downX: 0,
      scrollLeft: 0,
      scroll: null,
      enterpriseId: this.$route.query.enterpriseId,
    }
  },
  watch: {},

  mounted() {
    this.getPublicVideoType()
  },
  methods: {
    init2() {
      this.scroll = new BScroll(this.$refs.scroll, {
        scrollX: true,
        scrollY: false,
        click: true,
        probeType: 1,
        scrollbar: {
          fade: true,
          interactive: false,
          scrollbarTrackClickable: false,
          scrollbarTrackOffsetType: 'clickedPoint' // can use 'step'
        }
      })
      this.scroll.on('scrollEnd', () => {
        console.log('scrollEnd')
      })
      this.scroll.on('scrollStart', () => {
        console.log('scrollStart')
      })
      this.scroll.on('scroll', () => {
        console.log('scroll')
      })
    },
    getactive(e) {
      this.active = e
      this.$emit('getdata', this.list[this.active])
    },
    getPublicVideoType() {
      var that = this
      let params = {
        dateStr: 2022,
        enterpriseId: this.enterpriseId
      }
      getPublicVideoType(params).then(function (res) {
        that.list = res.result
        that.$emit('getdata', that.list[0])
        that.$nextTick(() => {
          that.init2()
        })
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.tabsbody {
  display: flex;
  justify-content: space-between;
  align-items: center;
  flex-wrap: nowrap;
  flex-direction: row;
  align-content: flex-start;
  border-bottom: 1px solid rgba(255, 255, 255, 1);
  height: 80px;
  width: 100%;

}

.tabs2 {
  display: flex;
  justify-content: flex-end;
  align-items: center;
  flex-wrap: nowrap;
  flex-direction: row;
  align-content: flex-start;

  .tabsItem {
    font-size: 22px;
    font-family: PingFangSC-Medium, PingFang SC;
    font-weight: 500;
    color: #FFFFFF;
    height: 80px;
    display: flex;
    justify-content: center;
    align-items: center;
    flex-wrap: nowrap;
    flex-direction: row;
    align-content: flex-start;

    p {
      font-size: 30px;
      font-family: PingFangSC-Medium, PingFang SC;
      font-weight: 500;
      color: #FFFFFF;
      line-height: 76px;
    }

    .line {
      width: 88px;
      height: 5px;
      border-radius: 3px;
      background: rgba(#FFB300, 0);
    }
  }

  .tabsItem.active {
    .line {
      background: #FFB300;
    }
  }
}

.items {
  display: inline-block;
}

.downimg {
  margin-left: 13px;
  width: 24px;
}
</style>
<style lang="scss">
.scroll-wrapper {
  position: relative;
  display: flex;
  align-content: center;
  white-space: nowrap;
  overflow: hidden;
  width: calc(100% - 120px);
  margin-bottom: -5px;
}

.scroll-content {
  display: inline-block;
  align-self: center;
}

.scroll-item {
  display: inline-block;


  .tabsItem {
    font-size: 30px;
    font-family: PingFangSC-Medium, PingFang SC;
    font-weight: 500;
    color: #FFFFFF;
    margin-right: 50px;
    height: 80px;
    flex: 1;
    flex-shrink: 0;
    width: auto;
    display: flex;
    justify-content: flex-start;
    align-items: center;
    flex-wrap: nowrap;
    flex-direction: column;
    align-content: flex-start;

    p {
      font-size: 30px;
      font-family: PingFangSC-Medium, PingFang SC;
      font-weight: 500;
      color: #FFFFFF;
      line-height: 76px;
      overflow: hidden;
      white-space: nowrap;
      text-overflow: ellipsis;
    }

    .line {
      width: 100%;
      height: 5px;
      border-radius: 3px;
      background: rgba(#FFB300, 0);
    }
  }

  .tabsItem.active {
    .line {
      background: #FFB300;
    }
  }
}
</style>
<style lang="scss">
.bscroll-indicator {
  height: 100%;
  width: 20px;
  border-radius: 6px;
  background-color: #4BAEFF !important;
}

</style>

喜欢 (0)