使用小程序进行拖拽排序时,发现由于频繁的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 <=

最低0.47元/天 解锁文章
3891

被折叠的 条评论
为什么被折叠?



