DragImage.vue 13 KB

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