shmily-drag-image.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  1. <template>
  2. <view class="con">
  3. <movable-area class="area" :style="{ height: areaHeight }" @mouseenter="mouseenter" @mouseleave="mouseleave">
  4. <block v-for="(item, index) in imageList" :key="item.id">
  5. <movable-view
  6. class="view"
  7. :x="item.x"
  8. :y="item.y"
  9. direction="all"
  10. :damping="40"
  11. :disabled="item.disable"
  12. @change="onChange($event, item)"
  13. @touchstart="touchstart(item)"
  14. @mousedown="touchstart(item)"
  15. @touchend="touchend(item)"
  16. @mouseup="touchend(item)"
  17. :style="{ width: viewWidth + 'px', height: viewWidth + 'px', 'z-index': item.zIndex, opacity: item.opacity }"
  18. >
  19. <view class="area-con" :style="{ width: childWidth, height: childWidth, transform: 'scale(' + item.scale + ')' }">
  20. <image class="pre-image" :src="item.src" mode="aspectFill"></image>
  21. <view class="del-con" @click="delImage(item, index)" @touchstart.stop="delImageMp(item, index)" @touchend.stop="nothing()" @mousedown.stop="nothing()" @mouseup.stop="nothing()">
  22. <view class="del-wrap">
  23. <image
  24. class="del-image"
  25. src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACEAAAAhCAYAAABX5MJvAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAAAhdEVYdENyZWF0aW9uIFRpbWUAMjAyMDowNzoyNSAyMTo1NDoyOU4TkJAAAADcSURBVFhH7ZfRCoMwDEXLvkjwwVf/bH/emmAyN6glTW9WBjsgwm28OeCLpj81Sil7zvlJ90UiONS/yY5VogsO6XrBg3IEQ5a/s8vRSWUAKmLqp2w5jz5BiNQEGMo3GbloDLtFXJ1IkaEuhAiiY6gEIqB4yqACSk9piIBiKQ8VUFpLviKg3C2rESKgWERCBZSWiEfgIfffYvrrsAgoISJ3Apy3zuTxcSxLQkV6ykNEPKVQkZEyiAiiZKgDIaC4upACSlcn5fM/+WuDCAHF1E/Z/N9AhkMZnPNDPI+UDjPIXgAQIGjNAAAAAElFTkSuQmCC"
  26. ></image>
  27. </view>
  28. </view>
  29. </view>
  30. </movable-view>
  31. </block>
  32. <view
  33. class="add"
  34. v-if="imageList.length < number"
  35. :style="{ top: add.y, left: add.x, width: viewWidth + 'px', height: viewWidth + 'px' }"
  36. @click="addImages"
  37. >
  38. <view class="add-wrap" :style="{ width: childWidth, height: childWidth }">
  39. <image
  40. style="width: 54rpx;height: 54rpx;"
  41. src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADYAAAA2CAYAAACMRWrdAAABIUlEQVRoQ+2a2w2DMAxFeQzWrsMUbadAsEw3S1CqVgppKwLX8BEOP4iHTXx8uUgWdVXoVhdaV0VhSmf7vr/H8V3XzY6V3P9iD+nYOI5P7/01LMI596AwoZV0TIBXIUWFXhKLFBWYSFGhhxQN6SFFQ5i4ogITKSr0cEVDekjRECauqMBEigq9U7piOk2yAti27SUe5ljlTfPEQ6KZecTvwl4P3ytvOv06R2HDMNzes7+6aRrvnHvtf50L13Lp50rx88zcvNlS3JpwKQ67XyK04nq2nFbk/LqVjin0TvmBNgQ2S4UUDcliHgpMpKjQwxUN6SFFQ5i4ogITKSr0cEVDekjRECauqMAsVoph+hVPtYr5+03p9tbYQ96xrYtT4ootbAJGVxxVTapVswAAAABJRU5ErkJggg=="
  42. ></image>
  43. </view>
  44. </view>
  45. </movable-area>
  46. </view>
  47. </template>
  48. <script>
  49. export default {
  50. data() {
  51. return {
  52. imageList: [],
  53. width: 0,
  54. add: {
  55. x: 0,
  56. y: 0
  57. },
  58. colsValue: 0,
  59. viewWidth: 0,
  60. tempItem: null,
  61. timer: null,
  62. changeStatus: true,
  63. preStatus: true,
  64. }
  65. },
  66. props: {
  67. // 返回排序后图片
  68. list: {
  69. type: Array,
  70. default: function() {
  71. return []
  72. }
  73. },
  74. // 选择图片数量限制
  75. number: {
  76. type: Number,
  77. default: 6
  78. },
  79. // 图片父容器宽度(实际显示的图片宽度为 imageWidth / 1.1 ),单位 rpx
  80. imageWidth: {
  81. type: Number,
  82. default: 230
  83. },
  84. // 图片列数(cols > 0 则 imageWidth 无效)
  85. cols: {
  86. type: Number,
  87. default: 0
  88. },
  89. // 图片周围空白填充,单位 rpx
  90. padding: {
  91. type: Number,
  92. default: 10
  93. },
  94. // 拖动图片时放大倍数 [0, ∞)
  95. scale: {
  96. type: Number,
  97. default: 1.1
  98. },
  99. // 拖动图片时不透明度
  100. opacity: {
  101. type: Number,
  102. default: 0.7
  103. },
  104. // 自定义添加(需配合 @aaddImage 事件使用)
  105. custom: {
  106. type: Boolean,
  107. default: false
  108. }
  109. },
  110. computed: {
  111. areaHeight() {
  112. if (this.imageList.length < this.number) {
  113. return Math.ceil((this.imageList.length + 1) / this.colsValue) * this.viewWidth + 'px'
  114. } else {
  115. return Math.ceil(this.imageList.length / this.colsValue) * this.viewWidth + 'px'
  116. }
  117. },
  118. childWidth() {
  119. return this.viewWidth - this.rpx2px(this.padding) * 2 + 'px'
  120. },
  121. },
  122. created() {
  123. this.width = uni.getSystemInfoSync().windowWidth
  124. this.viewWidth = this.rpx2px(this.imageWidth)
  125. },
  126. mounted() {
  127. const query = uni.createSelectorQuery().in(this)
  128. query.select('.area').boundingClientRect(data => {
  129. this.colsValue = Math.floor(data.width / this.viewWidth)
  130. if(this.cols > 0){
  131. this.colsValue = this.cols
  132. this.viewWidth = data.width / this.cols
  133. }
  134. for (let item of this.list) {
  135. this.addProperties(item)
  136. }
  137. })
  138. query.exec()
  139. },
  140. methods: {
  141. onChange(e, item) {
  142. if(!item) return
  143. item.oldX = e.detail.x
  144. item.oldY = e.detail.y
  145. if (e.detail.source === 'touch') {
  146. if(item.moveEnd){
  147. item.offset = Math.sqrt(Math.pow(item.oldX - item.absX * this.viewWidth, 2) + Math.pow(item.oldY - item.absY * this.viewWidth, 2))
  148. }
  149. let x = Math.floor((e.detail.x + this.viewWidth / 2) / this.viewWidth)
  150. if(x >= this.colsValue) return
  151. let y = Math.floor((e.detail.y + this.viewWidth / 2) / this.viewWidth)
  152. let index = this.colsValue * y + x
  153. if (item.index != index && index < this.imageList.length) {
  154. this.changeStatus = false
  155. for (let obj of this.imageList) {
  156. if (item.index > index && obj.index >= index && obj.index < item.index) {
  157. this.change(obj, 1)
  158. } else if (item.index < index && obj.index <= index && obj.index > item.index) {
  159. this.change(obj, -1)
  160. } else if(obj.id != item.id) {
  161. obj.offset = 0
  162. obj.x = obj.oldX
  163. obj.y = obj.oldY
  164. setTimeout(() => {
  165. this.$nextTick(() => {
  166. obj.x = obj.absX * this.viewWidth
  167. obj.y = obj.absY * this.viewWidth
  168. })
  169. }, 0)
  170. }
  171. }
  172. item.index = index
  173. item.absX = x
  174. item.absY = y
  175. this.sortList()
  176. }
  177. }
  178. },
  179. change(obj, i){
  180. obj.index += i
  181. obj.offset = 0
  182. obj.x = obj.oldX
  183. obj.y = obj.oldY
  184. obj.absX = obj.index % this.colsValue
  185. obj.absY = Math.floor(obj.index / this.colsValue)
  186. setTimeout(() => {
  187. this.$nextTick(() => {
  188. obj.x = obj.absX * this.viewWidth
  189. obj.y = obj.absY * this.viewWidth
  190. })
  191. }, 0)
  192. },
  193. touchstart(item) {
  194. this.imageList.forEach(v => {
  195. v.zIndex = v.index + 9
  196. })
  197. item.zIndex = 99
  198. item.moveEnd = true
  199. this.tempItem = item
  200. this.timer = setTimeout(() => {
  201. item.scale = this.scale
  202. item.opacity = this.opacity
  203. clearTimeout(this.timer)
  204. this.timer = null
  205. }, 200)
  206. },
  207. touchend(item) {
  208. this.previewImage(item)
  209. item.scale = 1
  210. item.opacity = 1
  211. item.x = item.oldX
  212. item.y = item.oldY
  213. item.offset = 0
  214. item.moveEnd = false
  215. setTimeout(() => {
  216. this.$nextTick(() => {
  217. item.x = item.absX * this.viewWidth
  218. item.y = item.absY * this.viewWidth
  219. this.tempItem = null
  220. this.changeStatus = true
  221. })
  222. }, 0)
  223. },
  224. previewImage(item){
  225. if(this.timer && this.preStatus && this.changeStatus && item.offset < 28.28){
  226. clearTimeout(this.timer)
  227. this.timer = null
  228. let src = this.list.findIndex(v => v === item.src)
  229. uni.previewImage({
  230. urls: this.list,
  231. current: src,
  232. success: () => {
  233. this.preStatus = false
  234. setTimeout(() => {
  235. this.preStatus = true
  236. }, 600)
  237. }
  238. })
  239. } else if (this.timer){
  240. clearTimeout(this.timer)
  241. this.timer = null
  242. }
  243. },
  244. mouseenter(){
  245. //#ifdef H5
  246. this.imageList.forEach(v => {
  247. v.disable = false
  248. })
  249. //#endif
  250. },
  251. mouseleave(){
  252. //#ifdef H5
  253. if(this.tempItem){
  254. this.imageList.forEach(v => {
  255. v.disable = true
  256. v.zIndex = v.index + 9
  257. v.offset = 0
  258. v.moveEnd = false
  259. if(v.id == this.tempItem.id){
  260. if (this.timer){
  261. clearTimeout(this.timer)
  262. this.timer = null
  263. }
  264. v.scale = 1
  265. v.opacity = 1
  266. v.x = v.oldX
  267. v.y = v.oldY
  268. this.$nextTick(() => {
  269. v.x = v.absX * this.viewWidth
  270. v.y = v.absY * this.viewWidth
  271. this.tempItem = null
  272. })
  273. }
  274. })
  275. this.changeStatus = true
  276. }
  277. //#endif
  278. },
  279. addImages() {
  280. if(this.custom){
  281. this.$emit('addImage')
  282. } else {
  283. let checkNumber = this.number - this.imageList.length
  284. uni.chooseImage({
  285. count: checkNumber,
  286. sourceType: ['album', 'camera'],
  287. success: res => {
  288. let count = checkNumber <= res.tempFilePaths.length ? checkNumber : res.tempFilePaths.length
  289. for (let i = 0; i < count; i++) {
  290. this.addProperties(res.tempFilePaths[i])
  291. }
  292. }
  293. })
  294. }
  295. },
  296. addImage(image){
  297. this.addProperties(image)
  298. },
  299. delImage(item, index){
  300. this.imageList.splice(index, 1)
  301. for (let obj of this.imageList) {
  302. if (obj.index > item.index) {
  303. obj.index -= 1
  304. obj.x = obj.oldX
  305. obj.y = obj.oldY
  306. obj.absX = obj.index % this.colsValue
  307. obj.absY = Math.floor(obj.index / this.colsValue)
  308. this.$nextTick(() => {
  309. obj.x = obj.absX * this.viewWidth
  310. obj.y = obj.absY * this.viewWidth
  311. })
  312. }
  313. }
  314. this.add.x = (this.imageList.length % this.colsValue) * this.viewWidth + 'px'
  315. this.add.y = Math.floor(this.imageList.length / this.colsValue) * this.viewWidth + 'px'
  316. this.sortList()
  317. },
  318. delImageMp(item, index){
  319. //#ifdef MP
  320. this.delImage(item, index)
  321. //#endif
  322. },
  323. sortList() {
  324. let list = this.imageList.slice()
  325. list.sort((a, b) => {
  326. return a.index - b.index
  327. })
  328. for (let i = 0; i < list.length; i++) {
  329. list[i] = list[i].src
  330. }
  331. this.$emit('update:list', list)
  332. },
  333. addProperties(item){
  334. let absX = this.imageList.length % this.colsValue
  335. let absY = Math.floor(this.imageList.length / this.colsValue)
  336. let x = absX * this.viewWidth
  337. let y = absY * this.viewWidth
  338. this.imageList.push({
  339. src: item,
  340. x,
  341. y,
  342. oldX: x,
  343. oldY: y,
  344. absX,
  345. absY,
  346. scale: 1,
  347. zIndex: 9,
  348. opacity: 1,
  349. index: this.imageList.length,
  350. id: this.guid(),
  351. disable: false,
  352. offset: 0,
  353. moveEnd: false
  354. })
  355. this.add.x = (this.imageList.length % this.colsValue) * this.viewWidth + 'px'
  356. this.add.y = Math.floor(this.imageList.length / this.colsValue) * this.viewWidth + 'px'
  357. this.sortList()
  358. },
  359. nothing(){},
  360. rpx2px(v){
  361. return this.width * v / 750
  362. },
  363. guid() {
  364. function S4() {
  365. return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1);
  366. }
  367. return (S4() + S4() + "-" + S4() + "-" + S4() + "-" + S4() + "-" + S4() + S4() + S4());
  368. }
  369. }
  370. }
  371. </script>
  372. <style lang="scss" scoped>
  373. .con {
  374. // padding: 30rpx;
  375. .area {
  376. width: 100%;
  377. .view {
  378. display: flex;
  379. justify-content: center;
  380. align-items: center;
  381. .area-con {
  382. position: relative;
  383. .pre-image {
  384. width: 100%;
  385. height: 100%;
  386. }
  387. .del-con {
  388. position: absolute;
  389. top: 0rpx;
  390. right: 0rpx;
  391. padding: 0 0 20rpx 20rpx;
  392. .del-wrap {
  393. width: 36rpx;
  394. height: 36rpx;
  395. background-color: rgba(0, 0, 0, 0.4);
  396. border-radius: 0 0 0 10rpx;
  397. display: flex;
  398. justify-content: center;
  399. align-items: center;
  400. .del-image {
  401. width: 20rpx;
  402. height: 20rpx;
  403. }
  404. }
  405. }
  406. }
  407. }
  408. .add {
  409. position: absolute;
  410. display: flex;
  411. justify-content: center;
  412. align-items: center;
  413. .add-wrap{
  414. display: flex;
  415. justify-content: center;
  416. align-items: center;
  417. background-color: #eeeeee;
  418. }
  419. }
  420. }
  421. }
  422. </style>