在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
<view class="container"> <view class="page-body"> <view class="page-section message"> <text wx:if="{{appear}}"> 小球出现 </text> <text wx:else> 小球消失 </text> </view> <view class="page-section"> <scroll-view class="scroll-view" scroll-y> <view class="scroll-area" style="{{appear ? 'background: #ccc' : ''}}"> <text class="notice">向下滚动让小球出现</text> <view class="filling"></view> <view class="ball"></view> </view> </scroll-view> </view> </view> </view> .scroll-view { height: 400rpx; background: #fff; border: 1px solid #ccc; box-sizing: border-box; } .scroll-area { height: 1300rpx; display: flex; flex-direction: column; align-items: center; transition: .5s; } .notice { margin-top: 150rpx; } .ball { width: 200rpx; height: 200rpx; background: #1AAD19; border-radius: 50%; } .filling { height: 400rpx; } .message { width: 100%; display: flex; justify-content: center; } .message text { font-size: 40rpx; font-family: -apple-system-font, Helvetica Neue,Helvetica,sans-serif; } Page({ data: { appear: false }, onLoad() { this._observer = wx.createIntersectionObserver(this) this._observer .relativeTo('.scroll-view') .observe('.ball', (res) => { console.log(res); this.setData({ appear: res.intersectionRatio > 0 }) }) }, onUnload() { if (this._observer) this._observer.disconnect() } })
1,boundingClientRect:目标边界。这个目标,就是我们的观察对象,可以看到刚开始相交的时候,它的位置情况。这个位置是相对于整个页面的,不是相对于参照元素的。top = 251(px) = scroll-view的高度(200px) + "小球消失/出现"message的高度(52px) - 相交高度(1px) 参考地址:https://blog.csdn.net/qq_25324335/article/details/83687695 |
请发表评论