Skip to content

Commit 3805882

Browse files
author
xuqingkai
committed
feat: ✨ sticky组件支持支付宝平台
1 parent 3d5d285 commit 3805882

File tree

4 files changed

+20
-26
lines changed

4 files changed

+20
-26
lines changed

src/pages/sticky/Index.vue

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<!--
22
* @Author: weisheng
33
* @Date: 2023-06-13 11:47:12
4-
* @LastEditTime: 2023-08-14 21:51:11
4+
* @LastEditTime: 2024-02-26 14:42:16
55
* @LastEditors: weisheng
66
* @Description:
77
* @FilePath: \wot-design-uni\src\pages\sticky\Index.vue
@@ -22,7 +22,7 @@
2222
<view class="demo-block">
2323
<view class="demo-title">吸顶距离</view>
2424
<view class="demo-container">
25-
<wd-sticky :offset-top="50" style="margin-left: 120px">
25+
<wd-sticky :offset-top="50" custom-style="margin-left: 120px">
2626
<wd-button>吸顶距离</wd-button>
2727
</wd-sticky>
2828
</view>

src/uni_modules/wot-design-uni/components/wd-resize/wd-resize.vue

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<template>
22
<view :class="`wd-resize ${customClass}`" :style="rootStyle">
33
<!--插槽需要脱离父容器文档流,防止父容器固宽固高,进而导致插槽大小被被父容器限制-->
4-
<view :class="`wd-resize__container ${customContainerClass}`">
4+
<view :id="resizeId" :class="`wd-resize__container ${customContainerClass}`">
55
<!--被监听的插槽-->
66
<slot />
77
<!--监听插槽变大-->
@@ -43,7 +43,7 @@ export default {
4343

4444
<script lang="ts" setup>
4545
import { computed, getCurrentInstance, onMounted, ref } from 'vue'
46-
import { addUnit, objToStyle } from '../common/util'
46+
import { addUnit, objToStyle, uuid } from '../common/util'
4747
4848
interface Props {
4949
customClass?: string
@@ -75,16 +75,12 @@ const rootStyle = computed(() => {
7575
let onScrollHandler = () => {}
7676
const { proxy } = getCurrentInstance() as any
7777
78+
const resizeId = ref<string>(`resize${uuid()}`)
7879
const emit = defineEmits(['resize'])
7980
8081
onMounted(() => {
81-
const query = uni
82-
.createSelectorQuery()
83-
// #ifndef MP-ALIPAY
84-
.in(proxy)
85-
// #endif
86-
.select('.wd-resize__container')
87-
.boundingClientRect()
82+
// 初始化数据获取
83+
const query = uni.createSelectorQuery().in(proxy).select(`#${resizeId.value}`).boundingClientRect()
8884
query.exec(([res]) => {
8985
// 闭包记录容器高度
9086
let lastHeight = res.height
@@ -94,13 +90,7 @@ onMounted(() => {
9490
width.value = lastWidth
9591
// 监听滚动事件
9692
onScrollHandler = () => {
97-
const query = uni
98-
.createSelectorQuery()
99-
// #ifndef MP-ALIPAY
100-
.in(proxy)
101-
// #endif
102-
.select('.wd-resize__container')
103-
.boundingClientRect()
93+
const query = uni.createSelectorQuery().in(proxy).select(`#${resizeId.value}`).boundingClientRect()
10494
query.exec(([res]) => {
10595
// 前两次滚动事件被触发,说明 created 的修改已渲染,通知用户代码当前容器大小
10696
if (scrollEventCount.value++ === 0) {

src/uni_modules/wot-design-uni/components/wd-sticky-box/wd-sticky-box.vue

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<template>
22
<view style="position: relative">
3-
<view :class="`wd-sticky-box ${props.customClass}`">
3+
<view :class="`wd-sticky-box ${props.customClass}`" :id="styckyBoxId">
44
<wd-resize @resize="resizeHandler">
55
<slot />
66
</wd-resize>
@@ -21,7 +21,7 @@ export default {
2121

2222
<script lang="ts" setup>
2323
import { getCurrentInstance, onBeforeMount, provide, ref } from 'vue'
24-
import { getRect } from '../common/util'
24+
import { getRect, uuid } from '../common/util'
2525
2626
interface Props {
2727
customClass?: string
@@ -31,6 +31,8 @@ const props = withDefaults(defineProps<Props>(), {
3131
customClass: ''
3232
})
3333
34+
const styckyBoxId = ref<string>(`wd-sticky-box${uuid()}`)
35+
3436
const observerMap = ref<Map<any, any>>(new Map())
3537
const height = ref<number>(0)
3638
const width = ref<number>(0)
@@ -109,10 +111,10 @@ function observerForChild(child) {
109111
if (height.value <= exposed.height.value) {
110112
exposed.setPosition(false, 'absolute', 0)
111113
}
112-
observer.relativeToViewport({ top: -offset }).observe('.wd-sticky-box', (result) => {
114+
observer.relativeToViewport({ top: -offset }).observe(`#${styckyBoxId.value}`, (result) => {
113115
scrollHandler(exposed, result)
114116
})
115-
getRect('.wd-sticky-box', false, proxy).then((res: any) => {
117+
getRect(`#${styckyBoxId.value}`, false, proxy).then((res: any) => {
116118
// 当 wd-sticky-box 位于 viewport 外部时不会触发 observe,此时根据位置手动修复位置。
117119
if (res.bottom <= offset) scrollHandler(exposed, { boundingClientRect: res })
118120
})

src/uni_modules/wot-design-uni/components/wd-sticky/wd-sticky.vue

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<view :style="`${rootStyle};display: inline-block;`">
33
<!--强制设置高宽,防止元素坍塌-->
44
<!--在使用 wd-sticky-box 时,某些情况下 wd-sticky__container 的 'position:absolute' 需要相对于 wd-sticky-box-->
5-
<view :class="`wd-sticky ${props.customClass}`" :style="stickyStyle">
5+
<view :class="`wd-sticky ${props.customClass}`" :style="stickyStyle" :id="styckyId">
66
<!--吸顶容器-->
77
<view class="wd-sticky__container" :style="containerStyle">
88
<!--监听元素尺寸变化-->
@@ -28,7 +28,7 @@ export default {
2828

2929
<script lang="ts" setup>
3030
import { type Ref, computed, getCurrentInstance, inject, ref } from 'vue'
31-
import { addUnit, getRect, objToStyle } from '../common/util'
31+
import { addUnit, getRect, objToStyle, uuid } from '../common/util'
3232
3333
interface Props {
3434
customStyle?: string
@@ -44,6 +44,8 @@ const props = withDefaults(defineProps<Props>(), {
4444
offsetTop: 0
4545
})
4646
47+
const styckyId = ref<string>(`wd-sticky${uuid()}`)
48+
4749
const openBox = ref<boolean>(false)
4850
const position = ref<string>('absolute')
4951
const top = ref<number>(0)
@@ -142,8 +144,8 @@ function observerContentScroll() {
142144
.relativeToViewport({
143145
top: -offset // viewport上边界往下拉
144146
})
145-
.observe('.wd-sticky', scrollHandler)
146-
getRect('.wd-sticky', false, proxy).then((res: any) => {
147+
.observe(`#${styckyId.value}`, scrollHandler)
148+
getRect(`#${styckyId.value}`, false, proxy).then((res: any) => {
147149
// 当 wd-sticky 位于 viewport 外部时不会触发 observe,此时根据位置手动修复位置。
148150
if (res.bottom <= offset) scrollHandler({ boundingClientRect: res })
149151
})

0 commit comments

Comments
 (0)