动态实现瀑布流

动态实现瀑布流

1.首先获取动态高度

根据标签动态获取,首先需要封装

export default{
	getRect(selector){
		return new Promise((resolve) => {
			// uni.createSelectorQuery获取元素节点,小程序里wx.createSelectorQuery Vue里在元素上添加ref属性,使用$refs来获取到该元素
			let view = uni.createSelectorQuery().select(selector);
			view.fields({
				size: true,
				rect: true,
				scrollOffset:true
			}, (res) => {
				resolve(res);
			}).exec();
		})
	}
}

在main中说明

import Tool from '@/common/util/Tool.js'
Vue.prototype.$Tool = Tool

根据id动态获取

calScrollViewHeight() {
				this.$Tool.getRect("#nav").then(res => {
					console.log(res);
					this.oldHeight = res.top
					this.height = res.height
				})
				this.$nextTick(function() {
					// 循环 栏目category_
					for (let index in this.categoryList) {
						const category = this.categoryList[index]
						this.$Tool.getRect('#category_' + category.id).then(res => {
							category.top_height = res.top
							this.categoryList[index] = category
						})
					}
				})
			},

监听页面滚动

// 监听页面滚动,同时控制顶部tab现隐
			onPageScroll(e) {
				if (this.getMore) return;
				this.getMore = true;

				for (const index in this.categoryList) {
					const item = this.categoryList[index]
					if (e.scrollTop > item.top_height - this.screenHeight - 60) {
						this.$set(this, 'categoryCurrent', index)
					}
				}
				this.getMore = false;

				//显示导航条
				if (this.oldHeight < e.scrollTop + this.height) {

					this.is_block_category = true

				} else {

					this.is_block_category = false

				}
			}
		},

根据索引滑动到制定高度

categoryTabsChange(index) {
				this.categoryCurrent = index;

				switch (index) {
					case 0:
						//商品
						uni.pageScrollTo({
							scrollTop: this.categoryList[index].top_height - 98,
							duration: 100
						})
						break;

					case index:
						//详情
						uni.pageScrollTo({
							scrollTop: this.categoryList[index].top_height,
							duration: 100
						})
						break;
				}
			},
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值