-
-
Notifications
You must be signed in to change notification settings - Fork 625
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
259 additions
and
71 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
<template> | ||
<div class="var-back-top" :class="[show ? 'var-back-top--active' : null]" @click.stop="click"> | ||
<slot> | ||
<var-button type="primary" round class="var-back-top__button"> | ||
<var-icon name="chevron-up" /> | ||
</var-button> | ||
</slot> | ||
</div> | ||
</template> | ||
<script lang="ts"> | ||
import { defineComponent, ref, Ref, onMounted, onBeforeUnmount } from 'vue' | ||
import Button from '../button' | ||
import Icon from '../icon' | ||
import { props } from './props' | ||
import { isString, easeInOutCubic, throttle } from '../utils/shared' | ||
import { getScrollTop, requestAnimationFrame } from '../utils/elements' | ||
export default defineComponent({ | ||
name: 'VarBackTop', | ||
components: { | ||
[Button.name]: Button, | ||
[Icon.name]: Icon, | ||
}, | ||
props, | ||
setup(props) { | ||
const element: Ref<Element | null> = ref(null) | ||
const show: Ref<boolean> = ref(false) | ||
const click = () => { | ||
props.onClick?.() | ||
const top = getScrollTop(element.value as Element) | ||
const startTime = Date.now() | ||
const frameFunc = () => { | ||
const progress = (Date.now() - startTime) / props.duration | ||
if (progress < 1) { | ||
;(element.value as Element).scrollTop = top * (1 - easeInOutCubic(progress)) | ||
requestAnimationFrame(frameFunc) | ||
} else { | ||
;(element.value as Element).scrollTop = 0 | ||
} | ||
} | ||
requestAnimationFrame(frameFunc) | ||
} | ||
const scroll = () => { | ||
show.value = getScrollTop(element.value as Element) >= +props.visibilityHeight | ||
} | ||
const throttleScroll = throttle(scroll, 200) | ||
const getElement = () => { | ||
if (!isString(props.target)) throw Error('[Varlet] BackTop: type of prop "target" should be a string') | ||
const el = document.querySelector(props.target) | ||
if (!el) throw Error('[Varlet] BackTop: "target" should be a selector') | ||
return el | ||
} | ||
onMounted(() => { | ||
element.value = getElement() | ||
if (!element.value) return | ||
element.value.addEventListener('scroll', throttleScroll) | ||
}) | ||
onBeforeUnmount(() => { | ||
if (!element.value) return | ||
element.value.removeEventListener('scroll', throttleScroll) | ||
}) | ||
return { | ||
show, | ||
click, | ||
} | ||
}, | ||
}) | ||
</script> | ||
|
||
<style lang="less"> | ||
.var { | ||
&-back-top { | ||
position: fixed; | ||
right: 50px; | ||
bottom: 50px; | ||
transform: scale(0); | ||
transition: 0.3s cubic-bezier(0.25, 0.8, 0.5, 1); | ||
&__button { | ||
width: 54px; | ||
height: 54px; | ||
} | ||
&--active { | ||
transform: scale(1); | ||
} | ||
} | ||
} | ||
</style> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
const BackTop = require('../../../cjs/back-top').default | ||
const { render } = require('@testing-library/vue') | ||
|
||
test('test backTop', async () => { | ||
const wrapper = render(BackTop) | ||
console.log(wrapper) | ||
}) |
Empty file.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
<template> | ||
<div class="var-back-top__example"> | ||
<div> | ||
<div v-for="list in lists" :key="list">Scroll to bottom {{ list }}</div> | ||
<var-back-top target=".var-back-top__example" visibility-height="100" :duration="300" @click="test" /> | ||
</div> | ||
</div> | ||
</template> | ||
|
||
<script> | ||
import { defineComponent } from 'vue' | ||
import BackTop from '..' | ||
const lists = [...Array(100).keys()] | ||
export default defineComponent({ | ||
name: 'BackTopExample', | ||
components: { | ||
[BackTop.name]: BackTop, | ||
}, | ||
setup() { | ||
const test = () => { | ||
console.log('click') | ||
} | ||
return { | ||
test, | ||
lists, | ||
} | ||
}, | ||
}) | ||
</script> | ||
|
||
<style scoped> | ||
.var-back-top__example { | ||
height: calc(100vh - 130px); | ||
overflow: auto; | ||
margin-top: 18px; | ||
} | ||
</style> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { App } from 'vue' | ||
import BackTop from './BackTop.vue' | ||
|
||
BackTop.install = function (app: App) { | ||
app.component(BackTop.name, BackTop) | ||
} | ||
|
||
export default BackTop |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
export const props = { | ||
visibilityHeight: { | ||
type: [Number, String], | ||
default: 200, | ||
}, | ||
duration: { | ||
type: Number, | ||
default: 300, | ||
}, | ||
target: { | ||
type: String, | ||
}, | ||
onClick: { | ||
type: Function, | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.