【微信小程序】手写索引选择器(城市列表,汽车品牌选择列表)
摘要: 小程序索引选择器,点击跳转相应条目,索引可滑动,滑动也可跳转
场景:城市选择列表, 汽车品牌选择列表
所用组件: scroll-view(小程序原生) https://developers.weixin.qq.com/miniprogram/dev/component/scroll-view.html
list: [
{
"letter": "A",
"list": [{
"name": "奥迪1",
"id": 1
},
{
"name": "奥迪2",
"id": 2
}]
},
{
"letter": "B",
"list": [{
"name": "宝马1",
"id": 3
},
{
"name": "宝马2",
"id": 4
}]
},
{
"letter": "C",
"list": [{
"name": "麻喇沙底",
"id": 5
},
{
"name": "adidas",
"id": 6
}]
},
{
"letter": "D",
"list": [{
"name": "煲事件",
"id": 7
},
{
"name": "保时捷",
"id": 8
}]
},
{
"letter": "E",
"list": [{
"name": "宝马1",
"id": 9
},
{
"name": "宝马2",
"id": 10
}]
},
{
"letter": "F",
"list": [{
"name": "宝马1",
"id": 11
},
{
"name": "宝马2",
"id": 12
}]
},
{
"letter": "G",
"list": [{
"name": "宝马1",
"id": 15
},
{
"name": "宝马2",
"id": 16
}]
},
{
"letter": "H",
"list": [{
"name": "宝马1",
"id": 17
},
{
"name": "宝马2",
"id": 18
}]
},
{
"letter": "I",
"list": [{
"name": "宝马1",
"id": 19
},
{
"name": "宝马2",
"id": 20
}]
},
{
"letter": "J",
"list": [{
"name": "宝马1",
"id": 21
},
{
"name": "宝马2",
"id": 22
}]
},
{
"letter": "K",
"list": [{
"name": "宝马1",
"id": 23
},
{
"name": "宝马2",
"id": 24
}]
},
]
html 结构:
主体部分
<scroll-view class="content" scroll-y="true" :style="scrollHeight" :scroll-top="scrollData.scrollTop" :scroll-into-view="toView">
<ul class="brandList">
<li v-for="(item, index) in list" :key="index" :id="item.letter">
<h2>{{item.letter}}</h2>
<div v-for="(el, i) in item.list" :key="i" class="pro" @click="getToast(el.name)">
<img src="/static/images/user.png" alt="" mode="aspectFit">
<span>{{el.name}}</span>
</div>
</li>
</ul>
</scroll-view>
// 侧边导航栏
<div class="letterList" @touchstart="handlerTouchMove"
@touchmove="handlerTouchMove"
@touchend="handlerTouchEnd" :style="fixedTop">
<p v-for="(item, index) in list" :key="index" @click="getLetter(item.letter)">{{item.letter}}</p>
</div>
scrollData: {
scrollTop: 0,
},
toView:'A',
step1:
首先,侧边导航栏肯定是用fixed定位固定(一般在右侧),其次在竖直方向居中,调用wx.getSystemInfo
let fixedTop = 0;
let length = this.list.length // 取到list数组的length,因为list的length决定了侧边导航的高度
wx.getSystemInfo({
success(res) {
fixedTop = (res.windowHeight - (20 * length))/2 //20为侧边导航每个p的高度
}
})
step2
手指在侧边导航滑动时的js
handlerTouchMove (e) {
let touches = e.touches[0] || {}
let pageY = touches.pageY
let rest = pageY - this.fixedTop
let index = Math.ceil(rest / 20)
console.log(this.list.length, index, 206);
console.log(index,195);
if(index > this.list.length) {
index = this.list.length - 1//
}
if( 0 <index <= this.list.length) { // 1213 10 9
12 index = index - 1
13 }
14 if(index <= 0) {
15 index = 0
16 }
17 let letter = this.list[index].letter
18 wx.showToast({
19 title: letter,
20 icon: 'none',
21 duration: 2000
22 })
23 this.toView = letter
24 },
25 handlerTouchEnd () {
26 wx.hideLoading()
27 },
step3
点击侧边索引时的js
getLetter (letter) {
this.toView = letter
},
结束惹,88, 下次见
是
【微信小程序】手写索引选择器(城市列表,汽车品牌选择列表)的更多相关文章
- 微信小程序开发之picker选择器组件用法
picker组件时一个从底部弹起的可滚动的选择器(嵌入页面滚动器组件picker-view查看https://mp.weixin.qq.com/debug/wxadoc/dev/component/p ...
- 「小程序JAVA实战」 小程序手写属于自己的第一个demo(六)
转自:https://idig8.com/2018/08/09/xiaochengxu-chuji-06/ 自己尝试的写一个小demo,用到自定义样式,自定义底部导航,页面之间的跳转等小功能.官方文档 ...
- 微信小程序-获取当前位置和城市名
微信小程序-获取当前城市位置 1, 获取当前地理位置,首先要拿到用户的授权wx.openSetting: 2,微信的getLocation接口,获取当前用户的地理位置(微信返回的是经纬度,速度等参数) ...
- mpvue开发微信小程序之时间+日期选择器
最近在做微信小程序,技术栈为mpvue+iview weapp组件库. 因项目需求,要用到日期+时间选择器,iview组件库目前还未提供时间日期选择器的组件,小程序官方组件日期时间也是分开的,在简书上 ...
- 微信小程序初探--写个扫雷分享给你玩
闲暇里,想学一下微信小程序, 于是,用微信小程序原生做了个扫雷玩. 以下略作总结,分享给大家. 微信里下拉,输入[mini计算器], 看到这个图标的就是了: 说好的扫雷,怎么变成计算器了?原因后面解释 ...
- [微信小程序] 微信小程序下拉滚动选择器picker绑定数据的两种方式
小程序 picker 多列选择器 数据动态获取 需求是将各校区对应各班级的数据 以两列选择器的方式展示出来,并且可以在选择完成之后记录选结果参数. 校区数据 和 班级数据 分别是两个接口,以 校区 t ...
- mpvue微信小程序怎么写轮播图,和官方微信代码的差别
目前用mpvue很多第三方的ui库是引入不了的,因为它不支持含有dom操作. 那我们要做轮播图的话一个是手写另外一个就是用小程序的swiper组件了: 官方代码: <swiper indicat ...
- 微信小程序中利用时间选择器和js无计算实现定时器(将字符串或秒数转换成倒计时)
转载注明出处 改成了一个单独的js文件,并修改代码增加了通用性,点击这里查看 今天写小程序,有一个需求就是用户选择时间,然后我这边就要开始倒计时. 因为小程序的限制,所以直接选用时间选择器作为选择定时 ...
- 微信小程序手绘地图实现之《Canvas》
环境:微信SDK2.9+ + uniapp (可切换直接使用.map.js不限制环境) 正题: 先创建一个地图组件 <template> <view class="cu ...
随机推荐
- Appium swipe实现屏幕滑动
在 Appium 中提供 swipe() 方法来模拟用户滑动屏幕. swipe() 实现过程 是先通过在屏幕上标记两个坐标,然后再从开始坐标移动到结束坐标. 先看下 swipe 方法定义: def s ...
- Python字典(Dictionary)update()方法
原文连接:https://www.runoob.com/python/att-dictionary-update.html Python字典(dictionary)update()函数把字典dict2 ...
- CentOS下安装FreeTDS
导读 官方网站:http://www.freetds.org 下载地址:http://ibiblio.org/pub/Linux/ALPHA/freetds/stable/freetds-stable ...
- Redis set集合
Set操作.Set集合就是不允许重复的列表 (无序集合) sadd(name,values) # name对应的集合中添加元素 scard(name) # 获取name对应的集合中元素个数 sdiff ...
- KVM学习笔记--静态迁移
.静态迁移过程如下 (1)确定虚拟机关闭状态 (2)准备迁移oeltest02虚拟机,查看该虚拟机配置的磁盘文件 (3)导入虚拟机配置文件 [root@node1~]# virsh dumpxml o ...
- 通俗的讲,就是高层模块定义接口,低层模块负责实现。 Bob Martins对DIP的定义: 高层模块不应依赖于低层模块,两者应该依赖于抽象。 抽象不不应该依赖于实现,实现应该依赖于抽象。
通俗的讲,就是高层模块定义接口,低层模块负责实现. Bob Martins对DIP的定义: 高层模块不应依赖于低层模块,两者应该依赖于抽象. 抽象不不应该依赖于实现,实现应该依赖于抽象. 总结出使用D ...
- c# 模拟表单提交,post form 上传文件、数据内容
转自:https://www.cnblogs.com/DoNetCShap/p/10696277.html 表单提交协议规定:要先将 HTTP 要求的 Content-Type 设为 multipar ...
- Java编程基础——常量变量和数据类型
Java编程基础——常量变量和数据类型 摘要:本文介绍了Java编程语言的常量变量和数据类型. 常量变量 常量的定义 一块内存中的数据存储空间,里面的数据不可以更改. 变量的定义 一块内存中的数据存储 ...
- JMeter性能测试入门--简单使用
1.JMeter整体简介 Apache JMeter是Apache组织开发的基于Java的压力测试工具.用于对软件做压力测试,它最初被设计用于Web应用测试,但后来扩展到其他测试领域. 它可以用于测试 ...
- 小鸟初学Shell编程(一)认识Shell
开篇介绍 Linux里非常的有用的一个功能,这个功能就叫Shell脚本. Shell脚本在我日常开发工作里也占了非常重要的角色,项目中一些简单的工作我们都可以使用Shell脚本来完成,比如定时删除日志 ...