uniapp 拖拽圖片排序功能實現(xiàn) 類似于微信朋友圈效果
更新時間:2024年07月19日 10:08:39 作者:星 辰.
這篇文章主要介紹了uniapp 拖拽圖片排序功能實現(xiàn)類似于微信朋友圈,一部分是拖拽的放大做小,xy位置判定,圖片數(shù)組的插入排序,另一部分是上傳加號圖片的?定位? 動態(tài)計算分為幾列,每一個圖片大小的位置?絕對定位的計算,需要的朋友可以參考下
1、效果展示

2、一部分是拖拽的放大做小,xy位置判定,圖片數(shù)組的插入排序,另一部分是上傳加號圖片的 定位 動態(tài)計算分為幾列,每一個圖片大小的位置 絕對定位的計算
<template>
<view class="con">
<template v-if="viewWidth">
<movable-area class="area" :style="{ height: areaHeight }" @mouseenter="mouseenter"
@mouseleave="mouseleave">
<movable-view v-for="(item, index) in imageList" :key="item.id" class="view" direction="all" :y="item.y"
:x="item.x" :damping="40" :disabled="item.disable" @change="onChange($event, item)"
@touchstart="touchstart(item)" @mousedown="touchstart(item)" @touchend="touchend(item)"
@mouseup="touchend(item)" :style="{
width: viewWidth + 'px',
height: viewWidth + 'px',
'z-index': item.zIndex,
opacity: item.opacity
}">
<view class="area-con" :style="{
width: childWidth,
height: childWidth,
borderRadius: borderRadius + 'rpx',
transform: 'scale(' + item.scale + ')'
}">
<image class="pre-image" :src="item.src" mode="aspectFill"></image>
<view class="del-con" @click="delImages(item, index)" @touchstart.stop="delImageMp(item, index)"
@touchend.stop="nothing()" @mousedown.stop="nothing()" @mouseup.stop="nothing()">
<view class="del-wrap">
<image class="del-image"
src="data:image/svg+xml;base64,PD94bWwgdmVyc2lvbj0iMS4wIiBzdGFuZGFsb25lPSJubyI/PjwhRE9DVFlQRSBzdmcgUFVCTElDICItLy9XM0MvL0RURCBTVkcgMS4xLy9FTiIgImh0dHA6Ly93d3cudzMub3JnL0dyYXBoaWNzL1NWRy8xLjEvRFREL3N2ZzExLmR0ZCI+PHN2ZyBjbGFzcz0iaWNvbiIgd2lkdGg9IjIwMHB4IiBoZWlnaHQ9IjIwMC4wMHB4IiB2aWV3Qm94PSIwIDAgMTAyNCAxMDI0IiB2ZXJzaW9uPSIxLjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyI+PHBhdGggZD0iTTYyNS40MjUzMDYgNjgxLjU4OTQ2NmwtMTE1LjQ3Mjk0MS0xMTUuNDcyOTQxLTExNS40NzE5MTggMTE1LjQ3Mjk0MS01NC4xMTc1NDgtNTQuMTE4NTcyIDExNS40NzE5MTgtMTE1LjQ3MTkxOC0xMTUuNDcxOTE4LTExNS40NzI5NDEgNTQuMTE3NTQ4LTU0LjExNzU0OCAxMTUuNDcxOTE4IDExNS40NzE5MTggMTE1LjQ3Mjk0MS0xMTUuNDcxOTE4IDU0LjExNzU0OCA1NC4xMTc1NDgtMTE1LjQ3Mjk0MSAxMTUuNDcyOTQxIDExNS40NzI5NDEgMTE1LjQ3MTkxOEw2MjUuNDI1MzA2IDY4MS41ODk0NjZ6TTc4MC41NDMxNzYgMjQxLjQwOTE4OWMtMTQ4LjgyNDUzNy0xNDguODI0NTM3LTM5Mi4zNTYwNjMtMTQ4LjgyNDUzNy01NDEuMTgwNiAwcy0xNDguODI0NTM3IDM5Mi4zNTUwMzkgMCA1NDEuMTc5NTc2IDM5Mi4zNTYwNjMgMTQ4LjgyNDUzNyA1NDEuMTgwNiAwQzkyOS4zNjc3MTMgNjMzLjc2NTI1MSA5MjkuMzY3NzEzIDM5MC4yMzM3MjYgNzgwLjU0MzE3NiAyNDEuNDA5MTg5eiIgZmlsbD0iI2RiZGJkYiIgLz48L3N2Zz4=">
</image>
</view>
</view>
</view>
</movable-view>
<view class="add" v-if="imageList.length < number"
:style="{ top: add.y, left: add.x, width: viewWidth + 'px', height: viewWidth + 'px' }"
@click="addImages">
<view class="add-wrap"
:style="{ width: childWidth, height: childWidth, borderRadius: borderRadius + 'rpx' }">
+
</view>
</view>
</movable-area>
</template>
</view>
</template>
<script>
export default {
emits: ['input', 'update:modelValue'],
props: {
// 排序圖片
value: {
type: Array,
default: function() {
return []
}
},
// 排序圖片
modelValue: {
type: Array,
default: function() {
return []
}
},
// 從 list 元素對象中讀取的鍵名
keyName: {
type: String,
default: null
},
// 選擇圖片數(shù)量限制
number: {
type: Number,
default: 9
},
// 圖片父容器寬度(實際顯示的圖片寬度為 imageWidth / 1.1 ),單位 rpx
// imageWidth > 0 則 cols 無效
imageWidth: {
type: Number,
default: 0
},
// 圖片列數(shù)
cols: {
type: Number,
default: 3
},
// 圖片圓角,單位 rpx
borderRadius: {
type: Number,
default: 10
},
// 圖片周圍空白填充,單位 rpx
padding: {
type: Number,
default: 10
},
// 拖動圖片時放大倍數(shù) [0, ∞)
scale: {
type: Number,
default: 1.1
},
// 拖動圖片時不透明度
opacity: {
type: Number,
default: 0.7
},
// 自定義添加
addImage: {
type: Function,
default: null
},
// 刪除確認
delImage: {
type: Function,
default: null
}
},
data() {
return {
imageList: [],
width: 0,
add: {
x: 0,
y: 0
},
colsValue: 0,
viewWidth: 0,
tempItem: null,
timer: null,
changeStatus: true,
preStatus: true,
first: true,
}
},
computed: {
areaHeight() {
let height = ''
// return '355px'
if (this.imageList.length < this.number) {
height = (Math.ceil((this.imageList.length + 1) / this.colsValue) * this.viewWidth).toFixed() + 'px'
} else {
height = (Math.ceil(this.imageList.length / this.colsValue) * this.viewWidth).toFixed() + 'px'
}
console.log('areaHeight', height)
return height
},
childWidth() {
return this.viewWidth - this.rpx2px(this.padding) * 2 + 'px'
},
},
watch: {
value: {
handler(n) {
if (!this.first && this.changeStatus) {
console.log('watch', n)
let flag = false
for (let i = 0; i < n.length; i++) {
if (flag) {
this.addProperties(this.getSrc(n[i]))
continue
}
if (this.imageList.length === i || this.imageList[i].src !== this.getSrc(n[i])) {
flag = true
this.imageList.splice(i)
this.addProperties(this.getSrc(n[i]))
}
}
}
},
deep: true
},
modelValue: {
handler(n) {
if (!this.first && this.changeStatus) {
console.log('watch', n)
let flag = false
for (let i = 0; i < n.length; i++) {
if (flag) {
this.addProperties(this.getSrc(n[i]))
continue
}
if (this.imageList.length === i || this.imageList[i].src !== this.getSrc(n[i])) {
flag = true
this.imageList.splice(i)
this.addProperties(this.getSrc(n[i]))
}
}
}
},
deep: true
},
},
created() {
// 獲取設備寬度
this.width = uni.getSystemInfoSync().windowWidth
},
mounted() {
// 獲取當前的存放移動區(qū)域的屬性
const query = uni.createSelectorQuery().in(this)
query.select('.con').boundingClientRect(data => {
// 設置的三列 進行傳值
this.colsValue = this.cols
// 元素寬度除以三進行均分
this.viewWidth = data.width / this.cols
if (this.imageWidth > 0) {
this.viewWidth = this.rpx2px(this.imageWidth)
this.colsValue = Math.floor(data.width / this.viewWidth)
}
let list = this.value
// #ifdef VUE3
list = this.modelValue
// #endif
for (let item of list) {
this.addProperties(this.getSrc(item))
}
this.first = false
})
query.exec()
},
methods: {
// 傳的如果有鍵值就采取這個方法。沒有就不需要
getSrc(item) {
if (this.keyName !== null) {
return item[this.keyName]
}
return item
},
onChange(e, item) {
if (!item) return
item.oldX = e.detail.x
item.oldY = e.detail.y
// 如果是拖動狀態(tài)中
if (e.detail.source === 'touch') {
if (item.moveEnd) {
item.offset = Math.sqrt(Math.pow(item.oldX - item.absX * this.viewWidth, 2) + Math.pow(item.oldY -
item
.absY * this.viewWidth, 2))
}
// x為 移動時候的坐標點加上劃定的一半的區(qū)域的和 除以劃定區(qū)域 去判斷有沒有超過自己定義的最大列數(shù)
let x = Math.floor((e.detail.x + this.viewWidth / 2) / this.viewWidth)
if (x >= this.colsValue) return
// y同理 也是判斷超過高度沒有
let y = Math.floor((e.detail.y + this.viewWidth / 2) / this.viewWidth)
// index則是 如果是第一張圖片右移動了半張圖片的位置 index就會加一
let index = this.colsValue * y + x
if (item.index != index && index < this.imageList.length) {
this.changeStatus = false
for (let obj of this.imageList) {
// 判斷圖片是左右上下移動 因為這個函數(shù)是一直觸發(fā)
if (item.index > index && obj.index >= index && obj.index < item.index) {
this.change(obj, 1)
} else if (item.index < index && obj.index <= index && obj.index > item.index) {
this.change(obj, -1)
} else if (obj.id != item.id) {
obj.offset = 0
obj.x = obj.oldX
obj.y = obj.oldY
setTimeout(() => {
this.$nextTick(() => {
obj.x = obj.absX * this.viewWidth
obj.y = obj.absY * this.viewWidth
})
}, 0)
}
}
item.index = index
item.absX = x
item.absY = y
if (!item.moveEnd) {
setTimeout(() => {
this.$nextTick(() => {
item.x = item.absX * this.viewWidth
item.y = item.absY * this.viewWidth
})
}, 0)
}
// console.log('bbb', JSON.parse(JSON.stringify(item)));
// 移動完成后重新排序
this.sortList()
}
}
},
// change事件會隨著移動函數(shù)一直觸發(fā),index隨之變化修改圖片的位置xy
change(obj, i) {
obj.index += i
obj.offset = 0
obj.x = obj.oldX
obj.y = obj.oldY
obj.absX = obj.index % this.colsValue
obj.absY = Math.floor(obj.index / this.colsValue)
setTimeout(() => {
this.$nextTick(() => {
obj.x = obj.absX * this.viewWidth
obj.y = obj.absY * this.viewWidth
})
}, 0)
},
// 長按圖片時候進行所有的層級進行加大 和放大
touchstart(item) {
this.imageList.forEach(v => {
v.zIndex = v.index + 9
})
item.zIndex = 99
item.moveEnd = true
this.tempItem = item
this.timer = setTimeout(() => {
item.scale = this.scale
item.opacity = this.opacity
clearTimeout(this.timer)
this.timer = null
}, 200)
},
// 點擊一次沒有觸發(fā)四個變量的更改就會觸發(fā)previewImage 變成預覽
// 拖拽過程中幾個變量會變,就不會觸發(fā)預覽 拖拽結束后就會縮小并且改變位置
touchend(item) {
this.previewImage(item)
item.scale = 1
item.opacity = 1
item.x = item.oldX
item.y = item.oldY
item.offset = 0
item.moveEnd = false
setTimeout(() => {
this.$nextTick(() => {
item.x = item.absX * this.viewWidth
item.y = item.absY * this.viewWidth
this.tempItem = null
this.changeStatus = true
})
// console.log('ccc', JSON.parse(JSON.stringify(item)));
}, 0)
// console.log('ddd', JSON.parse(JSON.stringify(item)));
},
previewImage(item) {
// timer是定時器 changeStatus是不是在移動狀態(tài)中 只要點擊移動了就是false offset也是為0
if (this.timer && this.preStatus && this.changeStatus && item.offset < 28.28) {
clearTimeout(this.timer)
this.timer = null
const list = this.value || this.modelValue
let srcList = list.map(v => this.getSrc(v))
console.log(list, srcList);
uni.previewImage({
urls: srcList,
current: item.src,
success: () => {
this.preStatus = false
setTimeout(() => {
this.preStatus = true
}, 600)
},
fail: (e) => {
console.log(e);
}
})
} else if (this.timer) {
clearTimeout(this.timer)
this.timer = null
}
},
mouseenter() {
//#ifdef H5
this.imageList.forEach(v => {
v.disable = false
})
//#endif
},
mouseleave() {
//#ifdef H5
if (this.tempItem) {
this.imageList.forEach(v => {
v.disable = true
v.zIndex = v.index + 9
v.offset = 0
v.moveEnd = false
if (v.id == this.tempItem.id) {
if (this.timer) {
clearTimeout(this.timer)
this.timer = null
}
v.scale = 1
v.opacity = 1
v.x = v.oldX
v.y = v.oldY
this.$nextTick(() => {
v.x = v.absX * this.viewWidth
v.y = v.absY * this.viewWidth
this.tempItem = null
})
}
})
this.changeStatus = true
}
//#endif
},
addImages() {
if (typeof this.addImage === 'function') {
this.addImage.bind(this.$parent)()
} else {
let checkNumber = this.number - this.imageList.length
uni.chooseImage({
count: checkNumber,
sourceType: ['album', 'camera'],
success: res => {
let count = checkNumber <= res.tempFilePaths.length ? checkNumber : res
.tempFilePaths.length
for (let i = 0; i < count; i++) {
this.addProperties(res.tempFilePaths[i])
}
this.sortList()
}
})
}
},
delImages(item, index) {
if (typeof this.delImage === 'function') {
this.delImage.bind(this.$parent)(() => {
this.delImageHandle(item, index)
})
} else {
this.delImageHandle(item, index)
}
},
delImageHandle(item, index) {
this.imageList.splice(index, 1)
for (let obj of this.imageList) {
if (obj.index > item.index) {
obj.index -= 1
obj.x = obj.oldX
obj.y = obj.oldY
obj.absX = obj.index % this.colsValue
obj.absY = Math.floor(obj.index / this.colsValue)
this.$nextTick(() => {
obj.x = obj.absX * this.viewWidth
obj.y = obj.absY * this.viewWidth
})
}
}
this.add.x = (this.imageList.length % this.colsValue) * this.viewWidth + 'px'
this.add.y = Math.floor(this.imageList.length / this.colsValue) * this.viewWidth + 'px'
this.sortList()
},
delImageMp(item, index) {
//#ifdef MP
this.delImages(item, index)
//#endif
},
sortList() {
console.log('sortList');
const result = []
let source = this.value
// #ifdef VUE3
source = this.modelValue
// #endif
// 使用slice進行深拷貝
let list = this.imageList.slice()
// 小到大排序
list.sort((a, b) => {
return a.index - b.index
})
for (let s of list) {
let item = source.find(d => this.getSrc(d) == s.src)
if (item) {
result.push(item)
} else {
if (this.keyName !== null) {
result.push({
[this.keyName]: s.src
})
} else {
result.push(s.src)
}
}
}
this.$emit("input", result);
this.$emit("update:modelValue", result);
},
addProperties(item) {
console.log(item);
// 這里的數(shù)組長度還沒有。計算后push進去才從0開始計算
// 數(shù)組長度取列數(shù)的余數(shù) 1就取1 2就取2 3就是0 4就是1
let absX = this.imageList.length % this.colsValue
// 向下取整數(shù)組長度除以列數(shù) 1/3取0 4/3取1
let absY = Math.floor(this.imageList.length / this.colsValue)
let x = absX * this.viewWidth
let y = absY * this.viewWidth
this.imageList.push({
src: item,
x,
y,
oldX: x,
oldY: y,
absX,
absY,
scale: 1,
zIndex: 9,
opacity: 1,
index: this.imageList.length,
id: this.guid(16),
disable: false,
offset: 0,
moveEnd: false
})
this.add.x = (this.imageList.length % this.colsValue) * this.viewWidth + 'px'
this.add.y = Math.floor(this.imageList.length / this.colsValue) * this.viewWidth + 'px'
},
nothing() {},
rpx2px(v) {
return this.width * v / 750
},
guid(len = 32) {
const chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'.split('')
const uuid = []
const radix = chars.length
for (let i = 0; i < len; i++) uuid[i] = chars[0 | Math.random() * radix]
uuid.shift()
return `u${uuid.join('')}`
}
}
}
</script>
<style lang="scss" scoped>
.con {
// padding: 30rpx;
.area {
width: 100%;
.view {
display: flex;
justify-content: center;
align-items: center;
.area-con {
position: relative;
overflow: hidden;
.pre-image {
width: 100%;
height: 100%;
}
.del-con {
position: absolute;
top: 0rpx;
right: 0rpx;
padding: 0 0 20rpx 20rpx;
.del-wrap {
width: 40rpx;
height: 40rpx;
// background-color: rgba(0, 0, 0, 0.4);
border-radius: 0 0 0 10rpx;
display: flex;
justify-content: center;
align-items: center;
.del-image {
width: 40rpx;
height: 40rpx;
}
}
}
}
}
.add {
position: absolute;
display: flex;
justify-content: center;
align-items: center;
.add-wrap {
display: flex;
justify-content: center;
align-items: center;
border: 1rpx solid #000;
// background-color: #eeeeee;
}
}
}
}
</style>到此這篇關于uniapp 拖拽圖片排序, 類似于微信朋友圈的文章就介紹到這了,更多相關uniapp 拖拽圖片排序內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
vue2?自定義?el-radio-button?的樣式并設置默認值的方法
這篇文章主要介紹了vue2?自定義?el-radio-button?的樣式并設置默認值的操作方法,代碼分為html部分和css修改樣式代碼,代碼簡單易懂,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-10-10
解決Vue的項目使用Element ui 走馬燈無法實現(xiàn)的問題
這篇文章主要介紹了解決Vue的項目使用Element ui 走馬燈無法實現(xiàn)的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-08-08
Vue CLI3.0中使用jQuery和Bootstrap的方法
這篇文章主要介紹了Vue CLI3.0中使用jQuery和Bootstrap的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-02-02

