scroll-sticky.uvue 3.2 KB
Newer Older
1
<template>
雪洛's avatar
雪洛 已提交
2
	<scroll-view ref="scroll" @scroll="onScroll" class="page" rebound="false">
M
mehaotian 已提交
3 4 5 6 7 8
		<view class="content-item">
			<text class="text">向上滑动页面,体验元素吸顶效果。</text>
		</view>
		<view v-for="(item,index) in list" :key="index" class="content-item">
			<text class="text">first content-{{item}}</text>
		</view>
9

DCloud-yyl's avatar
DCloud-yyl 已提交
10
		<view ref="sticky" class="search">
M
mehaotian 已提交
11 12 13 14 15 16
			<view style="flex-direction: row;">
				<image src="/static/template/scroll-fold-nav/search.png" style="width: 15px;" mode="widthFix"></image>
				<text class="search-tip-text">请输入你要搜索的内容</text>
			</view>
			<text class="search-btn">搜索</text>
		</view>
17

M
mehaotian 已提交
18 19 20
		<view v-for="(item,index) in list" :key="index" class="content-item">
			<text class="text">second content-{{item}}</text>
		</view>
21

M
mehaotian 已提交
22 23 24
		<view v-for="(item,index) in list" :key="index" class="content-item">
			<text class="text">second content-{{item}}</text>
		</view>
25 26 27 28

	</scroll-view>
</template>

M
mehaotian 已提交
29 30 31 32 33 34 35 36
<script>
	export default {
		data() {
			return {
				list: ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14', '15'],
				stickyTop: 0,
				stickyTran: 0,
				scrollTop: 0,
DCloud-yyl's avatar
DCloud-yyl 已提交
37
				stickyNode: null as Element | null
M
mehaotian 已提交
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
			}
		},
		methods: {
			onScroll(e : ScrollEvent) {
				if (e.detail.scrollTop > this.stickyTop) {
					let stickyTran = e.detail.scrollTop - this.stickyTop;
					if (stickyTran != this.stickyTran) {
						this.stickyNode?.style?.setProperty('transform', 'translateY(' + stickyTran + 'px)');
					}
					this.stickyTran = stickyTran;
				} else {
					this.stickyNode?.style?.setProperty('transform', '');
					this.stickyTran = 0;
				}
			},
			back() {
				uni.navigateBack({
					success(result) {
						console.log('navigateBack success', result.errMsg)
					},
					fail(error) {
						console.log('navigateBack fail', error.errMsg)
					},
					complete(result) {
						console.log('navigateBack complete', result.errMsg)
					},
				})
			}
		},
		onLoad() {
		},
		onReady() {
DCloud-yyl's avatar
DCloud-yyl 已提交
70
			this.stickyNode = this.$refs['sticky'] as Element;
M
mehaotian 已提交
71 72 73
			// let rect = this.stickyNode?.getBoundingClientRect();
			// this.stickyTop = rect?.top;
			//this.stickyTop = this.stickyNode?.getBoundingClientRect()?.top;
雪洛's avatar
雪洛 已提交
74 75 76
			const stickyRect = (this.$refs['sticky'] as Element).getBoundingClientRect();
      const scrollRect = (this.$refs['scroll'] as Element).getBoundingClientRect();
			this.stickyTop = stickyRect.top - scrollRect.top;
M
mehaotian 已提交
77
		}
78 79 80 81
	}
</script>

<style>
M
mehaotian 已提交
82 83
	.page {
		flex: 1;
DCloud-yyl's avatar
DCloud-yyl 已提交
84
		padding: 0 15px;
M
mehaotian 已提交
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
		background-color: #f5f5f5;
	}

	.content-item {
		padding: 15px;
		margin: 5px 0;
		background-color: #fff;
		border-radius: 5px;

	}

	.text {
		font-size: 14px;
		color: #666;
		line-height: 20px;
	}
101

M
mehaotian 已提交
102 103 104 105 106
	.search {
		background-color: #FFFFFF;
		border: 1px solid #fbdf0d;
		height: 35px;
		border-radius: 100px;
H
hdx 已提交
107
		margin: 0 12px;
M
mehaotian 已提交
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
		padding: 8px;
		flex-direction: row;
		align-items: center;
		justify-content: space-between;
		z-index: 100;
	}

	.search-tip-text {
		font-size: 12px;
		color: #666;
	}

	.search-btn {
		font-size: 12px;
		background-color: #ff6900;
		color: #FFF;
		padding: 5px 8px;
		border-radius: 100px;
	}
雪洛's avatar
雪洛 已提交
127
</style>