WXML部分

1 <view class="index">
2
3 <!-- 数据展示区 -->
4 <scroll-view
5 class="scroll-list"
6 scroll-y
7 style="height: {{windowHeight - bottomHeight}}px"
8 bindscroll="bindscroll">
9 <view class="scroll-item" wx:for="{{optionsList}}" wx:key="index">
10 <view>{{item.time}}</view>
11 </view>
12 </scroll-view>
13
14 <!-- 页面可拖拽区域 -->
15 <view
16 wx:if='{{editting}}'
17 class="touchView"
18 catchtouchstart="scrollTouchStart"
19 catchtouchmove="scrollTouchMove"
20 catchtouchend="scrollTouchEnd"
21 style="height: {{windowHeight - bottomHeight}}px">
22 </view>
23
24 <!-- 页面拖拽处理区域 -->
25 <movable-area
26 wx:if='{{editting}}'
27 class="moveable_row"
28 style="height: {{windowHeight - bottomHeight}}px">
29 <movable-view
30 class="moveable_item {{moveData ? 'show' : 'hide'}}"
31 style="height: {{scrollItemHeight}}px;"
32 direction="vertical"
33 x="{{moveData.x}}"
34 y="{{moveData.y}}"
35 inertia="false"
36 damping="9999"
37 friction="9999"
38 >
39 <view class="scroll-item moving" hidden='{{!moveData}}'>
40 <view>{{selectItem.time}}</view>
41 </view>
42 </movable-view>
43 </movable-area>
44
45 <!-- 底部栏 -->
46 <view class="index-bottom">
47 <button type="primary" wx:if='{{!editting}}' bindtap="changeEditing">编辑</button>
48 <button type="warn" wx:else bindtap="changeEditing">取消</button>
49 </view>
50
51 </view>
WXSS部分

1 .index {
2 position: relative;
3 }
4
5 /* 数据展示区 */
6 .scroll-list{
7 width: 100%;
8 }
9 .scroll-item {
10 width: 100%;
11 padding: 10rpx 20rpx;
12 box-sizing: border-box;
13 border: 1px solid #999;
14 }
15 .scroll-item.moving {
16 border: 1px dashed blue;
17 }
18
19 /* 页面可拖拽区域 */
20 .touchView {
21 width: 100%;
22 position: absolute;
23 left: 0;
24 top: 0;
25 z-index: 1;
26 }
27
28 /* 页面拖拽处理区域 */
29 .moveable_row {
30 width: 100%;
31 position: absolute;
32 left: 0;
33 top: 0;
34 }
35 .moveable_item {
36 width: 100%;
37 }
38 .moveable_item.show {
39 background: #fff;
40 }
41 .moveable_item.hide {
42 background: transparent;
43 }
44
45
46 /* 底部栏 */
47 .index-bottom {
48 width: 100%;
49 display: flex;
50 justify-content: flex-end;
51 padding: 10rpx 20rpx;
52 box-sizing: border-box;
53 }
js部分  

1 Page({
2 data:{
3 optionsList: [], // 数据源
4 windowHeight: 0, // 屏幕高度
5 scrollItemHeight: 0, // 列表单项高度
6 bottomHeight: 0, // 底部按钮高度
7 selectItem: {}, // 当前选中元素
8 selectIndex: 0, // 当前选中索引
9 editting: true, // 是否是“编辑”状态
10 moveData: null, // 列表项移动时记录移动位置
11 scrollTop: 0 // scroll-view距离顶部距离
12 },
13 // 初始化页面数据
14 init() {
15 this.setData({
16 optionsList: [{time: '12:00'}, {time: '13:00'}, {time: '14:00'}, {time: '15:00'}, {time: '16:00'}, {time: '17:00'}, {time: '18:00'}, {time: '19:00'}, {time: '20:00'}, {time: '21:00'}, {time: '22:00'}, {time: '23:00'}, {time: '24:00'}, {time: '25:00'}, {time: '26:00'}, {time: '27:00'}, {time: '28:00'}, {time: '29:00'}, {time: '30:00'}, {time: '31:00'}, {time: '32:00'}, {time: '33:00'}, {time: '34:00'}, {time: '35:00'}],
17 windowHeight: wx.getSystemInfoSync().windowHeight
18 }, () => {
19 wx.createSelectorQuery().select('.index-bottom').boundingClientRect(res => {
20 this.setData({
21 bottomHeight: res.height
22 })
23 }).exec()
24 wx.createSelectorQuery().select('.scroll-item').boundingClientRect(res => {
25 this.setData({
26 scrollItemHeight: res.height
27 })
28 }).exec()
29 });
30 },
31
32 // 开始拖拽
33 scrollTouchStart(event) {
34 const { scrollItemHeight } = this.data
35 const firstTouchPosition = {
36 x: event.changedTouches[0].pageX,
37 y: event.changedTouches[0].pageY,
38 }
39 const { data, index } = this.getPositionDomByXY(firstTouchPosition);
40 this.setData({
41 moveData:{
42 x: 0,
43 y: firstTouchPosition.y - scrollItemHeight / 2
44 },
45 selectItem: data,
46 selectIndex: index
47 })
48
49 },
50
51 // 拖拽ing...
52 scrollTouchMove(event) {
53 const { scrollItemHeight } = this.data
54 this.setData({
55 moveData:{
56 x: 0,
57 y: event.changedTouches[0].pageY - scrollItemHeight / 2
58 },
59 })
60
61 },
62
63 // 拖拽结束
64 scrollTouchEnd:function (event) {
65 const { selectIndex, optionsList } = this.data
66 const endTouchPosition = {
67 x: event.changedTouches[0].pageX,
68 y: event.changedTouches[0].pageY,
69 }
70 const { index } = this.getPositionDomByXY(endTouchPosition)
71 // 交换顺序
72 const temp = optionsList[selectIndex]
73 optionsList[selectIndex] = optionsList[index]
74 optionsList[index] = temp
75 this.setData({
76 optionsList,
77 moveData: null
78 })
79 },
80
81 // 根据(x,y)坐标轴获取页面元素
82 getPositionDomByXY(potions) {
83 const { scrollItemHeight, optionsList, scrollTop } = this.data
84 const y = potions.y + scrollTop;
85 const len = optionsList.length
86 for(let i = 0; i < len; i++){
87 if(y >= i*scrollItemHeight && y < (i+1)*scrollItemHeight){
88 // 返回匹配到的数据项
89 return {
90 data: optionsList[i],
91 index: i
92 };
93 }
94 }
95 return y > (len-1)*scrollItemHeight ? {
96 // 匹配项位于列表之下
97 data: optionsList[len - 1],
98 index: len - 1
99 } : {
100 // 匹配项位于列表之上
101 data: optionsList[0],
102 index: 0
103 }
104 },
105
106 // 切换编辑状态
107 changeEditing() {
108 const { editting } = this.data
109 this.setData({ editting: !editting })
110 },
111
112 // 监听滚动事件
113 bindscroll(e) {
114 this.data.scrollTop = e.detail.scrollTop
115 },
116 onLoad: function () {
117 this.init()
118 },
119 });

微信小程序拖动列表功能的更多相关文章

  1. 微信小程序新闻列表功能(读取文件、template模板使用)

    微信小程序新闻列表功能(读取文件.template) 不忘初心,方得始终.初心易得,始终难守. 在之前的项目基础上进行修改,实现读取文件内容作为新闻内容进行展示. 首先,修改 post.wxml 文件 ...

  2. 微信小程序调用蓝牙功能控制车位锁

    第一次学用微信小程序,项目需要,被逼着研究了一下,功能是调用微信小程序的蓝牙功能,连接上智能车位锁,控制升降,大概步骤及调用的小程序接口API如下: 1.打开蓝牙模块 wx.openBluetooth ...

  3. 微信小程序实战 购物车功能

    代码地址如下:http://www.demodashi.com/demo/12400.html 一.准备工作 软件环境:微信开发者工具 官方下载地址:https://mp.weixin.qq.com/ ...

  4. 微信小程序在线支付功能使用总结

    最近需要在微信小程序中用到在线支付功能,于是看了一下官方的文档,发现要在小程序里实现微信支付还是很方便的,如果你以前开发过服务号下的微信支付,那么你会发现其实小程序里的微信支付和服务号里的开发过程如出 ...

  5. 微信小程序开发-蓝牙功能开发

    0. 前言 这两天刚好了解了一下微信小程序的蓝牙功能.主要用于配网功能.发现微信的小程序蓝牙API已经封装的很好了.编程起来很方便.什么蓝牙知识都不懂的情况下,不到两天就晚上数据的收发了,剩下的就是数 ...

  6. 微信小程序之购物车功能

    前言 以往的购物车,基本都是通过大量的 DOM 操作来实现.微信小程序其实跟 vue.js 的用法非常像,接下来就看看小程序可以怎样实现购物车功能. 需求 先来弄清楚购物车的需求. 单选.全选和取消, ...

  7. [转]微信小程序之购物车功能

    本文转自:https://www.cnblogs.com/linxin/p/6834206.html 前言 以往的购物车,基本都是通过大量的 DOM 操作来实现.微信小程序其实跟 vue.js 的用法 ...

  8. 【微信小程序】转载:微信小程序之购物车功能

    前言 以往的购物车,基本都是通过大量的 DOM 操作来实现.微信小程序其实跟 vue.js 的用法非常像,接下来就看看小程序可以怎样实现购物车功能. 需求 先来弄清楚购物车的需求. 单选.全选和取消, ...

  9. 运用wxs制作微信小程序左滑功能和跳转,性能更优越

    锲子 微信小程序自定义左滑功能加上跳转,换成以往,左滑功能的逻辑一般是在js中实现,但在拖动方面,性能并不是那么的流畅.如今,官方新扩展了一套脚本语言wxs,在IOS设备上运行,性能会比JS快2~20 ...

随机推荐

  1. Latex 特殊字符汇总

  2. git配置修改,git自定义命令,合并命令

    一.自定义合并命令 将add commit push合并 git config --global alias.cmp '!f() { git add -A && git commit ...

  3. 关于SQL Server 各种安装失败均失败,报错“等待数据库引擎恢复句柄失败”的经验分享

    最近安装SQL 2019遇到这个问题,试过自己合网上几乎所有办法,怎么都安装不上,最后在微软社区解决了,由于这个问题比较特殊,并且网上几乎没有正确的决绝方案,因此将我的解决过程及经验记录分享一下,也为 ...

  4. Jmeter beanshell把数据写入csv文件中,最后清除csv数据

    有时候我们需要使用jmeter去结合csv文件去做一些简单的数据驱动处理: 例如把数据库数据黏贴到csv文件中或者把网页上的数据填入到csv文件中: 直接我一般是用手自己黏贴复制过csv文件中,比较麻 ...

  5. Django基础七之CBV装饰器和中间件

    Django基础七之CBV装饰器和中间件 目录 Django基础七之CBV装饰器和中间件 1. CBV加装饰器 2. Django中间件 2.1 Django中间件介绍 2.2 自定义中间件 2.2. ...

  6. openEuler网络配置+换源+桌面环境ukui等基本环境部署

    镜像下载.域名解析.时间同步请点击阿里云开源镜像站 1.网络配置 你可以选择查看官方文档进行配置:配置网络 (openeuler.org) 接下来的操作基本都需要root权限,所以直接使用root用户 ...

  7. LFS系列镜像在阿里云镜像站首发上线

    LFS镜像 镜像详情页: https://developer.aliyun.com/mirror/lfs Linux From Scratch (LFS) 是一个项目,它为您提供完全从源代码构建您自己 ...

  8. 超详细GoodSync11.2.7.8单机、两个服务器之间的文件同步使用教程

    GoodSync安装教程 第一步:双机GoodSync_v11.2.7.8.exe文件 链接:https://pan.baidu.com/s/16FVater4f9vu07QiGGIK9A 提取码:b ...

  9. vue学习过程总结(08) - vue开发报错提示缺少本地文件的包

    vue开发启动过程会报错某个src下自己写的包找不到为安装,原因有两个 1.import的from后面的路径不正确 2.如果开发中用到了scss是也会一直报这个错,这时候可能你没有安装scss加载器, ...

  10. S2-045(RCE远程代码执行)

    环境搭建: https://blog.csdn.net/qq_36374896/article/details/84145020 漏洞复现 进入漏洞环境 (048和045一样) cd vulhub-m ...