(六)scroll-view 组件

(一)横向滚动、纵向滚动

scroll-x  scroll-y


  
    
    
      A
      B
      C
    


    
    
      A
      B
      C
    
  

同时配合css

.view {
  height: 400px;
  width: 100%;
  /* 必须是 inline-block 给父元素设置display: flex 不好使 */
  display: inline-block;
}

.demo-1 {
  background: red;
}

.demo-2 {
  background: green;
}

.demo-3 {
  background: yellow;
}

.wrapper {
  height: 500px;
}

.x-wrapper {
  white-space: nowrap;
  height: 400px;
  margin: 100px 0;
}

(二)scroll-into-view

wxml:


    
    
      A
      B
      C
    

    
    
    
  

js:

Page({
  data: {
    targetIndex: 'block1'
  },
  scrollTarget (e) {
    this.setData({
      targetIndex: `block${e.currentTarget.dataset.id}`
    })
    console.log(e.currentTarget.dataset.id)
  }
}) 

涨知识:

1. scroll-into-view 绑定id,当点击按钮的时候,改变 绑定id,达到滚动到可是区域的效果

2. 小程序传参数 需要 在 标签上绑定 data-参数名称,e.currentTarget.dataset.参数名称获取

3. scroll-with-animation 滚动的时候会有动画,缓慢的平滑

4. 每个 view 的 id 不能以 数字开头

(三)scroll-anchoring

(六)scroll-view 组件_第1张图片

举个例子:一篇文章里面有多张图片 下滑的时候,上面的图片突然加载出来了,图片从 height: 0 变为了 height: 100 ,页面会抖动一下,加上这个 scroll-anchoring 会避免引起抖动~

简而言之:控制滚动实体在内容变化时 不发生滚动

(四)上拉加载、下拉刷新

(六)scroll-view 组件_第2张图片

下拉刷新:

过程:下拉 (bindrefresherpulling)---达到阀值  ----> 可释放下拉刷新 (bindrefresherrefresh)

代码:


  
    
    
      
      
        
        {{ message }}
      
      
      A
      B
      C
    
  

js:

Page({
  data: {
    refreshState: false,
    message: '下拉刷新',
    timer: null
  },
  // 复位 清除模拟接口用的定时器
  refreshend () {
    if (this.data.timer) {
      clearInterval(this.data.timer)
    }
  },
  // 中断
  freshabort () {
    this.setData({
      refreshState: false,
      message: '下拉刷新'
    })
  },
  // 正在下拉
  pulling (e) {
    this.setData({
      refreshState: true
    })
    if (e.detail.dy >= 50) { // 达到阀值
      this.setData({
        message: '释放刷新'
      })
    } else {
      this.setData({
        message: '下拉刷新'
      })
    }
  },
  // 释放
  getNewData () {
    this.setData({
      message: '正在刷新'
    })
    this.setData({
      timer: setTimeout(() => {
        console.log(111)
        this.setData({
          refreshState: false
        })
      }, 3000)
    })
  }
}) 

css:

.view {
  height: 400px;
  width: 100%;
  display: inline-block;
}

.demo-1 {
  background: red;
}

.demo-2 {
  background: green;
}

.demo-3 {
  background: yellow;
}

.wrapper {
  height: 500px;
}

.loading {
  animation: loading 2s infinite;
  width: 50rpx;
  height: 50rpx;
  background-size: 100% 100%;
  background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAMgAAADICAYAAACtWK6eAAAR6UlEQVR4Xu2dCdR21RTHf9aiJSQRJWOhzBUqQyxTppSMRaTIkFWZp1BaopVZGTJkSKmQROaQUilDkkKEKCWUDFFYrD/3Wd/7vd73veece86999znv9d61vet9e6zzz7/ff7Pc+8Z9r4WFiNgBJZF4FrGxggYgeURMEE8O4zACgiYIJ4eRsAE8RwwAmkI+BckDTe3mhMETJA5CbSHmYaACZKGm1vNCQImyJwE2sNMQ8AEScPNreYEARNkTgLtYaYhYIKk4eZWc4KACTIngfYw0xAwQdJwc6s5QcAEmZNAe5hpCJggabgN0Wot4J6LPusB3wROBb4PnAVcOoRzU+3TBKkjsk8ADgI2CnD37cALA/SsEoCACRIAEnAf4N7Nv/r/P4GTgNOB7zafMEvxWm8DXhDZ7BLg+cAnIttZfRECJsjKU2JL4A3AQ1pmjoiiiZxzQm4FfKvjjD0B2K6jjbluboIsH/6XAG+KnB0iyJMi2yynfjWwRgZb2wAnZrAzlyZMkKXD/u+Os6Errp8GHtPRh1nzfwBrA3/LZG+uzHQN5BTB+jjwxI4De0fCe8Osy+cAh3bsf3HzNwMvzWxzLsyZIKuHWcQQQXKIHrVi30luAJwL3DqHA4tsPBz4cgG7kzZpgqwe3tOalaocQb8M0PP/DyKM3R84OUI/RvU1wAExDawLJs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PYpCCJgghYC12Wkg8B/hxTHnY/vWXAAAAABJRU5ErkJggg==')
}

@keyframes loading {
  0%{-webkit-transform:rotate(0deg);}
	100%{-webkit-transform:rotate(360deg);}
}

.pull {
  width: 100px;
  margin: 10px auto;
  display: flex;
}

.message {
  color: #ccc;
  font-size: 16px;
}

效果:

(六)scroll-view 组件_第3张图片

(六)scroll-view 组件_第4张图片(六)scroll-view 组件_第5张图片

你可能感兴趣的:(小程序)