123456789101112131415161718192021222324252627282930313233343536373839404142434445 |
- import { ref, computed, nextTick } from "vue";
- export const useScroll = () => {
- const scrollRef = ref(null);
- const targeScrolltDom = computed(() => scrollRef.value.targetScrollDom);
- const scrollToTop = async () => {
- await nextTick();
- targeScrolltDom.value.scrollTo({
- top: 0,
- left: 0,
- behavior: 'smooth'
- });
- };
- const scrollToBottom = async () => {
- await nextTick();
- if (scrollRef.value) {
- targeScrolltDom.value.scrollTo({
- top: targeScrolltDom.value.scrollHeight,
- left: 0,
- behavior: 'smooth'
- });
- }
- }
- const scrollToBottomIfAtBottom = async () => {
- await nextTick()
- if (scrollRef.value) {
- const threshold = 100;
- const distanceToBottom = targeScrolltDom.value.scrollHeight - targeScrolltDom.value.scrollTop - targeScrolltDom.value.clientHeight;
- if (distanceToBottom <= threshold) {
- targeScrolltDom.value.scrollTop = targeScrolltDom.value.scrollHeight;
- }
- }
- }
- return {
- scrollRef,
- scrollToTop,
- scrollToBottom,
- scrollToBottomIfAtBottom
- }
- }
|