2022-01-20 00:56:59 +08:00
|
|
|
<script setup lang="ts">
|
2022-08-10 21:31:59 +08:00
|
|
|
import { computed, onMounted, ref, watch } from 'vue';
|
2022-01-20 00:56:59 +08:00
|
|
|
import { useElementSize } from '@vueuse/core';
|
|
|
|
import BScroll from '@better-scroll/core';
|
|
|
|
import type { Options } from '@better-scroll/core';
|
|
|
|
|
2022-07-10 14:02:00 +08:00
|
|
|
defineOptions({ name: 'BetterScroll' });
|
|
|
|
|
2022-01-20 00:56:59 +08:00
|
|
|
interface Props {
|
2023-11-17 08:45:00 +08:00
|
|
|
/**
|
|
|
|
* BetterScroll options
|
|
|
|
* @link https://better-scroll.github.io/docs/zh-CN/guide/base-scroll-options.html
|
|
|
|
*/
|
2022-01-20 00:56:59 +08:00
|
|
|
options: Options;
|
|
|
|
}
|
|
|
|
|
|
|
|
const props = defineProps<Props>();
|
|
|
|
|
|
|
|
const bsWrap = ref<HTMLElement>();
|
|
|
|
const bsContent = ref<HTMLElement>();
|
2023-11-17 08:45:00 +08:00
|
|
|
const { width: wrapWidth } = useElementSize(bsWrap);
|
|
|
|
const { width, height } = useElementSize(bsContent);
|
|
|
|
|
|
|
|
const instance = ref<BScroll>();
|
2022-01-20 00:56:59 +08:00
|
|
|
const isScrollY = computed(() => Boolean(props.options.scrollY));
|
|
|
|
|
|
|
|
function initBetterScroll() {
|
|
|
|
if (!bsWrap.value) return;
|
|
|
|
instance.value = new BScroll(bsWrap.value, props.options);
|
|
|
|
}
|
|
|
|
|
2023-11-17 08:45:00 +08:00
|
|
|
// refresh BS when scroll element size changed
|
2022-02-07 15:46:35 +08:00
|
|
|
watch([() => wrapWidth.value, () => width.value, () => height.value], () => {
|
2023-11-17 08:45:00 +08:00
|
|
|
instance.value?.refresh();
|
2022-01-20 00:56:59 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
onMounted(() => {
|
|
|
|
initBetterScroll();
|
|
|
|
});
|
|
|
|
|
|
|
|
defineExpose({ instance });
|
|
|
|
</script>
|
2022-05-28 12:30:17 +08:00
|
|
|
|
2023-11-17 08:45:00 +08:00
|
|
|
<template>
|
|
|
|
<div ref="bsWrap" class="h-full text-left">
|
|
|
|
<div ref="bsContent" class="inline-block" :class="{ 'h-full': !isScrollY }">
|
|
|
|
<slot></slot>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</template>
|
|
|
|
|
2022-01-20 00:56:59 +08:00
|
|
|
<style scoped></style>
|