微信小程序仿通讯录功能

作者:默认S 时间:2024-05-22 10:31:38 

本文实例为大家分享了微信小程序实现通讯录功能的具体代码,供大家参考,具体内容如下

微信小程序模仿通讯录功能需要用到scroll-view标签

思路:首先需要获取到你所需要展示的数据样式的高度(这就需要用到微信给我们提供的一个API来完成了,因为小程序是没有DOM树结构的,这个可以去看我的前一篇里面有详细的记载怎么获取想要的元素的宽高。),然后组合成一个高度数组(便于后面根据这个数组进行判断),再获取滚动距离,用这两个比较判断之后就可以得出滚动的时候右边选中的字母了,然后再利用scroll-view标签的scroll-into-view属性来实现点击右侧导航字母,对应的左侧列表滚动到相应的位置。(每个人的想法不同,解法和理解也不太可能相同。所以,按自己的心走就好了),话不多说,上代码!

wxml


<view>
<!-- 左侧列表内容部分 -->
<scroll-view class="content" enable-back-to-top scroll-into-view="{{toView}}" scroll-y="true" scroll-with-animation="true" bindscroll="onPageScroll">
<view wx:for="{{listMain}}" wx:for-item="group" wx:key="{{group.id}}" id="{{ 'inToView'+group.id}}" data-id='{{group.id}}'>
 <view class="address_top">{{group.name}}</view>
 <view wx:for="{{group.list}}" wx:for-item="bdiet" wx:key="{{index}}">
 <navigator url='./wholeDetail?id={{bdiet.id}}' hover-class='none'>
  <view class="address_bottom" data-id='{{bdiet.id}}'>{{bdiet.wiki_name}}</view>
 </navigator>
 </view>
</view>
</scroll-view>
<!-- 右侧字母导航 -->
<view class="orientation_region">
<view class="orientation">#</view>
<block wx:for="{{listMain}}" wx:key="{{item.id}}">
 <view class="orientation_city {{isActive==item.id ? 'active':'' }}" bindtap="scrollToViewFn" data-id="{{item.id}}">
 {{item.name}}
 </view>
</block>
</view>
</view>

wxss


page {
height: 100%;
}

.content {
padding-bottom: 20rpx;
box-sizing: border-box;
height: 100%;
position: fixed;
}

.location {
width: 100%;
}

.location_top {
height: 76rpx;
line-height: 76rpx;
background: #f4f4f4;
color: #606660;
font-size: 28rpx;
padding: 0 20rpx;
}

.location_bottom {
height: 140rpx;
line-height: 140rpx;
color: #d91f16;
font-size: 28rpx;
border-top: 1rpx #e5e5e5 solid;
border-bottom: 1rpx #e5e5e5 solid;
padding: 0 20rpx;
align-items: center;
display: -webkit-flex;
}

.address_top {
height: 56rpx;
line-height: 56rpx;
background: #ebebeb;
color: #384857;
font-size: 28rpx;
padding: 0 20rpx;
}

.address_bottom {
height: 88rpx;
line-height: 88rpx;
background: #fff;
color: #000;
font-size: 28rpx;
border-bottom: 1rpx #e5e5e5 solid;
margin: 0 32rpx;
}

.location_img {
width: 48rpx;
height: 48rpx;
position: absolute;
right: 20rpx;
top: 125rpx;
}

.add_city {
width: 228rpx;
height: 60rpx;
line-height: 60rpx;
text-align: center;
border: 1rpx solid #e5e5e5;
color: #000;
margin-right: 20rpx;
}

.add_citying {
width: 228rpx;
height: 60rpx;
line-height: 60rpx;
text-align: center;
border: 1rpx solid #09bb07;
color: #09bb07;
margin-right: 20rpx;
}

.orientation {
white-space: normal;
display: inline-block;
width: 45rpx;
height: 50rpx;
font-size: 28rpx;
font-weight: bold;
color: rgb(88, 87, 87);
text-align: center;
}

.orientation_region {
padding: 5px 0px;
width: 45rpx;
font-size: 20rpx;
position: fixed;
top: 50%;
right: 6rpx;
transform: translate(0, -50%);
background: rgb(199, 198, 198);
border-radius: 10px;
}

.orientation_city {
height: 40rpx;
line-height: 40rpx;
color: #000;
text-align: center;
}

.active {
color: #2cc1d1;
}

.list-fixed {
position: fixed;
width: 100%;
z-index: 999;
height: 56rpx;
line-height: 56rpx;
background: #ebebeb;
color: #999;
font-size: 28rpx;
padding: 0 20rpx;
z-index: 9999;
}

.fixed-title {
color: #2cc1d1;
}

核心来了(JS逻辑)


Page({
/**
* 页面的初始数据
*/
data: {
isActive: null,
listMain: [],
toView: 'inToView0',
oHeight: [],
},
//点击右侧字母导航定位触发
scrollToViewFn: function (e) {
var that = this;
var _id = e.target.dataset.id;
var scrollTop = that.data.scrollTop;
console.log('点击获取Id', _id)
console.log('点击获取滚动距离', scrollTop)
for (var i = 0; i < that.data.oHeight.length; i++) {
 if (that.data.oHeight[i].key === _id) {
 that.setData({
  toView: 'inToView' + that.data.oHeight[i].key
 });
 break
 }
}
},
// 页面滑动时触发
onPageScroll: function (e) {
var that = this;
that.setData({
 scrollTop: e.detail.scrollTop
})
var scrollTop = that.data.scrollTop;
console.log(scrollTop);
for(var i =0; i< that.data.oHeight.length; i++){
 if (scrollTop >= 0 && scrollTop + 20 < that.data.oHeight[0].height){
 that.setData({
  isActive: that.data.oHeight[0].key
 });
 } else if (scrollTop + 20 <= that.data.oHeight[i].height) {
 that.setData({
  isActive: that.data.oHeight[i].key
 });
 return false;
 }
}
},
// 处理数据格式,及获取分组高度
getBrands: function () {
var that = this;
var url = config.DOMAIN_API.wikiWholeList,
 data = {};
//发起get请求,使用方式如下:
util.ajaxPost(url, data).then((res) => { //成功处理
 that.setData({
 listMain: res
 });
 var number = 0;
 for (let i = 0; i < that.data.listMain.length; i++) {
 wx.createSelectorQuery().select('#inToView' + that.data.listMain[i].id).boundingClientRect(function (rect) {
  number = rect.height + number;
  var newArry = [{ 'height': number, 'key': rect.dataset.id, "name": that.data.listMain[i].name }]
  that.setData({
  oHeight: that.data.oHeight.concat(newArry)
  })
 }).exec();
 };
}).catch((errMsg) => { //错误处理,已统一处理,此处可以不需要
 console.log(errMsg);
});

},
onLoad: function (options) {
var that = this;
wx.hideShareMenu()
that.getBrands();
},
})

来源:https://blog.csdn.net/qq_37949737/article/details/90764700

标签:微信小程序,通讯录
0
投稿

猜你喜欢

  • python魔法方法-自定义序列详解

    2022-10-08 08:56:12
  • python正则表达式常见的知识点汇总

    2023-10-02 18:56:21
  • Node.js(安装,启动,测试)

    2024-05-13 09:29:28
  • Oracle 下的开发日积月累

    2009-02-28 11:08:00
  • 需要掌握的八个CSS布局技巧

    2008-05-17 11:45:00
  • MySQL查询缓存优化示例详析

    2024-01-27 12:21:32
  • M1芯片安装python3.9.1的实现

    2021-12-15 12:13:46
  • Python简单生成随机数的方法示例

    2021-01-21 02:30:37
  • 深入理解Vue 的条件渲染和列表渲染

    2024-04-09 10:46:37
  • 如何让框架的网页背景透明

    2008-04-08 14:24:00
  • pymongo中聚合查询的使用方法

    2021-07-07 19:31:23
  • pyinstaller打包opencv和numpy程序运行错误解决

    2023-02-23 02:42:54
  • Python中numpy模块常见用法demo实例小结

    2022-08-03 12:45:12
  • sql在一个表中添加字段并添加备注的方法

    2024-01-26 10:16:59
  • 详解mysql建立索引的使用办法及优缺点分析

    2024-01-23 08:48:15
  • Python + selenium自动化环境搭建的完整步骤

    2023-11-19 12:42:57
  • python学生管理系统代码实现

    2023-10-31 07:55:04
  • 通过python连接Linux命令行代码实例

    2023-01-25 23:10:33
  • flask结合jinja2使用详解

    2022-07-22 12:18:57
  • FSO遍历文件夹目录及目录下文件asp代码

    2008-10-10 12:54:00
  • asp之家 网络编程 m.aspxhome.com