1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
| export default { data() { return { ..., mainTitleEles: [], mainEles: [], ot: [], len: 0, searchBottomY: 0, selectorQuery: {}, } }, mounted() { this.selectorQuery = uni.createSelectorQuery().in(this); this.initFixedTop(); }, methods: { initFixedTop() { this.mainTitle = this.selectorQuery.selectAll('.main-title'); uni.createSelectorQuery().selectAll('.main-title').fields({ rect: true, dataset: true }, res => { this.mainTitleEles = res; this.len = this.mainTitleEles.length; for (let i = 0; i < this.len; i++) { this.ot.push(this.mainTitleEles[i].top); } uni.createSelectorQuery().selectAll('.main').fields({ rect: true, dataset: true, size: true }, res => { this.mainEles = res; this.ot.push(this.mainEles[this.len - 1].top + this.mainEles[this.len - 1].height); }).exec(); }).exec(); }, onScroll(mescroll) { const st = mescroll.scrollTop; for (let i = 0; i < this.len; i++) { if (st > this.ot[i] && st < this.ot[i + 1]) { this.$refs[this.mainTitleEles[i].dataset.id].$el.className = 'main-title fixed'; } else { this.$refs[this.mainTitleEles[i].dataset.id].$el.className = 'main-title'; } } }, } };
|