Skip to content

useScrollToBottom

Introduction

滚动到底部并执行回调函数

Basic Usage

ts
import { useScrollToBottom } from '@flypeng/tool/browser';

const touchBottomInstance = useScrollToBottom(window, () => {
  console.log('Container 滚动到底部并执行回调函数');
});

// Stop to listen scroll of selector
touchBottomInstance.stop();

Type Declaration

ts
declare class TouchBottomClass {
  private isEnable;
  private isEnd;
  private scrollCallback;
  private callback;
  private selector;
  constructor(callback: Function, selector: Element);
  private start;
  private end;
  stop(): void;
}
/**
 * 滚动到底部并执行回调函数
 * @param selector
 * @param callback
 * @returns
 */
declare function useScrollToBottom(selector: Element, callback: Function): TouchBottomClass;

Online Demo

useScrollToBottom
滚动到底部并执行回调函数
Scroll Container (See Console)

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

<template>
  <div class="scroll-container">
    <h5>Scroll Container (See Console)</h5>
    <section ref="scrollElement">
      <p v-for="item in 100">{{ item }}</p>
    </section>
  </div>
</template>

<script lang="ts" setup>
import { useScrollToBottom, TouchBottomClass } from '@flypeng/tool/browser';
import { onMounted, onUnmounted, ref } from 'vue';

const scrollElement = ref<Element | null>(null);
let touchBottomInstance: TouchBottomClass;

onMounted(() => {
  if (scrollElement.value) {
    touchBottomInstance = useScrollToBottom(scrollElement.value, () => {
      console.log('Container 滚动到底部并执行回调函数');
    });
  }
});

onUnmounted(() => {
  touchBottomInstance.stop();
});
</script>

<style scoped>
.scroll-container > h5 {
  display: inline-block;
  position: sticky;
  left: 50%;
  transform: translateX(-50%);
  top: 10px;
  color: var(--vp-c-brand);
  font-size: 16px;
  font-weight: 600;
}

.scroll-container > section {
  height: 400px;
  border: 1px solid var(--component-preview-border);
  border-radius: 4px;
  overflow-x: hidden;
  overflow-y: scroll;
  text-align: center;
  margin-top: 10px;
}
</style>

Released under the MIT License.