ruoyi-plus-soybean/src/components/custom/better-scroll.vue

54 lines
1.3 KiB
Vue
Raw Normal View History

2022-01-20 00:56:59 +08:00
<script setup lang="ts">
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';
defineOptions({ name: 'BetterScroll' });
2022-01-20 00:56:59 +08:00
interface Props {
2023-11-17 08:45:00 +08:00
/**
* BetterScroll options
2023-12-14 21:45:29 +08:00
*
2023-11-17 08:45:00 +08:00
* @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>();
2024-03-24 03:02:08 +08:00
const bsWrapper = ref<HTMLElement>();
2022-01-20 00:56:59 +08:00
const bsContent = ref<HTMLElement>();
2024-03-24 03:02:08 +08:00
const { width: wrapWidth } = useElementSize(bsWrapper);
2023-11-17 08:45:00 +08:00
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() {
2024-03-24 03:02:08 +08:00
if (!bsWrapper.value) return;
instance.value = new BScroll(bsWrapper.value, props.options);
2022-01-20 00:56:59 +08:00
}
2023-11-17 08:45:00 +08:00
// refresh BS when scroll element size changed
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>
2023-11-17 08:45:00 +08:00
<template>
2024-03-24 03:02:08 +08:00
<div ref="bsWrapper" class="h-full text-left">
2023-11-17 08:45:00 +08:00
<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>