-
Notifications
You must be signed in to change notification settings - Fork 491
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
提供了滚动到指定元素的示例代码 closed #4750
- Loading branch information
naturalshen
committed
Dec 24, 2024
1 parent
020f13a
commit 60d6131
Showing
5 changed files
with
101 additions
and
29 deletions.
There are no files selected for viewing
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 |
---|---|---|
@@ -1,18 +1,40 @@ | ||
<template> | ||
<t-list style="height: 300px" :scroll="{ type: 'virtual' }" | ||
><t-list-item v-for="(item, index) in listRef" :key="index"> | ||
<t-list-item-meta :image="imageUrl" title="列表标题" :description="item.content" /></t-list-item | ||
></t-list> | ||
<t-space direction="vertical"> | ||
<t-list | ||
ref="list" | ||
style="height: 300px" | ||
:scroll="{ type: 'virtual', rowHeight: 80, bufferSize: 10, threshold: 10 }" | ||
> | ||
<t-list-item v-for="(item, index) in listData" :key="`t${index + 1}`"> | ||
<t-list-item-meta :image="imageUrl" title="列表标题" :description="item.content" /> | ||
</t-list-item> | ||
</t-list> | ||
<t-space> | ||
<t-button @click="handleScroll">滚动到指定节点</t-button> | ||
</t-space> | ||
</t-space> | ||
</template> | ||
<script lang="ts" setup> | ||
import { ref } from 'vue'; | ||
import { ListItemMetaProps } from 'tdesign-vue-next'; | ||
const list = []; | ||
import { ref, onMounted } from 'vue'; | ||
import { ListItemMetaProps, ListInstanceFunctions } from 'tdesign-vue-next'; | ||
const list = ref<ListInstanceFunctions>(); // 用于存储对 t-list 的引用 | ||
const listData = ref([]); // 使用 ref 来存储列表数据 | ||
const imageUrl: ListItemMetaProps['image'] = 'https://tdesign.gtimg.com/site/avatar.jpg'; | ||
for (let i = 0; i < 3000; i++) { | ||
list.push({ | ||
content: `列表内容的描述性文字`, | ||
onMounted(() => { | ||
for (let i = 0; i < 3000; i++) { | ||
listData.value.push({ content: `第${i + 1}个列表内容的描述性文字` }); | ||
} | ||
}); | ||
const handleScroll = () => { | ||
// scroll 属性需要设置 rowHeight 参数 | ||
list.value?.scrollTo({ | ||
// 指定key滚动,即当前节点对应的唯一值,推荐使用 | ||
key: 't30', | ||
// 指定index滚动,如果存在多级嵌套,需要自己计算index | ||
// index: 100, | ||
}); | ||
} | ||
const listRef = ref(list); | ||
}; | ||
</script> |
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 |
---|---|---|
@@ -1,16 +1,40 @@ | ||
<template> | ||
<t-list style="height: 300px" :scroll="{ type: 'virtual' }" | ||
><t-list-item v-for="(item, index) in listRef" :key="index"> | ||
<t-list-item-meta :image="imageUrl" title="列表标题" :description="item.content" /></t-list-item | ||
></t-list> | ||
<t-space direction="vertical"> | ||
<t-list | ||
ref="list" | ||
style="height: 300px" | ||
:scroll="{ type: 'virtual', rowHeight: 80, bufferSize: 10, threshold: 10 }" | ||
> | ||
<t-list-item v-for="(item, index) in listData" :key="`t${index + 1}`"> | ||
<t-list-item-meta :image="imageUrl" title="列表标题" :description="item.content" /> | ||
</t-list-item> | ||
</t-list> | ||
<t-space> | ||
<t-button @click="handleScroll">滚动到指定节点</t-button> | ||
</t-space> | ||
</t-space> | ||
</template> | ||
|
||
<script setup> | ||
import { ref } from 'vue'; | ||
const list = []; | ||
import { ref, onMounted } from 'vue'; | ||
const list = ref(); // 用于存储对 t-list 的引用 | ||
const listData = ref([]); // 使用 ref 来存储列表数据 | ||
const imageUrl = 'https://tdesign.gtimg.com/site/avatar.jpg'; | ||
for (let i = 0; i < 3000; i++) { | ||
list.push({ content: `列表内容的描述性文字` }); | ||
} | ||
const listRef = ref(list); | ||
onMounted(() => { | ||
for (let i = 0; i < 3000; i++) { | ||
listData.value.push({ content: `第${i + 1}个列表内容的描述性文字` }); | ||
} | ||
}); | ||
const handleScroll = () => { | ||
// scroll 属性需要设置 rowHeight 参数 | ||
list.value?.scrollTo({ | ||
// 指定key滚动,即当前节点对应的唯一值,推荐使用 | ||
key: 't30', | ||
// 指定index滚动,如果存在多级嵌套,需要自己计算index | ||
// index: 100, | ||
}); | ||
}; | ||
</script> |
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
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
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