用js写一个自定义的滚动条

在一些业务场景下,默认滚动条样式不符合业务需求,且用css并不好改,此时需要用js,自己写一个滚动条

滚动条组件 tabScroll.vue

<script>
import { computed, onUpdated, reactive, ref } from "vue";
export default {
  props: {
    tabScrollLeft: {
      type: Number,
      default: () => 0,
    },
  },
  setup(props) {
    const tabScrollLeft = computed(() => {
      return props.tabScrollLeft;
    });

    return {
      tabScrollLeft,
    };
  },
};
</script>
<template>
  <div class="scroll-thumb" id="thumb">
    <div
      class="scroll-slider"
      :style="{
        left: tabScrollLeft * 48 + 'px',
      }"
      ref="scrollElement"
    ></div>
  </div>
</template>
<style lang="scss" scoped>
.scroll-thumb {
  width: 96px;
  height: 4px;
  border-radius: 16px;
  background: #d9d9d9;
  position: relative;
  .scroll-slider {
    position: absolute;
    width: 48px;
    background: #001e8d;
    height: 100%;
    border-radius: 16px;
    // transition: ease 0.1s;
  }
}
</style>

运用到业务组件list.vue中:

<script>
	import tabScroll from '@/components/tabScroll.vue'
	import { ref, defineComponent, onMounted, onUnmounted, reactive } from 'vue'
	import { throttle } from 'underscore'
	export default defineComponent({
		 components: { tabScroll },
	})
	setup() {
	 	const state = reactive({
            isScroll: false,
        })
		const tabScrollLeft = ref(0)
		const scrollListenerHandler = throttle(() => {
            if (window.scrollY > 10) {
                state.isScroll = true
            } else {
                state.isScroll = false
            }
        }, 100)
		const scrollTabListener = () => {
            setTimeout(() => {
                let box = document.querySelector('.van-tabs__nav')
                if (box) {
                    tabScrollLeft.value = box.scrollLeft / 490
                    active.value = store.state.currentTab

                    box.addEventListener('scroll', function (event) {
                        const scrollLeft = event.target.scrollLeft
                        tabScrollLeft.value = scrollLeft / 490
                    })
                }
            }, 100)
        }
         onMounted(() => {
            window.addEventListener('scroll', scrollListenerHandler)
            scrollTabListener()
        })
         onUnmounted(() => {
            window.removeEventListener('scroll', scrollListenerHandler)
        })
        return {
			  tabScrollLeft,
		}
	}
</script>
<template>
	 <van-tabs
            id="goods-type-wrapper"
            class="goods-type-wrapper"
            v-model:active="active"
            color="#001e8d"
            title-active-color="#001e8d"
            @click-tab="onClickTab"
            :class="{ active: state.isScroll, unActive: !state.isScroll }"
        >
            <van-tab v-for="(item, index) in tablists" :name="item.goodsTypeID" :key="item.goodsTypeID">
                <template #title>
                    <div class="goods-type-list">
                        <div class="goods-type-list-icon" :class="{ active: state.isScroll }">
                            <div class="img_box">
                                <img :src="iconList[index]" alt="" />
                            </div>
                        </div>
                        <div class="goods-type-list-name">{{ item.goodsType }}</div>
                    </div>
                </template>
                <div class="no-data" v-if="isNoData">
                    <img src="@/assets/images/bookPage/noDataIcon.png" alt="" />
                    <div>{{ $t('common.noData') }}</div>
                </div>
                <div class="tab-scroll" v-if="!state.isScroll">
                    <tab-scroll :tabScrollLeft="tabScrollLeft"></tab-scroll>
                </div>
            </van-tab>
        </van-tabs>
</template>
.tab-scroll {
    position: fixed;
    top: 128px;
    z-index: 100;
    left: calc(50% - 48px);
}
  • 5
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值