源代码地址
version:element-plus 1.0.1-beta.0
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18<template> <transition name="el-fade-in"> <div v-if="visible" :style="{ 'right': styleRight, 'bottom': styleBottom }" class="el-backtop" @click.stop="handleClick" > <slot> <i class="el-icon-caret-top"></i> </slot> </div> </transition> </template>
引入的函数
复制代码
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
31interface EventListenerObject { handleEvent(evt: Event): void; } declare type EventListenerOrEventListenerObject = EventListener | EventListenerObject; export const on = function( element: HTMLElement | Document | Window, event: string, handler: EventListenerOrEventListenerObject, useCapture = false, ): void { if (element && event && handler) { element.addEventListener(event, handler, useCapture) } } export const off = function( element: HTMLElement | Document | Window, event: string, handler: EventListenerOrEventListenerObject, ): void { if (element && event && handler) { element.removeEventListener(event, handler, false) } } export const easeInOutCubic = (value: number): number => value < 0.5 ? cubic(value * 2) / 2 : 1 - cubic((1 - value) * 2) / 2
复制代码
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
101
102
103
104
105
106
107<script lang="ts"> import { defineComponent, ref, computed, onMounted, onBeforeUnmount } from 'vue' import throttle from 'lodash/throttle' import { on, off } from '@element-plus/utils/dom' import { easeInOutCubic } from '@element-plus/utils/animation' // 传入props类型限制 interface IElBacktopProps { visibilityHeight: number // 滚动高度达到此参数值才出现 target: string // 触发滚动的对象 一般指向页面整体容器的类名 right: number // 控制其显示位置, 距离页面右边距 bottom: number // 控制其显示位置, 距离页面底部距离 } export default defineComponent({ name: 'ElBacktop', props: { visibilityHeight: { type: Number, default: 200, }, target: { type: String, default: '', }, right: { type: Number, default: 40, }, bottom: { type: Number, default: 40, }, }, emits: ['click'], setup(props: IElBacktopProps, ctx) { const el = ref(null) const container = ref(null) const visible = ref(false) const styleBottom = computed(() => `${props.bottom}px`) const styleRight = computed(() => `${props.right}px`) const scrollToTop = () => { const beginTime = Date.now() const beginValue = el.value.scrollTop // 没有就模拟帧函数 const rAF = window.requestAnimationFrame || (func => setTimeout(func, 16)) const frameFunc = () => { const progress = (Date.now() - beginTime) / 500 // 判断是否隔了500ms if (progress < 1) { el.value.scrollTop = beginValue * (1 - easeInOutCubic(progress)) // 继续调用 rAF(frameFunc) } else { el.value.scrollTop = 0 } } rAF(frameFunc) } // 控制是否显示 按钮 const onScroll = () => { visible.value = el.value.scrollTop >= props.visibilityHeight } // 点击触发的回调 const handleClick = event => { scrollToTop() ctx.emit('click', event) // 回调 } // 节流触发 const throttledScrollHandler = throttle(onScroll, 300) // 挂载时 onMounted(() => { container.value = document el.value = document.documentElement if (props.target) { // el 指向 找到的元素 el.value = document.querySelector(props.target) if (!el.value) { // 没找到 丢出 错误 throw new Error(`target is not existed: ${props.target}`) } container.value = el.value } // 事件监听 scroll 节流 on(container.value, 'scroll', throttledScrollHandler) }) // 页面销毁之前 onBeforeUnmount(() => { off(container.value, 'scroll', throttledScrollHandler) }) return { el, container, visible, styleBottom, styleRight, handleClick, } }, }) </script>
最后
以上就是忧郁皮卡丘最近收集整理的关于element-ui element-plus backtop - 分析的全部内容,更多相关element-ui内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复