微信小程序-动画加载滑进函数封装自写

//xx.wxml
<view id="an7" class="box {{isan4?'jiazaihou':''}}">
nih
 </view>
//xx.wxss
.需要动画的标签类{
  transition:all 1s;
  transform: translateY(40px);
  opacity: 0;
}

.jiazaihou后期添加的类{
  transform:translateY(0) !important;
  opacity:1 !important;

}
//xx.js
data:{
    isan5:false,
    isan6:false,
    isan7:false,
}
onPageScroll(e){
    // 滑动出现
    this.getOffsetValue('an');
    this.getOffsetValue('an');
    this.getOffsetValue('an3');
    this.getOffsetValue('an4');
    this.getOffsetValue('an5');
    this.getOffsetValue('an7');
  },

  // 获取元素的offset值
  getOffsetValue(idName){
    let _this = this;
    let widH = wx.getSystemInfoSync().windowHeight;//获取设备视图区高
    const query = wx.createSelectorQuery();
    query.select(`#${idName}`).boundingClientRect();
    query.selectViewport().scrollOffset();
    query.exec(function(res){
      let itemTop = res[0].top;
      let itemH = res[0].height;
      let scrolTop = res[1].scrollTop;
     
      if(scrolTop + itemH > itemTop + widH/2){
        switch(idName){
        
            case 'an5':
              _this.setData({isan5:true});
            break;
            case 'an6':
              _this.setData({isan6:true});
            break;
        }
      }
    })
  },

注意:transition善用该属性!

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容