微信小程序使用wxs实现拖拽排序

使用小程序进行拖拽排序时,发现由于频繁的setData更改坐标,拖动的时候会产生很大的卡顿,然后发现使用wxs可以解决这个卡顿问题
直接上代码
filter.wxs

// 将需要计算的参数传过来
var windowHeight = 0;//屏幕高度
var rowHeight = 0;//每一项的高度
var startY = 0;//移动定位的top 位置
var scrollViewY = 0;//scroll-view列表顶部距离屏幕顶部距离 - 每一项高度的一半
var showScrollTop = 0;//滚动条滚动距离
var initialIndex = -1;//初始索引
var moveoutindex = -1;//移动到哪个索引

var instances = [], instanceLen;

function startYFun() {
   
  return startY;
}
// 滚动条位置
function scrolltoupper(e, ownerInstance) {
   
  showScrollTop = e.detail.scrollTop;
  ownerInstance.callMethod('showScrollTopFun', {
   
    showScrollTop: showScrollTop
  });
};

// 长按事件
function onLongPress(e, ownerInstance) {
   
  // 震动
  ownerInstance.callMethod('triggerShake');
  // 参数
  rowHeight = e.currentTarget.dataset.rowheight;
  scrollViewY = e.currentTarget.dataset.scrollviewy;

  startY = e.touches[0].clientY - rowHeight / 2; //   距离屏幕顶部高度-每一项高度的一半
  moveoutindex = parseInt((startY + showScrollTop - scrollViewY) / rowHeight);
  initialIndex = e.currentTarget.dataset.index;
  if (initialIndex <= moveoutindex) moveoutindex++;

  // startYFun();
  // 传参执行js方法onLongPressWxs
  ownerInstance.callMethod('onLongPressWxs', {
   
    initialIndex: initialIndex,
    moveoutindex: moveoutindex
  });


  instances = ownerInstance.selectAllComponents('.forItem.itemExist'), instanceLen = instances.length;
  // for (var i = 0; i < instanceLen; i++) {
   
    var instance = instances[initialIndex];
    instance.setStyle({
   
      'top': startY + 'px',
    })
  // }
}
// 移动或移动结束事件
function listitemmove(e, ownerInstance) {
   
  if (initialIndex == -1) return;
  if (e.type == "touchmove") {
   
    
    startY = e.touches[0].clientY - rowHeight / 2; //   距离屏幕顶部高度-每一项高度的一半
    moveoutindex = parseInt((startY + showScrollTop - scrollViewY) / rowHeight);
    if (moveoutindex <= 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值