农燊高科官方网站
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.
 
 
 

640 rindas
26 KiB

  1. var deleteNum = 0;
  2. (function(window, document) {
  3. //定义上传类
  4. var Cupload = function(options) {
  5. //初始化 new对象
  6. if (!(this instanceof Cupload)) {
  7. return new Cupload(options)
  8. }
  9. //设置默认参数
  10. this.localValue = {
  11. ele: '#cupload',
  12. name: 'image',
  13. num: 1,
  14. width: 148,
  15. height: 148,
  16. }
  17. //参数覆盖
  18. this.opt = this.extend(this.localValue, options, true)
  19. //所需变量
  20. this.i = 0;
  21. this.imageArr = new Array();//图片
  22. this.widthArr = new Array();//图片宽度
  23. this.heightArr = new Array();//图片高度
  24. this.imageBox = new Array();//图片盒子
  25. this.imagePreview = new Array();//图片预览
  26. this.imageInput = new Array();//图片input
  27. this.imageDelete = new Array();//图片删除遮罩
  28. this.deleteBtn = new Array();//图片删除按钮
  29. this.sortLeftBtn = new Array();//图片左排序按钮
  30. this.sortRightBtn = new Array();//图片右排序按钮
  31. if ((typeof options.ele) === "string") {
  32. this.opt.ele = document.querySelector(options.ele)
  33. } else {
  34. this.opt.ele = options.ele
  35. }
  36. this.initDom();
  37. }
  38. Cupload.prototype = {
  39. constructor: this,
  40. //初始化
  41. initDom: function() {
  42. this.cteateImageList()
  43. this.createUploadBox()
  44. this.createOverlay()
  45. if (this.opt.data) {
  46. this.showImagePreview()
  47. }
  48. },
  49. //参数覆盖
  50. extend: function(o, n, override) {
  51. for (var key in n) {
  52. if (n.hasOwnProperty(key) && (!o.hasOwnProperty(key) || override)) {
  53. o[key] = n[key]
  54. }
  55. }
  56. return o
  57. },
  58. //创建图片列表
  59. cteateImageList: function() {
  60. this.imageList = document.createElement('ul')
  61. this.imageList.className = 'cupload-image-list'
  62. this.imageList.style.margin = 0
  63. this.imageList.style.padding = 0
  64. this.imageList.style.display = 'inline-block'
  65. this.imageList.style.minHeight = this.opt.height
  66. this.opt.ele.appendChild(this.imageList)
  67. this.imageList.ondragstart = function(event) {
  68. console.log('start')
  69. }
  70. },
  71. //创建上传框
  72. createUploadBox: function() {
  73. this.uploadBox = document.createElement('div')
  74. this.uploadBox.className = 'cupload-upload-box'
  75. this.uploadBox.style.position = 'relative'
  76. this.uploadBox.style.display = 'inline-block'
  77. this.uploadBox.style.textAlign = 'center'
  78. this.uploadBox.style.backgroundColor = '#fbfdff'
  79. this.uploadBox.style.border = '1px dashed #c0ccda'
  80. this.uploadBox.style.borderRadius = '6px'
  81. this.uploadBox.style.WebkitBoxSizing = 'border-box'
  82. this.uploadBox.style.boxSizing = 'border-box'
  83. this.uploadBox.style.width = this.opt.width + 'px'
  84. this.uploadBox.style.height = this.opt.height + 'px'
  85. this.uploadBox.style.lineHeight = this.opt.height + 'px'
  86. this.opt.ele.appendChild(this.uploadBox)
  87. this.createUploadBtn()
  88. this.createUploadInput()
  89. var _this = this
  90. this.uploadBox.onmouseover = function() {
  91. _this.uploadBox.style.borderColor = '#007b76'
  92. }
  93. this.uploadBox.onmouseout = function() {
  94. _this.uploadBox.style.borderColor = '#c0ccda'
  95. }
  96. },
  97. //创建遮罩
  98. createOverlay: function() {
  99. this.overlay = document.createElement('div')
  100. this.overlay.className = 'cupload-overlay'
  101. this.overlay.style.display = "none"
  102. this.overlay.style.position = "fixed"
  103. this.overlay.style.textAlign = "center"
  104. this.overlay.style.top = 0
  105. this.overlay.style.right = 0
  106. this.overlay.style.bottom = 0
  107. this.overlay.style.left = 0
  108. this.overlay.style.zIndex = 9115
  109. this.overlay.style.backgroundColor = "rgba(0,0,0,.3)"
  110. this.opt.ele.appendChild(this.overlay)
  111. var _this = this
  112. this.overlay.onclick = function() {
  113. _this.zoomOutImage()
  114. }
  115. },
  116. //创建上传按钮
  117. createUploadBtn: function() {
  118. this.uploadBtn = document.createElement('span')
  119. this.uploadBtn.className = 'cupload-upload-btn'
  120. this.uploadBtn.style.position = 'absolute'
  121. this.uploadBtn.style.left = this.opt.width/2 - 14 + 'px'
  122. this.uploadBtn.style.fontSize = '28px'
  123. this.uploadBtn.style.color = '#8c939d'
  124. this.uploadBtn.innerHTML = '+'
  125. this.uploadBox.appendChild(this.uploadBtn)
  126. },
  127. //创建上传input
  128. createUploadInput: function() {
  129. this.uploadInput = document.createElement('input')
  130. this.uploadInput.className = 'cupload-upload-input'
  131. this.uploadInput.style.position = 'absolute'
  132. this.uploadInput.style.top = 0
  133. this.uploadInput.style.right = 0
  134. this.uploadInput.style.width = '100%'
  135. this.uploadInput.style.height = '100%'
  136. this.uploadInput.style.opacity = 0
  137. this.uploadInput.style.cursor = 'pointer'
  138. this.uploadInput.type = 'file'
  139. this.uploadInput.multiple = 'multiple'
  140. this.uploadInput.accept = 'image/*'
  141. this.uploadInput.title = ''
  142. this.uploadBox.appendChild(this.uploadInput)
  143. var _this = this
  144. this.uploadInput.onchange = function() {
  145. _this.removeUploadBox()
  146. _this.uploadImage()
  147. }
  148. },
  149. //上传图片
  150. uploadImage: function() {
  151. if(this.uploadInput.files.length + this.imageList.children.length > this.opt.num) {
  152. this.createUploadBox()
  153. alert('图片数量超出限制,请重新选择')
  154. return
  155. }
  156. for(j = 0; j < this.uploadInput.files.length; j++){
  157. var file = this.uploadInput.files[j]
  158. if (!file || this.limitedSize(file)) {
  159. this.createUploadBox()
  160. return false
  161. }
  162. var reader = new FileReader()
  163. var _this = this
  164. reader.filename = file.name;
  165. reader.onload = function(e) {
  166. _this.limitedWidthAndHeight(e.target.result, e.target.filename)
  167. }
  168. reader.readAsDataURL(file);
  169. }
  170. if (this.uploadInput.files.length + this.imageList.children.length < this.opt.num) {
  171. this.createUploadBox()
  172. }
  173. },
  174. //检测图片大小限制
  175. limitedSize: function(file) {
  176. if (this.opt.minSize && file.size < this.opt.minSize * 1024) {
  177. alert('图片' + file.name + '大小未到最小限制,请重新选择')
  178. return true
  179. }
  180. if (this.opt.maxSize && file.size > this.opt.maxSize * 1024) {
  181. alert('图片' + file.name + '大小超出最大限制,请重新选择')
  182. return true
  183. }
  184. if (this.opt.limitedSize && file.size > this.opt.limitedSize * 1024) {
  185. alert('图片' + file.name + '大小不符合要求,请重新选择')
  186. return true
  187. }
  188. return false
  189. },
  190. //检测图片像素限制
  191. limitedWidthAndHeight: function(src, name) {
  192. var tempImage = new Image()
  193. tempImage.src = src
  194. var _this = this
  195. tempImage.onload = function() {
  196. if (_this.opt.minWidth && this.width < _this.opt.minWidth) {
  197. alert('图片' + name + '宽度未到最小限制,请重新选择')
  198. _this.isCreateUploadBox()
  199. return false
  200. }
  201. if (_this.opt.minHeight && this.height < _this.opt.minHeight) {
  202. alert('图片' + name + '高度未到最小限制,请重新选择')
  203. _this.isCreateUploadBox()
  204. return false
  205. }
  206. if (_this.opt.maxWidth && this.width > _this.opt.maxWidth) {
  207. alert('图片' + name + '宽度超出最大限制,请重新选择')
  208. _this.isCreateUploadBox()
  209. return false
  210. }
  211. if (_this.opt.maxHeight && this.height > _this.opt.maxHeight) {
  212. alert('图片' + name + '高度超出最大限制,请重新选择')
  213. _this.isCreateUploadBox()
  214. return false
  215. }
  216. if (_this.opt.limitedWidth && this.width != _this.opt.limitedWidth) {
  217. alert('图片' + name + '宽度不符合要求,请重新选择')
  218. _this.isCreateUploadBox()
  219. return false
  220. }
  221. if (_this.opt.limitedHeight && this.height != _this.opt.limitedHeight) {
  222. alert('图片' + name + '高度不符合要求,请重新选择')
  223. _this.isCreateUploadBox()
  224. return false
  225. }
  226. _this.foreachNum(src, name, this.width, this.height)
  227. }
  228. },
  229. //检测图片数量
  230. foreachNum: function(src, name, width, height) {
  231. if(this.opt.url) {
  232. var key = this.opt.name
  233. var data = {}
  234. data[key] = src
  235. var _this = this
  236. this.ajaxUploadImage(data, function(res) {
  237. _this.createImageBox(res.responseText, res.responseText, width, height)
  238. })
  239. } else {
  240. this.createImageBox(src, name, width, height)
  241. }
  242. },
  243. //图片异步上传
  244. ajaxUploadImage: function(data,success) {
  245. var xhr = null;
  246. if(window.XMLHttpRequest){
  247. xhr = new XMLHttpRequest()
  248. } else {
  249. xhr = new ActiveXObject('Microsoft.XMLHTTP')
  250. }
  251. if(typeof data == 'object'){
  252. var str = '';
  253. for(var key in data){
  254. str += key+'='+data[key]+'&';
  255. }
  256. data = str.replace(/&$/, '');
  257. }
  258. xhr.open('POST', this.opt.url, true);
  259. xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  260. xhr.send(data);
  261. xhr.onreadystatechange = function(){
  262. if(xhr.readyState == 4){
  263. if(xhr.status == 200){
  264. success(xhr)
  265. } else {
  266. alert(((xhr.responseText).split("</p>")[0]).split("<p>")[1])
  267. return false
  268. }
  269. }
  270. }
  271. },
  272. //创建图片框
  273. createImageBox: function(src, name, width, height, state = true) {
  274. this.imageArr[this.i] = src
  275. this.widthArr[this.i] = width
  276. this.heightArr[this.i] = height
  277. this.imageBox[this.i] = document.createElement('li')
  278. this.imageBox[this.i].className = 'cupload-image-box'
  279. this.imageBox[this.i].style.position = 'relative'
  280. this.imageBox[this.i].style.display = 'inline-block'
  281. this.imageBox[this.i].style.marginRight = 8 + 'px'
  282. this.imageBox[this.i].style.backgroundColor = '#fbfdff'
  283. this.imageBox[this.i].style.border = '1px solid #007b76'
  284. this.imageBox[this.i].style.borderRadius = '6px'
  285. this.imageBox[this.i].style.WebkitBoxSizing = 'border-box'
  286. this.imageBox[this.i].style.boxSizing = 'border-box'
  287. this.imageBox[this.i].style.width = this.opt.width + 'px'
  288. this.imageBox[this.i].style.height = this.opt.height + 'px'
  289. this.imageList.appendChild(this.imageBox[this.i])
  290. this.createImagePreview(src, width, height)
  291. this.createImageInput(src)
  292. this.createImageDelete(name)
  293. if (!state) {
  294. this.setDefaultImage()
  295. }
  296. var _this = this
  297. for (var m = 0; m <= this.i; m++) {
  298. this.imageBox[m].index = m
  299. this.imageBox[m].onmouseover = function(n) {
  300. return function() {
  301. _this.showImageDelete(n)
  302. }
  303. }(m)
  304. this.imageBox[m].onmouseout = function(n) {
  305. return function() {
  306. _this.hideImageDelete(n)
  307. }
  308. }(m)
  309. }
  310. this.i++
  311. },
  312. //创建图片预览框
  313. createImagePreview: function(src, width, height) {
  314. this.imagePreview[this.i] = document.createElement('img')
  315. this.imagePreview[this.i].className = 'cupload-image-preview'
  316. this.imagePreview[this.i].style.position = 'absolute'
  317. this.imagePreview[this.i].style.top = 0
  318. this.imagePreview[this.i].style.left = 0
  319. this.imagePreview[this.i].style.right = 0
  320. this.imagePreview[this.i].style.bottom = 0
  321. this.imagePreview[this.i].style.margin = 'auto'
  322. this.imagePreview[this.i].src = src
  323. this.setImageAttribute(width, height)
  324. this.imageBox[this.i].appendChild(this.imagePreview[this.i])
  325. },
  326. //创建图片input
  327. createImageInput: function(src) {
  328. this.imageInput[this.i] = document.createElement('input')
  329. this.imageInput[this.i].type = 'hidden'
  330. this.imageInput[this.i].name = this.opt.name + '[]'
  331. this.imageInput[this.i].value = src
  332. this.imageBox[this.i].appendChild(this.imageInput[this.i])
  333. },
  334. //创建删除
  335. createImageDelete: function(name) {
  336. this.imageDelete[this.i] = document.createElement('div')
  337. this.imageDelete[this.i].className = 'cupload-image-delete'
  338. this.imageDelete[this.i].style.position = 'absolute'
  339. this.imageDelete[this.i].style.width = '100%'
  340. this.imageDelete[this.i].style.height = '100%'
  341. this.imageDelete[this.i].style.left = 0
  342. this.imageDelete[this.i].style.top = 0
  343. this.imageDelete[this.i].style.textAlign = 'center'
  344. this.imageDelete[this.i].style.color = '#fff'
  345. this.imageDelete[this.i].style.opacity = 0
  346. this.imageDelete[this.i].style.cursor = 'zoom-in'
  347. this.imageDelete[this.i].style.backgroundColor = 'rgba(0,0,0,.5)'
  348. this.imageDelete[this.i].style.WebkitTransition = '.3s'
  349. this.imageDelete[this.i].style.transition = '.3s'
  350. this.imageDelete[this.i].title = name
  351. this.imageBox[this.i].appendChild(this.imageDelete[this.i])
  352. this.createDeleteBtn()
  353. this.createSortBtn()
  354. var _this = this
  355. for (var m = 0; m <= this.i; m++) {
  356. this.imageDelete[m].onclick = function(n) {
  357. return function() {
  358. _this.zoomInImage(n)
  359. }
  360. }(m)
  361. }
  362. },
  363. //创建删除按钮
  364. createDeleteBtn: function() {
  365. this.deleteBtn[this.i] = document.createElement('span')
  366. this.deleteBtn[this.i].className = 'cupload-delete-btn'
  367. this.deleteBtn[this.i].style.position = 'absolute'
  368. this.deleteBtn[this.i].style.top = 0
  369. this.deleteBtn[this.i].style.right = 0
  370. this.deleteBtn[this.i].style.margin = 0
  371. this.deleteBtn[this.i].style.padding = 0
  372. this.deleteBtn[this.i].style.fontSize = '18px'
  373. this.deleteBtn[this.i].style.width = '24px'
  374. this.deleteBtn[this.i].style.height = '24px'
  375. this.deleteBtn[this.i].style.cursor = 'pointer'
  376. this.deleteBtn[this.i].style.backgroundImage = "url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAAABmJLR0QA/wD/AP+gvaeTAAAAwUlEQVRYhe2WwQ3CMAxFPxXqOjAAqGwHo3QNCIxSxuBzcaRStcRxcin4XSJHjv2aNkoBRwHJhmSgnhvJpqbAPqN5ZKeprbU8ydhvEgDoJ2uqCHQyXhW5Maf7mrUEyYdhu7WEab+5HXiZzJXPp88Uijsm6tQ7KkZcYF0CckSDNi5i7uudzqXipbkx63oFLuACPymwzcy/4/NKTcV2/Dp2AQBPACB5sBYneRzXyl18qfAXHDlbBFqRGAoaD1KjzRb4G96BvtfyCUSIygAAAABJRU5ErkJggg==')"
  377. this.deleteBtn[this.i].style.backgroundSize = '18px 18px'
  378. this.deleteBtn[this.i].style.backgroundRepeat = 'no-repeat'
  379. this.deleteBtn[this.i].style.backgroundPosition = 'right top'
  380. this.deleteBtn[this.i].innerHTML = ''
  381. this.deleteBtn[this.i].title = '删除'
  382. this.imageDelete[this.i].appendChild(this.deleteBtn[this.i])
  383. var _this = this
  384. for (var m = 0; m <= this.i; m++) {
  385. this.deleteBtn[m].onclick = function(n) {
  386. return function() {
  387. _this.deleteImage(n)
  388. }
  389. }(m)
  390. }
  391. },
  392. createSortBtn: function() {
  393. this.sortLeftBtn[this.i] = document.createElement('span')
  394. this.sortLeftBtn[this.i].className = 'cupload-sort-left'
  395. this.sortLeftBtn[this.i].style.position = 'absolute'
  396. this.sortLeftBtn[this.i].style.bottom = 0
  397. this.sortLeftBtn[this.i].style.left = 0
  398. this.sortLeftBtn[this.i].style.margin = 0
  399. this.sortLeftBtn[this.i].style.padding = 0
  400. this.sortLeftBtn[this.i].style.fontSize = '18px'
  401. this.sortLeftBtn[this.i].style.width = '24px'
  402. this.sortLeftBtn[this.i].style.height = '24px'
  403. this.sortLeftBtn[this.i].style.cursor = 'pointer'
  404. this.sortLeftBtn[this.i].style.backgroundImage = "url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAAABmJLR0QA/wD/AP+gvaeTAAABP0lEQVRYhe2UIUsEURRGv7suGza4sCBoENYgGMwWs/4A/4AWwWRyMfgHRCyKGhWsYtegRYOCCGoUk1kwKLiws8dywRFGcXZ23yDMiW8e75z3YK5UUPBfAQbzlK8BEbCa9axySrFJ2pC0klWcGsCALb7Y8aAg8gHgICZfDyJ2eQU4dnEHCPf8QBU4dXkELIaU14BLl7eB+ZDyOnDt8g9gLqR8BHhw+Rsw00/ft98IaEg6lzTmS2eSbnroiyTtm9lT4ldgm/6z+9sLTPoLDPnSraTk2u5oSdo0s7sfdwATwLPXvgBTPQz4G0ADePSIV2A6j4hh4N4j3oHZPCLqwJVHtAg5D2IRNeDCI8JOxFhEFTjxiA6wnEdEBTiKRTTziCgDh7GhspT1zFKazWbWlrQgac+XRrMGdA0wDqS6QEFBEp/yS6NBq8E1tgAAAABJRU5ErkJggg==')"
  405. this.sortLeftBtn[this.i].style.backgroundSize = '18px 18px'
  406. this.sortLeftBtn[this.i].style.backgroundRepeat = 'no-repeat'
  407. this.sortLeftBtn[this.i].style.backgroundPosition = 'left bottom'
  408. this.sortLeftBtn[this.i].innerHTML = ''
  409. this.sortLeftBtn[this.i].title = '左移'
  410. this.imageDelete[this.i].appendChild(this.sortLeftBtn[this.i])
  411. this.sortRightBtn[this.i] = document.createElement('span')
  412. this.sortRightBtn[this.i].className = 'cupload-sort-right'
  413. this.sortRightBtn[this.i].style.position = 'absolute'
  414. this.sortRightBtn[this.i].style.bottom = 0
  415. this.sortRightBtn[this.i].style.right = 0
  416. this.sortRightBtn[this.i].style.margin = 0
  417. this.sortRightBtn[this.i].style.padding = 0
  418. this.sortRightBtn[this.i].style.fontSize = '18px'
  419. this.sortRightBtn[this.i].style.width = '24px'
  420. this.sortRightBtn[this.i].style.height = '24px'
  421. this.sortRightBtn[this.i].style.cursor = 'pointer'
  422. this.sortRightBtn[this.i].style.backgroundImage = "url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAAABmJLR0QA/wD/AP+gvaeTAAAA4UlEQVRYhe3WMUpDQRCA4XkKWlhIqnQWwc4iKbyBTVohJ/AC2lh4gxxBA7lAqhwgvbWtpYWVnVgI4mfhghKeEPCFSeD93cIu8w+7OzMRLS3bDga4Qy9LYOqbZ5xkCBzjqUi84DRD4giPReIVZxkSXTwUiTcMMyQ6uC8S7xhlSBxgUSQ+cJEhsY95kfjEVYbEHmZ+GNftq2oOHkbETkMeuxExiYjzsh5XVXXz525cWz+Xv2MuZ6qhzFdms66gSVZ9hOsKnvcNZRYimaVYZjOS2Y5lDyQ2YCTr41bWUNrS8l++ABQQn/PCTE8cAAAAAElFTkSuQmCC')"
  423. this.sortRightBtn[this.i].style.backgroundSize = '18px 18px'
  424. this.sortRightBtn[this.i].style.backgroundRepeat = 'no-repeat'
  425. this.sortRightBtn[this.i].style.backgroundPosition = 'right bottom'
  426. this.sortRightBtn[this.i].innerHTML = ''
  427. this.sortRightBtn[this.i].title = '右移'
  428. this.imageDelete[this.i].appendChild(this.sortRightBtn[this.i])
  429. var _this = this
  430. for (var m = 0; m <= this.i; m++) {
  431. this.sortLeftBtn[m].onclick = function(n) {
  432. return function() {
  433. _this.sortLeft(event, n)
  434. }
  435. }(m)
  436. this.sortRightBtn[m].onclick = function(n) {
  437. return function() {
  438. _this.sortRight(event, n)
  439. }
  440. }(m)
  441. }
  442. },
  443. //设置默认图片
  444. setDefaultImage: function() {
  445. this.imageBox[this.i].style.backgroundColor = "#B2B2B2"
  446. this.imageDelete[this.i].title = '图片不存在'
  447. this.imagePreview[this.i].src = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAKAAAAB4CAMAAABCfAldAAAAllBMVEUAAAB/f39XV1fKysrr6+vo6Oj+/v5DQ0OZmZl5eXni4uLGxsbe3t7b29u2trajo6Nubm5mZmbOzs6wsLCGhob8/Pzl5eXU1NS9vb2rq6tSUlJLS0vY2NiRkZFeXl7y8vLw8PBzc3P6+vr39/fBwcH09PTe3t7w8PDv7+/b29vt7e3j4+Pa2trq6urg4ODZ2dnm5ub4+PhWkjdOAAAAJnRSTlMAWCWy29fzDHdP0a7MyJuEQje4k2Dw1b+ijR8XxG0v5OFJ7uun5sxAWrkAAAUySURBVHja7ZsNV9owFIY7TUBBFBD5EEHcNM1N0hb+/59bmsQltSutbUdzdvqefbid5OXxvtzmamvQq1evXr169er1X+qqNQ0r+n0L7+YORy3poF5593EsWYfvbqrzPRygJSV4EUhtR0JAmQ4PVfmelwJIKxJ8rRzXvNwQBH6uCHgbtQQI5G6oDJGo8tlEtxUBf7CkHcAE6Zd8IVBlNftRGVC0EzB+1G9pLCotvzQgkBd9hcFAvARM0EzZjfk/BIzrC5C+bCwOcH5hE8CI1hYLX7QbChk9q6g2IPCQ0rCeKPulD6/HJ3bWQ74Ch/qAjNE6Cmk00CfX/qfkO1tpJgEvXUFK2WmuvVYy4JIKXh6QSqjXrbLaDSLp4BmgjJQNFspp+EplAX0DDEN2etdO6xOjoW8VTK8wSE+p04Gsn28Rq4DNWDIKZQF9q6As4NO9mXsHks+3iGUHhyMz9/6SfL5VMA34aaZtrk9pB3sGmB4hD8ZFXQI9A0w7eBI4Q4J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ewzgHaAO2I6AvgG6AV9zIN4BCjQNjB5xQnwDdDt4JxvEP0Ab8BQJ4h9ggqbOCOgfoMCbwGjFgVwCMIEaP9gidb9s8tVMZcApSmp0sBkB6wrsG6VcEyKgsuwDKbcogfoSchiqqik+8qqK+WfAexTHvL6OsoCV9bwZX1fUeDX7HLHmclNtjTdXQa9evXr16tWr17f1G41D8rN+B+2KAAAAAElFTkSuQmCC'
  448. if (130 / this.opt.width > 105 / this.opt.height) {
  449. this.imagePreview[this.i].style.width = this.opt.width - 2 + 'px'
  450. this.imagePreview[this.i].style.height = 105 / (130 / this.opt.width) - 2 + 'px'
  451. } else {
  452. this.imagePreview[this.i].style.width = 130 / (105 / this.opt.height) - 2 + 'px'
  453. this.imagePreview[this.i].style.height = this.opt.height - 2 + 'px'
  454. }
  455. },
  456. //设置图片宽高
  457. setImageAttribute: function(width, height) {
  458. if (width / this.opt.width > height / this.opt.height) {
  459. this.imagePreview[this.i].style.width = this.opt.width - 2 + 'px'
  460. this.imagePreview[this.i].style.height = height / (width / this.opt.width) - 2 + 'px'
  461. } else {
  462. this.imagePreview[this.i].style.width = width / (height / this.opt.height) - 2 + 'px'
  463. this.imagePreview[this.i].style.height = this.opt.height - 2 + 'px'
  464. }
  465. },
  466. //data图片预览
  467. showImagePreview: function() {
  468. var obj = this.opt.data
  469. if (obj.length >= this.opt.num) {
  470. this.removeUploadBox()
  471. }
  472. var _this = this
  473. var tempImage = new Image()
  474. tempImage.src = obj[this.i]
  475. tempImage.onload = function() {
  476. _this.createImageBox(obj[_this.i], obj[_this.i], this.width, this.height)
  477. setTimeout(function() {
  478. if (obj[_this.i]) {
  479. _this.showImagePreview()
  480. }
  481. }, 0);
  482. }
  483. tempImage.onerror = function() {
  484. _this.createImageBox(obj[_this.i], obj[_this.i], 0, 0, false)
  485. setTimeout(function() {
  486. if (obj[_this.i]) {
  487. _this.showImagePreview()
  488. }
  489. }, 0);
  490. }
  491. },
  492. //图片放大预览
  493. zoomInImage: function(n) {
  494. if(event.target.classList[0] === 'cupload-delete-btn' || event.target.classList[0] === 'cupload-sort-right' || event.target.classList[0] === 'cupload-sort-left') {
  495. return;
  496. }
  497. if(this.imagePreview[n].src == 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAKAAAAB4CAMAAABCfAldAAAAllBMVEUAAAB/f39XV1fKysrr6+vo6Oj+/v5DQ0OZmZl5eXni4uLGxsbe3t7b29u2trajo6Nubm5mZmbOzs6wsLCGhob8/Pzl5eXU1NS9vb2rq6tSUlJLS0vY2NiRkZFeXl7y8vLw8PBzc3P6+vr39/fBwcH09PTe3t7w8PDv7+/b29vt7e3j4+Pa2trq6urg4ODZ2dnm5ub4+PhWkjdOAAAAJnRSTlMAWCWy29fzDHdP0a7MyJuEQje4k2Dw1b+ijR8XxG0v5OFJ7uun5sxAWrkAAAUySURBVHja7ZsNV9owFIY7TUBBFBD5EEHcNM1N0hb+/59bmsQltSutbUdzdvqefbid5OXxvtzmamvQq1evXr169er1X+qqNQ0r+n0L7+YORy3poF5593EsWYfvbqrzPRygJSV4EUhtR0JAmQ4PVfmelwJIKxJ8rRzXvNwQBH6uCHgbtQQI5G6oDJGo8tlEtxUBf7CkHcAE6Zd8IVBlNftRGVC0EzB+1G9pLCotvzQgkBd9hcFAvARM0EzZjfk/BIzrC5C+bCwOcH5hE8CI1hYLX7QbChk9q6g2IPCQ0rCeKPulD6/HJ3bWQ74Ch/qAjNE6Cmk00CfX/qfkO1tpJgEvXUFK2WmuvVYy4JIKXh6QSqjXrbLaDSLp4BmgjJQNFspp+EplAX0DDEN2etdO6xOjoW8VTK8wSE+p04Gsn28Rq4DNWDIKZQF9q6As4NO9mXsHks+3iGUHhyMz9/6SfL5VMA34aaZtrk9pB3sGmB4hD8ZFXQI9A0w7eBI4Q4JngFTPCHZI8KyCcrGeEaRm6ZDgW8Q0NDOC1IsM2LcKUsroxzYwQ0IasGeAaQcvgj9DQki7BzSjqPzLDIFj4/B+YrTzChq2zw/SI+Qw1AZvKuCOA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VcEyKgsuwDKbcogfoSchiqqik+8qqK+WfAexTHvL6OsoCV9bwZX1fUeDX7HLHmclNtjTdXQa9evXr16tWr17f1G41D8rN+B+2KAAAAAElFTkSuQmCC') {
  498. alert('图片不存在')
  499. return;
  500. }
  501. this.zommImage = document.createElement('img')
  502. this.zommImage.style.display = "inline-block"
  503. this.zommImage.style.verticalAlign = "middle"
  504. this.zommImage.style.position= "absolute";
  505. this.zommImage.style.top="50%";
  506. this.zommImage.style.transform="translate(-50%,-50%)";
  507. this.zommImage.src = this.imageArr[n]
  508. if (this.widthArr[n] / window.innerWidth > this.heightArr[n] / window.innerHeight) {
  509. this.zommImage.style.width = 0.8 * window.innerWidth + 'px'
  510. this.zommImage.style.height = 0.8 * this.heightArr[n] / (this.widthArr[n] / window.innerWidth) + 'px'
  511. } else {
  512. this.zommImage.style.width = 0.8 * this.widthArr[n] / (this.heightArr[n] / window.innerHeight) + 'px'
  513. this.zommImage.style.height = 0.8 * window.innerHeight + 'px'
  514. }
  515. this.overlay.appendChild(this.zommImage)
  516. this.overlay.style.lineHeight = window.innerHeight + 'px'
  517. this.overlay.style.cursor = "zoom-out"
  518. this.overlay.style.display = "block"
  519. },
  520. //关闭图片放大预览
  521. zoomOutImage: function() {
  522. this.overlay.style.display = "none"
  523. this.zommImage.remove()
  524. },
  525. //检测当前图片数量,判断是否创建上传框
  526. isCreateUploadBox: function() {
  527. this.removeUploadBox()
  528. if (this.imageList.children.length < this.opt.num) {
  529. this.createUploadBox()
  530. }
  531. },
  532. //删除图片
  533. deleteImage: function(n) {
  534. this.imageBox[n].remove()
  535. deleteNum = n ;
  536. this.removeUploadBox()
  537. if (this.imageList.children.length < this.opt.num) {
  538. this.createUploadBox()
  539. }
  540. deleteList()
  541. // if(this.opt.deleteUrl) {
  542. // var xhr = null;
  543. // var key = this.opt.name
  544. // var data = {}
  545. // data[key] = this.imageArr[n]
  546. // if(window.XMLHttpRequest){
  547. // xhr = new XMLHttpRequest()
  548. // } else {
  549. // xhr = new ActiveXObject('Microsoft.XMLHTTP')
  550. // }
  551. // if(typeof data == 'object'){
  552. // var str = '';
  553. // for(var key in data){
  554. // str += key+'='+data[key]+'&';
  555. // }
  556. // data = str.replace(/&$/, '');
  557. // }
  558. // xhr.open('POST', this.opt.deleteUrl, true);
  559. // xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  560. // xhr.send(data);
  561. // xhr.onreadystatechange = function(){
  562. // if(xhr.readyState == 4){
  563. // if(xhr.status == 200){
  564. // console.log(xhr.response)
  565. // } else {
  566. // alert(((xhr.responseText).split("</p>")[0]).split("<p>")[1])
  567. // return false
  568. // }
  569. // }
  570. // }
  571. // }
  572. },
  573. sortLeft: function(event, n) {
  574. if(this.imageBox[n].previousSibling) {
  575. this.imageList.insertBefore(this.imageBox[n], this.imageBox[n].previousSibling)
  576. }
  577. },
  578. sortRight: function(event, n) {
  579. if(this.imageBox[n].nextSibling) {
  580. this.imageList.insertBefore(this.imageBox[n].nextSibling, this.imageBox[n])
  581. }
  582. },
  583. //移除上传框
  584. removeUploadBox: function() {
  585. this.uploadBox.remove()
  586. },
  587. //显示图片删除
  588. showImageDelete: function(m) {
  589. this.imageDelete[m].style.opacity = 1
  590. },
  591. //隐藏图片删除
  592. hideImageDelete: function(m) {
  593. this.imageDelete[m].style.opacity = 0
  594. },
  595. }
  596. window.Cupload = Cupload;
  597. })(window, document)