微信小程序——通讯录索引实现

前言

实现通讯录索引的关键就是使用好3个触发事件 bindtouchstart、bindtouchend和bindtouchmove。
在这里插入图片描述

展示效果

在这里插入图片描述

代码分析

bindtouchstart 显示字母栏选中效果

  //点击开始
  getIndex: function (e) {
    console.log('点击开始')
    console.log(e)
    this.setData({
      showSus: true,
      tempIndex: e.currentTarget.dataset.index
    })
  }

bindtouchmove 的事件返回参数中是可以得到所滑动到的位置坐标的
利用简单的计算得到我们的滑动到了哪里

  // 滑动中
  toMove: function (e) {
    console.log('滑动中')
    var clientY = e.touches[0].clientY;
    var index = 0;
    if (clientY > 150 && clientY < 490) {     //在滑动区域内
      index = parseInt((clientY - 151) / 13); //13就是每个字母块儿的高度
      if (index >= 0 && index <= 25) {      //避免出现误差 index在0-25中
        this.setData({
          tempIndex: index
        })
      }
    }
  },

bindtouchend 结束整个滑动动作并作出异常判断

  // 滑动中
  toEnd: function () {
    console.log('滑动结束')
      this.setData({
        showSus: false
      })
       //当滑动结束时的字母分组无人时 不发生跳转
      if (this.data.contactList[this.data.tempIndex].nameList.length != 0) {
        console.log(this.data.tempIndex)
          this.setData({
            letterId: this.data.tempIndex,
          })
      }
  },

wxml

<scroll-view scroll-y class="height100" style="margin-top:{{CustomBar}}px" scroll-with-animation="true" enable-back-to-top="true" scroll-into-view='sw{{letterId}}' >
	<view class="index-title" id="sw{{index}}" hidden="{{!(item.nameList.length!=0)}}" wx:for="{{contactList}}" wx:for-item="item">
		<view class="tip">{{item.tip}}</view>
		<view  wx:for="{{item.nameList}}" class="name-item flxr aic" wx:for-item="col">
			<view class="head">{{item.tip}}</view>
			<view class="name ml20">{{col.name}}</view>
		</view>
	</view>

</scroll-view>
<view class="index-list flxc" bindtouchend="toEnd" bindtouchmove="toMove">
	<view class="index-list-item {{}}" wx:for="{{letterList}}" bindtouchstart="getIndex"  data-index="{{index}}">{{item}}</view>
	<view wx:if="{{showSus}}" class="xuanfu">{{letterList[tempIndex]}}</view>
</view>

代码下载链接————下载链接

总结

至此我们的通讯录索引就实现了。

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

允乐.��

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值