2021-10-30 17:27:56 +08:00
|
|
|
|
<template>
|
2021-11-04 23:11:20 +08:00
|
|
|
|
<div ref="scrollbar" class="wh-full text-left">
|
2021-11-07 01:23:01 +08:00
|
|
|
|
<div ref="scrollbarContent" class="inline-block" :class="{ 'h-full': !isScrollY }">
|
2021-10-30 17:27:56 +08:00
|
|
|
|
<slot></slot>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
</template>
|
|
|
|
|
|
|
|
|
|
<script setup lang="ts">
|
2021-11-07 01:23:01 +08:00
|
|
|
|
import { ref, computed, watch, onMounted } from 'vue';
|
2021-10-30 17:27:56 +08:00
|
|
|
|
import BScroll from '@better-scroll/core';
|
|
|
|
|
import type { Options } from '@better-scroll/core';
|
|
|
|
|
import { useElementSize } from '@vueuse/core';
|
|
|
|
|
|
2021-11-07 01:23:01 +08:00
|
|
|
|
interface Props {
|
|
|
|
|
/** better-scroll的配置: https://better-scroll.github.io/docs/zh-CN/guide/base-scroll-options.html */
|
|
|
|
|
options?: Options;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const props = withDefaults(defineProps<Props>(), {
|
|
|
|
|
options: undefined
|
2021-10-30 17:27:56 +08:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const scrollbar = ref<HTMLElement | null>(null);
|
|
|
|
|
const bsInstance = ref<BScroll | null>(null);
|
|
|
|
|
const scrollbarContent = ref<HTMLElement | null>(null);
|
2021-11-07 01:23:01 +08:00
|
|
|
|
const isScrollY = computed(() => Boolean(props.options?.scrollY));
|
2021-10-30 17:27:56 +08:00
|
|
|
|
|
|
|
|
|
function initBetterScroll() {
|
|
|
|
|
bsInstance.value = new BScroll(scrollbar.value!, props.options);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 滚动元素发生变化,刷新BS
|
|
|
|
|
const { width, height } = useElementSize(scrollbarContent);
|
|
|
|
|
watch([() => width.value, () => height.value], () => {
|
|
|
|
|
if (bsInstance.value) {
|
|
|
|
|
bsInstance.value.refresh();
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
onMounted(() => {
|
|
|
|
|
initBetterScroll();
|
|
|
|
|
});
|
|
|
|
|
</script>
|
|
|
|
|
<style scoped></style>
|