useScrollTo
适用于 Vue3
、Nuxt3
、HTML
使元素可自定义缓动
示例
纵向缓动

curiously including times despite stain now ack outmaneuver unbend past

how matriculate interestingly er breed notable excommunicate

questionably wrongly link woefully sans

fatal svelte metro denationalize baritone coast adventurous at throughout

outside variant beatboxer thorny sculpting humanise

commonly fail worthy derivative punctually fooey overcooked bravely strategy ew

how before unripe worried candidacy loudly enchanting

binge recipe hm identical huzzah aboard till quote among

abaft advanced anti posh exchange household yawningly punctually sample yard

since actual true redecorate sequestrate that ack into

minus whoever until sternly helplessly frugal frenetically the along supposing

generally decentralize bloomer unto bias because what party visualize naturalisation

kangaroo wonderfully weep restfully samurai rich

boohoo bony heifer polished before

yet stark when flashy harden onto troubled terminology but

fooey nourish sprinter positively helplessly that

supposing vitality till silk peek hmph interrogate greedily so

wherever puzzled sister amidst before opposite provided

yum generously along out onto when medium politicise pace pish

off flytrap meh glow apropos

between e-mail because paint toe seriously painfully famous yuck deposition

whoever absent unless suppurate dizzy opposite because

slob cauliflower wan kissingly hmph consequently bandanna bartender vein editor

next terraform when phooey if

blah grown stonework immigration faithfully whoever

considering boo correctly yieldingly highly why athwart pressure

apropos aw ah captain while relief lacquer spiff geez understated

deport gosh briefly step-sister whether instead innocently revive

well thanks stick finally college

pfft when along hm pro

gadzooks because expect grave gosh cautious

terraform clapboard and meh inasmuch whether

supposing joyously visible nor that into bah inside mid patriarch

glue fully modern minus bloat beyond double semiconductor

pish warped grief even faraway than vie barricade

tightly slot behind woefully astonishing enchanting

adjust idle onto gym overtake illiteracy overlooked dad

concrete an given material by astrakhan a

inasmuch except branch inferior into whether administration aha

until kinase treasure gee fooey hm undertaker hmph eventually cauliflower

uh-huh provided um freely zowie ashamed understated stable judicious why

disastrous vaguely sad qua foregather owlishly pro

yippee rarely zigzag elegantly till fishing forecast rehabilitate which mockingly

but sponsor thankfully gosh but verb

whoa before notwithstanding mass encrypt fooey wide-eyed safely youthfully hourly

amid office longingly courageously that yippee yuck ack seriously

wick silk hmph bah and round

amongst walkway plush scared hotdog trash care supposing

junior though as helpful burrow after

from alert whenever glance angrily
查看代码
<script setup lang="ts">
import { ref } from "vue";
import { faker } from "@faker-js/faker/locale/en";
import { useMessage } from "../../components/message";
import { useScrollTo, debounce } from "@pureadmin/utils";
const scrollRef = ref();
const { message } = useMessage();
// 加个防抖。连续点击,只会执行第一次点击事件,立即执行
const onTop = debounce(
() =>
useScrollTo({
el: scrollRef,
to: 0,
directions: "scrollTop",
duration: 2000,
callback: msg => {
msg === "滚动完毕"
? message.success("已滚动到顶部")
: message.info(msg);
}
}).start(),
1200,
true
);
const onBottom = debounce(
() =>
useScrollTo({
el: scrollRef,
to: scrollRef.value!.scrollHeight,
directions: "scrollTop",
duration: 2000,
callback: msg => {
msg === "滚动完毕"
? message.success("已滚动到底部")
: message.info(msg);
}
}).start(),
1200,
true
);
const onTopShortly = () => {
useScrollTo({ el: scrollRef, to: 0, directions: "scrollTop" }).start();
};
const onBottomShortly = () => {
useScrollTo({
el: scrollRef,
to: scrollRef.value!.scrollHeight,
directions: "scrollTop"
}).start();
};
// 整页滚动
const onPageTop = debounce(
() =>
useScrollTo({
el: document.documentElement,
to: 0,
directions: "scrollTop",
duration: 800,
callback: msg => {
msg === "滚动完毕"
? message.success("整页已滚动到顶部")
: message.info(msg);
}
}).start(),
1200,
true
);
const onPageBottom = debounce(
() =>
useScrollTo({
el: document.documentElement,
to: document.documentElement.scrollHeight,
directions: "scrollTop",
duration: 800,
callback: msg => {
msg === "滚动完毕"
? message.success("整页已滚动到底部")
: message.info(msg);
}
}).start(),
1200,
true
);
</script>
<template>
<naive-theme>
<div
ref="scrollRef"
class="w-full h-[335px] mt-3 overflow-y-scroll border-solid border-1 border-[var(--vp-c-divider)] rounded-lg"
>
<div v-for="(_, index) in 50" :key="index" class="my-[10px]">
<div class="flex items-center justify-start p-2 w-9/10 ml-3">
<img
class="rounded-lg w-[80px] mr-10"
:src="`https://xiaoxian521.github.io/hyperlink/avatar/400*400_${
index + 1
}.jpg`"
/>
<p>{{ faker.word.words({ count: { min: 5, max: 10 } }) }}</p>
</div>
</div>
</div>
<n-button tertiary size="small" class="mt-4" @click="onTop">
滚动到顶部
</n-button>
<n-button tertiary size="small" class="mt-4 ml-2" @click="onBottom">
滚动到底部
</n-button>
<br />
<n-button tertiary size="small" class="mt-2" @click="onTopShortly">
滚动到顶部(无缓动效果)
</n-button>
<n-button tertiary size="small" class="mt-2 ml-2" @click="onBottomShortly">
滚动到底部(无缓动效果)
</n-button>
<br />
<n-button tertiary size="small" class="mt-2" @click="onPageTop">
整页滚动到顶部
</n-button>
<n-button tertiary size="small" class="mt-2 ml-2" @click="onPageBottom">
整页滚动到底部
</n-button>
</naive-theme>
</template>
横向缓动

Shanon

Vivianne

Randall

Dalton

Ora

Wyatt

Clarabelle

Frederik

Dorthy

Bo

Mark

Destiney

Denis

Kyle

Clarissa

Kristopher

Frederic

Lorenza

Nickolas

Sedrick

Eric

Brown

Vernon

Ramona

Simone

Clementine

Jasper

Francesco

Maudie

Evalyn

Tia

Gonzalo

Sarai

Brice

Madelyn

Alysson

Amie

Kenneth

Brigitte

Kimberly

Berta

Lenna

Erin

Okey

Edna

Daphney

Kamron

Lempi

Jade

Grayce
查看代码
<script setup lang="ts">
import { ref } from "vue";
import { faker } from "@faker-js/faker/locale/en";
import { useMessage } from "../../components/message";
import { useScrollTo, debounce } from "@pureadmin/utils";
const scrollRef = ref();
const { message } = useMessage();
// 加个防抖。连续点击,只会执行第一次点击事件,立即执行
const onLeft = debounce(
() =>
useScrollTo({
el: scrollRef,
to: 0,
directions: "scrollLeft",
duration: 2000,
callback: msg => {
msg === "滚动完毕"
? message.success("已滚动到最左侧")
: message.info(msg);
}
}).start(),
1200,
true
);
const onRight = debounce(
() =>
useScrollTo({
el: scrollRef,
to: scrollRef.value!.scrollWidth,
directions: "scrollLeft",
duration: 2000,
callback: msg => {
msg === "滚动完毕"
? message.success("已滚动到最右侧")
: message.info(msg);
}
}).start(),
1200,
true
);
const onLeftShortly = () => {
useScrollTo({ el: scrollRef, to: 0, directions: "scrollLeft" }).start();
};
const onRightShortly = () => {
useScrollTo({
el: scrollRef,
to: scrollRef.value!.scrollWidth,
directions: "scrollLeft"
}).start();
};
</script>
<template>
<naive-theme>
<div
ref="scrollRef"
class="w-full my-3 overflow-x-scroll flex border-solid border-1 border-[var(--vp-c-divider)] rounded-lg"
>
<div
v-for="(_, index) in 50"
:key="index"
class="shrink-0 text-center px-[20px] pt-[28px]"
>
<img
class="rounded-lg w-[80px]"
:src="`https://xiaoxian521.github.io/hyperlink/avatar/400*400_${
index + 1
}.jpg`"
/>
<p>{{ faker.person.firstName() }}</p>
</div>
</div>
<n-button tertiary size="small" class="mt-2" @click="onLeft">
滚动到最左侧
</n-button>
<n-button tertiary size="small" class="mt-2 ml-2" @click="onRight">
滚动到最右侧
</n-button>
<br />
<n-button tertiary size="small" class="mt-2" @click="onLeftShortly">
滚动到最左侧(无缓动效果)
</n-button>
<n-button tertiary size="small" class="mt-2 ml-2" @click="onRightShortly">
滚动到最右侧(无缓动效果)
</n-button>
</naive-theme>
</template>
最简代码
整页滚动到顶部
<script setup lang="ts">
import { useScrollTo } from "@pureadmin/utils";
const onPageTop = () => {
useScrollTo({
el: document.documentElement,
to: 0,
directions: "scrollTop"
}).start();
};
</script>
<template>
<button @click="onPageTop">整页滚动到顶部</button>
</template>
API
参数
// 在此处配置参数
const {} = useScrollTo(options);
参数属性 | 必传 | 说明 | 类型 |
---|---|---|---|
el | 是 | 滚动对象的实例 | Ref<Element> /HTMLElement |
to | 是 | 滚动到某个位置 | number |
directions | 是 | 滚动方向 | scrollTop /scrollLeft |
duration | 否 | 滚动时长,单位毫秒。默认0 ,没有缓动效果 | number |
callback | 否 | 滚动完毕的回调,返回一个参数msg 。msg 等于滚动完毕 ,代表滚动完成;msg 等于无需滚动 ,代表当前已处于滚动目标位置,无需滚动 | (msg?:string) => void |
返回值、方法
返回值、方法 | 说明 | 类型 |
---|---|---|
start | 开始滚动 | () => void |
stop | 暂停滚动 | () => void |