微信小程序?qū)崿F(xiàn)圖片拖拽調(diào)換位置效果(開箱即用)
在編寫類似發(fā)布朋友圈功能的功能時(shí),需要實(shí)現(xiàn)圖片的拖拽排序,刪除圖片等功能。
一、效果展示

二、示例代碼
1.1、在自己的小程序中創(chuàng)建組件
1.2、組件源碼 wxml代碼
<view class="drag-container">
<view wx:for="{{dragImgList}}" wx:key="id"
style="transform: translate({{index === currentIndex ? tranX : item.tranX}}px, {{index === currentIndex ? tranY : item.tranY}}px); z-index: {{index === currentIndex ? 10 : 1}}; width: {{previewSize}}px; height: {{previewSize}}px;"
class="drag-item drag-item-transition"
mark:index="{{index}}" mark:key="{{item.key}}"
catch:longpress="longPress"
catch:touchmove="touchMove"
catch:touchend="touchEnd">
<image class="drag-item-img" src="{{item.src}}" mode="aspectFill"/>
<view catch:tap="deleteImg" mark:key="{{item.key}}" class="drag-item-delete">
<view class="drag-item-delete_default" style="{{deleteStyle}}">x</view>
</view>
</view>
<view bindtap="uploadImage" class="drag-item drag-upload" hidden="{{dragImgList.length >= maxCount}}" style="transform: translate({{uploadPosition.tranX}}px, {{uploadPosition.tranY}}px); width: {{previewSize}}px; height: {{previewSize}}px;">
<text>+</text>
</view>
</view>wxss代碼
.drag-container {
position: relative;
left: 30rpx;
top: 20rpx;
}
.drag-item {
position: absolute;
top: 0;
left: 0;
}
.drag-item-transition {
transition: transform 0.1s
}
.drag-item-img {
width: 100%;
height: 100%;
}
.drag-item-delete {
position: absolute;
top: 0;
right: 0;
}
.drag-item-delete_default {
display: flex;
width: 21px;
height: 15px;
line-height: 10px;
justify-content: center;
background-color: rgba(0, 0, 0, 0.7);
border-radius: 0 0 0 12px;
color: #FEFEFE;
}
.drag-upload {
display: flex;
justify-content: center;
align-items: center;
border: 2px dashed silver;
width: 100%;
height: 100%;
box-sizing: border-box;
font-size: 70px;
}
.drag-upload text{
margin-top: -20%;
color: silver;
}js代碼
Component({
properties: {
// 每個(gè)格子的大小 100*100
previewSize: {
type: Number,
value: 100
},
// 默認(rèn)圖片列表
defaultImgList: {
type: Array,
value: [],
observer(t) {
if (t?.length && !this.data.dragImgList.length) {
const e = this.getDragImgList(t);
this.setUploaPosition(e.length), this.setData({
dragImgList: e
})
}
}
},
// 最大個(gè)數(shù)
maxCount: {
type: Number,
value: 9
},
// 每行列數(shù)
columns: {
type: Number,
value: 3
},
// 每個(gè)格子之間的間隔
gap: {
type: Number,
value: 9
},
deleteStyle: {
type: String,
value: ""
}
},
data: {
dragImgList: [],
containerRes: {
top: 0,
left: 0,
width: 0,
height: 0
},
currentKey: -1,
currentIndex: -1,
tranX: 0,
tranY: 0,
uploadPosition: {
tranX: 0,
tranY: 0
}
},
lifetimes: {
ready() {
this.createSelectorQuery().select(".drag-container").boundingClientRect((({
top: t,
left: e
}) => {
this.setData({
"containerRes.top": t,
"containerRes.left": e
})
})).exec()
}
},
methods: {
longPress(t) {
const e = t.mark.index,
{
pageX: a,
pageY: i
} = t.touches[0],
{
previewSize: s,
containerRes: {
top: n,
left: r
}
} = this.data;
this.setData({
currentIndex: e,
tranX: a - s / 2 - r,
tranY: i - s / 2 - n
})
},
touchMove(t) {
if (this.data.currentIndex < 0) return;
const {
pageX: e,
pageY: a
} = t.touches[0], {
previewSize: i,
containerRes: {
top: s,
left: n
}
} = this.data, r = e - i / 2 - n, o = a - i / 2 - s;
this.setData({
tranX: r,
tranY: o
});
const h = t.mark.key,
g = this.getMoveKey(r, o);
h !== g && this.data.currentKey !== h && (this.data.currentKey = h, this.replace(h, g))
},
getMoveKey(t, e) {
const {
dragImgList: a,
previewSize: i,
columns: s
} = this.data, n = (t, e) => {
const a = Math.round(t / i);
return a >= e ? e - 1 : a < 0 ? 0 : a
}, r = s * n(e, Math.ceil(a.length / s)) + n(t, s);
return r >= a.length ? a.length - 1 : r
},
replace(t, e) {
const a = this.data.dragImgList;
a.forEach((a => {
t < e ? a.key > t && a.key <= e ? a.key-- : a.key === t && (a.key = e) : t > e && (a.key >= e && a.key < t ? a.key++ : a.key === t && (a.key = e))
})), this.getListPosition(a)
},
getListPosition(t) {
const {
previewSize: e,
columns: a,
gap: i
} = this.data, s = t.map((t => (t.tranX = (e + i) * (t.key % a), t.tranY = Math.floor(t.key / a) * (e + i), t)));
this.setData({
dragImgList: s
}), this.updateEvent(s)
},
touchEnd() {
this.setData({
tranX: 0,
tranY: 0,
currentIndex: -1
}), this.data.currentKey = -1
},
updateEvent(t) {
const e = [...t].sort(((t, e) => t.key - e.key)).map((t => t.src));
this.triggerEvent("updateImageList", {
list: e
})
},
async uploadImage() {
let {
dragImgList: t,
maxCount: e
} = this.data;
try {
const a = await wx.chooseMedia({
count: e - t.length,
mediaType: ["image"]
}),
i = this.getDragImgList(a?.tempFiles?.map((({
tempFilePath: t
}) => t)) || [], !1);
t = t.concat(i), this.setUploaPosition(t.length), this.setData({
dragImgList: t
}), this.updateEvent(t)
} catch (t) {
console.log(t)
}
},
getContainerRect(t) {
const {
columns: e,
previewSize: a,
maxCount: i,
gap: s
} = this.data, n = t === i ? t : t + 1, r = Math.ceil(n / e);
return {
width: e * a + (e - 1) * s,
height: r * a + s * (r - 1)
}
},
getDragImgList(t, e = !0) {
let {
dragImgList: a,
previewSize: i,
columns: s,
gap: n
} = this.data;
return t.map(((t, r) => {
const o = (e ? 0 : a.length) + r;
return {
tranX: (i + n) * (o % s),
tranY: Math.floor(o / s) * (i + n),
src: t,
id: o,
key: o
}
}))
},
setUploaPosition(t) {
const {
previewSize: e,
columns: a,
gap: i
} = this.data, s = {
tranX: t % a * (e + i),
tranY: Math.floor(t / a) * (e + i)
}, {
width: n,
height: r
} = this.getContainerRect(t);
this.setData({
uploadPosition: s,
"containerRes.width": n,
"containerRes.height": r
})
},
deleteImg(t) {
const e = t.mark.key,
a = this.data.dragImgList.filter((t => t.key !== e));
a.forEach((t => {
t.key > e && t.key--
})), this.getListPosition(a), this.setUploaPosition(a.length)
}
}
});json代碼
{
"component": true,
"usingComponents":{}
}1.3、在自己的小程序中新建page
1.4、新建page的源碼 wxml代碼
<view>
<wxDragImg
defaultImgList="{{imgList}}"
previewSize="{{120}}"
maxCount="{{9}}"
columns="{{3}}"
gap="{{10}}"
bind:updateImageList="updateImageList">
</wxDragImg>
</view>js代碼
Page({
data: {
imgList: []
},
onLoad() {},
updateImageList(e) {
console.log(e)
}
})json代碼
{
"usingComponents": {
"wxDragImg": "../wx-drag-img"
}
}到此這篇關(guān)于微信小程序?qū)崿F(xiàn)圖片拖拽調(diào)換位置效果 -- 開箱即用的文章就介紹到這了,更多相關(guān)微信小程序圖片拖拽調(diào)換位置內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
功能強(qiáng)大的Bootstrap組件(結(jié)合js)
這篇文章主要介紹了功能強(qiáng)大的Bootstrap組件,介紹js結(jié)合Bootstrap組件的使用方法,感興趣的小伙伴們可以參考一下2016-08-08
地址欄傳遞中文參數(shù)亂碼在js里用escape轉(zhuǎn)碼
亂碼,頭大的一件事可以考慮轉(zhuǎn)碼方式不直接傳中文,在js里用escape轉(zhuǎn)碼,具體實(shí)現(xiàn)如下,有類似情況的朋友可以參考下2013-08-08
精通Javascript系列之Javascript基礎(chǔ)篇
javascrpit的基本概念分析,剛開始學(xué)習(xí)js的朋友可以參考下。2011-06-06
使用Turn.js實(shí)現(xiàn)翻書效果的完整步驟
最近項(xiàng)目經(jīng)理我個(gè)項(xiàng)目練練手,其項(xiàng)目需求是要實(shí)現(xiàn)翻書效果,下面這篇文章主要給大家介紹了關(guān)于使用Turn.js實(shí)現(xiàn)翻書效果的相關(guān)資料,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-12-12
javascript實(shí)現(xiàn)十六進(jìn)制顏色值(HEX)和RGB格式相互轉(zhuǎn)換
這篇文章主要介紹了javascript實(shí)現(xiàn)十六進(jìn)制顏色值(HEX)和RGB格式之間的轉(zhuǎn)換,使用正則的方法實(shí)現(xiàn)RGB顏色轉(zhuǎn)換為16進(jìn)制,需要的朋友可以參考下2014-06-06

