refactor: ♻️ 索引栏支持侧边栏滑动时同步滚动列表

This commit is contained in:
xuqingkai 2024-05-20 13:25:11 +08:00
parent 19b9f19646
commit ab47b68b87
6 changed files with 19 additions and 58 deletions

View File

@ -1,7 +1,7 @@
/* /*
* @Author: weisheng * @Author: weisheng
* @Date: 2023-07-27 10:26:09 * @Date: 2023-07-27 10:26:09
* @LastEditTime: 2024-05-07 17:05:08 * @LastEditTime: 2024-05-20 13:18:53
* @LastEditors: weisheng * @LastEditors: weisheng
* @Description: * @Description:
* @FilePath: \wot-design-uni\docs\.vitepress\config.mts * @FilePath: \wot-design-uni\docs\.vitepress\config.mts
@ -251,7 +251,7 @@ export default defineConfig({
link: "/component/backtop", link: "/component/backtop",
text: "Backtop 回到顶部" text: "Backtop 回到顶部"
}, { }, {
link: "/component/indexBar", link: "/component/index-bar",
text: "IndexBar 索引栏" text: "IndexBar 索引栏"
}] }]
}, { }, {

View File

@ -13,10 +13,10 @@
```vue ```vue
<template> <template>
<view class="wraper"> <view class="wraper">
<wd-index-bar> <wd-index-bar sticky>
<view v-for="item in data" :key="item.index" class="city-wrap"> <view v-for="item in data" :key="item.index">
<wd-index-anchor :index="item.index" /> <wd-index-anchor :index="item.index" />
<view v-for="city in item.data" class="city" :key="city">{{ city }}</view> <wd-cell border clickable v-for="city in item.data" :key="city" :title="city" @click="handleClick(item.index, city)"></wd-cell>
</view> </view>
</wd-index-bar> </wd-index-bar>
</view> </view>
@ -117,26 +117,11 @@ const data = ref([
</script> </script>
<style lang="scss"> <style lang="scss">
.city {
padding: 20px;
font-size: 14px;
color: black;
border-bottom: 1px solid #ddd;
}
.wraper { .wraper {
height: calc(100vh - var(--window-top)); height: calc(100vh - var(--window-top));
height: calc(100vh - var(--window-top) - constant(safe-area-inset-bottom)); height: calc(100vh - var(--window-top) - constant(safe-area-inset-bottom));
height: calc(100vh - var(--window-top) - env(safe-area-inset-bottom)); height: calc(100vh - var(--window-top) - env(safe-area-inset-bottom));
} }
.wot-theme-dark {
.city {
color: white;
border-bottom-color: #666;
}
}
</style> </style>
``` ```

View File

@ -2,9 +2,9 @@
<page-wraper> <page-wraper>
<view class="wraper"> <view class="wraper">
<wd-index-bar sticky> <wd-index-bar sticky>
<view v-for="item in data" :key="item.index" class="city-wrap"> <view v-for="item in data" :key="item.index">
<wd-index-anchor :index="item.index" /> <wd-index-anchor :index="item.index" />
<view v-for="city in item.data" class="city" :key="city" @click="handleClick(item.index, city)">{{ city }}</view> <wd-cell border clickable v-for="city in item.data" :key="city" :title="city" @click="handleClick(item.index, city)"></wd-cell>
</view> </view>
</wd-index-bar> </wd-index-bar>
</view> </view>
@ -111,23 +111,9 @@ function handleClick(index: string, city: string) {
</script> </script>
<style lang="scss"> <style lang="scss">
.city {
padding: 20px;
font-size: 14px;
color: black;
border-bottom: 1px solid #ddd;
}
.wraper { .wraper {
height: calc(100vh - var(--window-top)); height: calc(100vh - var(--window-top));
height: calc(100vh - var(--window-top) - constant(safe-area-inset-bottom)); height: calc(100vh - var(--window-top) - constant(safe-area-inset-bottom));
height: calc(100vh - var(--window-top) - env(safe-area-inset-bottom)); height: calc(100vh - var(--window-top) - env(safe-area-inset-bottom));
} }
.wot-theme-dark {
.city {
color: white;
border-bottom-color: #666;
}
}
</style> </style>

View File

@ -21,21 +21,7 @@
// #endif // #endif
@include b(index-anchor) { @include b(index-anchor) {
background-color: $-color-gray-3; background-color: $-color-gray-2;
padding: 10px;
font-size: 14px;
color: $-color-title;
@include when(sticky){
position: sticky;
top: 0;
left: 0;
z-index: 1;
}
}
@include b(index-anchor) {
background-color: $-color-gray-3;
padding: 10px; padding: 10px;
font-size: 14px; font-size: 14px;
color: $-color-title; color: $-color-title;

View File

@ -1,12 +1,8 @@
<template> <template>
<!-- #ifdef MP-DINGTALK --> <!-- #ifdef MP-DINGTALK -->
<view :class="`wd-index-anchor-ding ${indexBar && indexBar.props.sticky && indexBar.anchorState.activeIndex === index ? 'is-sticky' : ''}`"> <view :class="`wd-index-anchor-ding ${isSticky ? 'is-sticky' : ''}`">
<!-- #endif --> <!-- #endif -->
<view <view :class="`wd-index-anchor ${isSticky ? 'is-sticky' : ''} ${customClass}`" :style="customStyle" :id="indexAnchorId">
:class="`wd-index-anchor ${indexBar && indexBar.props.sticky && indexBar.anchorState.activeIndex === index ? 'is-sticky' : ''} ${customClass}`"
:style="customStyle"
:id="indexAnchorId"
>
<slot> <slot>
{{ index }} {{ index }}
</slot> </slot>
@ -18,7 +14,7 @@
<script setup lang="ts"> <script setup lang="ts">
import { indexAnchorProps } from './type' import { indexAnchorProps } from './type'
import { onMounted, getCurrentInstance, ref } from 'vue' import { onMounted, getCurrentInstance, ref, computed } from 'vue'
import { indexBarInjectionKey } from '../wd-index-bar/type' import { indexBarInjectionKey } from '../wd-index-bar/type'
import { getRect, isDef, uuid } from '../common/util' import { getRect, isDef, uuid } from '../common/util'
import { useParent } from '../composables/useParent' import { useParent } from '../composables/useParent'
@ -31,6 +27,10 @@ const indexAnchorId = ref<string>(`indexBar${uuid()}`)
const { proxy } = getCurrentInstance()! const { proxy } = getCurrentInstance()!
const isSticky = computed(() => {
return indexBar && indexBar.props.sticky && indexBar.anchorState.activeIndex === props.index
})
function getInfo() { function getInfo() {
getRect(`#${indexAnchorId.value}`, false, proxy).then((res) => { getRect(`#${indexAnchorId.value}`, false, proxy).then((res) => {
if (isDef(indexBar)) { if (isDef(indexBar)) {

View File

@ -100,7 +100,11 @@ function getAnchorByPageY(pageY: number) {
function handleTouchMove(e: TouchEvent) { function handleTouchMove(e: TouchEvent) {
const clientY = e.touches[0].pageY const clientY = e.touches[0].pageY
if (state.activeIndex === getAnchorByPageY(clientY).index) {
return
}
state.activeIndex = getAnchorByPageY(clientY).index state.activeIndex = getAnchorByPageY(clientY).index
scrollTop.value = getAnchorByPageY(clientY).top - offsetTop
} }
function handleTouchEnd(e: TouchEvent) { function handleTouchEnd(e: TouchEvent) {