使用taro完成微信小程序拖拽排序

3 篇文章 0 订阅
1 篇文章 0 订阅

使用MovableArea, MovableView

支持节点任意大小,任意拖拽排序

完整代码,复制就可以使用

import React, { useEffect, useState } from "react";
import { MovableArea, MovableView, View } from "@tarojs/components";

export default () => {

    const [changeId, setChangeId] = useState();
    const [initData, setInitData] = useState([]);
    const [dragList, setDragList] = useState([]);

    const initMove = () => {
        const setY = (index) => {
            let y = 0;
            let _data = dragList.slice(0, index)
            _data.forEach(item => {
                y += item.height;
            })
            y /= 2;
            return y
        }
        setInitData(dragList.map((item, index) => {
            if (index) {
                item.y = setY(index);
            } else {
                item.y = 0
            }
            return item
        }))
    }
    useEffect(() => {
        setDragList([
            {
                id: 0,
                height: 100,
                color: 'red',
                y: 0
            },
            {
                id: 1,
                height: 50,
                color: 'pink',
                y: 0
            },
            {
                id: 2,
                height: 200,
                color: 'blue',
                y: 0
            },
            {
                id: 3,
                height: 300,
                color: 'gray',
                y: 0
            }
        ])
    }, [])

    useEffect(() => {
        let time = setTimeout(() => {
            initMove();
        }, 1000)
        return () => {
            clearTimeout(time);
        }
    }, [dragList])

    return (
        <>
            <MovableArea style='height: 600px; width:350px; background: #00bcd2;'>
                {
                    initData.map((item) => {
                        return (
                            <MovableView
                                onChange={(e) => {
                                    setChangeId(item.id);
                                    setDragList(dragList.map(_item => {
                                        if (_item.id == item.id) {
                                            _item.y = e.detail.y;
                                        }
                                        return _item;
                                    }).sort((a, b) => a.y - b.y));
                                }}
                                out-of-bounds
                                direction='vertical'
                                animation={false}
                                y={item.y}
                                style={{
                                    width: '750rpx',
                                    height: item.height + 'rpx',
                                    backgroundColor: item.color,
                                    zIndex: item.id == changeId ? 2 : 1,
                                }}>
                                <View>id: {item.id}</View>
                            </MovableView>
                        )
                    })
                }
            </MovableArea>
        </>
    )
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值