Picture.js 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530
  1. Namespace.register("U.MD.UI.picture");
  2. /**
  3. * 图片控件调用接口函数
  4. * @arr {Array || Object} 图片信息数组
  5. * 形如:
  6. * ['//img/1.jpg',
  7. * '//img/2.jpg',
  8. * '//img/3.jpg',
  9. * '//img/4.jpg',
  10. * '//img/5.jpeg',
  11. * '//img/6.jpg']
  12. */
  13. U.MD.UI.picture = function (arr, startPicNum) {
  14. if ($('#U_MD_UI_PictureForm')[0]) { /*判断窗体是否存在*/
  15. if ($('#U_MD_UI_PictureForm')[0].style.display === 'none') /*重新将窗体显示到页面中*/
  16. U.selectEl('#U_MD_UI_PictureForm')[0].style.display = 'block';
  17. U.MD.UI.picture.DIVBOX = {
  18. "USERPICS": arr,
  19. "PICAREA": U.selectEl('#U_MD_UI_Picture_picArea')[0], /*获取拖拽的元素*/
  20. "PICBOX": U.selectEl('#U_MD_UI_Picture_picBox')[0], /*获取控件最外层元素*/
  21. 'FA': U.selectEl('#U_MD_UI_Picture_fa')[0] /*获取功能栏目元素*/
  22. };
  23. U.MD.UI.picture.initloadPic(arr, startPicNum)
  24. return;
  25. }
  26. if (!arr.length)
  27. return
  28. U.MD.UI.picture.create(arr, startPicNum); /*创建控件的内容*/
  29. U.MD.UI.picture.DIVBOX.PICAREA.onmousedown = function (e) { U.MD.UI.picture.mDown(U.MD.UI.picture.DIVBOX.PICAREA, U.MD.UI.picture.DIVBOX.PICBOX, e) }
  30. U.MD.UI.picture.wheel(); /*定义滚轮事件*/
  31. }
  32. /*定义功能栏目的图片*/
  33. U.MD.UI.picture.FA = [
  34. { src: "/img/pictureView/add.png", id: "U_MD_UI_Picture_add" }, //放大按钮图片素材
  35. {src: "/img/pictureView/narrow.png", id: "U_MD_UI_Picture_narrow" }, //缩小按钮图片素材
  36. {src: "/img/pictureView/prev.png", id: "U_MD_UI_Picture_prev" }, //上一张按钮图片素材
  37. {src: "/img/pictureView/next.png", id: "U_MD_UI_Picture_next" }, //下一张按钮图片素材
  38. {src: "/img/pictureView/turnLeft.png", id: "U_MD_UI_Picture_turnLeft" }, //左旋转按钮图片素材
  39. {src: "/img/pictureView/turnRight.png", id: "U_MD_UI_Picture_turnRight"} //右旋转按钮图片素材
  40. ];
  41. U.MD.UI.picture.picnumber = 0; /*定义图片数*/
  42. U.MD.UI.picture.rotatenum = 0; /*定义旋转数*/
  43. U.MD.UI.picture.originalSize = [] /*定义原始图片尺寸大小的数组*/
  44. U.MD.UI.picture.pics = []; /*定义存放图片的数组*/
  45. U.MD.UI.picture.scalingvalue = 50; /*定义缩放大小单位:px*/
  46. U.MD.UI.picture.focuspicsrc = ''; /*当前图片的src*/
  47. U.MD.UI.picture.focuspicsize = ''; /*当前图片的文件大小*/
  48. /**
  49. * 图片控件创建函数
  50. * @arr {Array || Object} 图片信息数组
  51. */
  52. U.MD.UI.picture.create = function (arr, startPicNum) {
  53. var _c = $$('div', { style: { height: "100%", "overflow": "hidden" }, id: "U_MD_UI_Picture_bossBox" }); /*创建控件最外层元素*/
  54. var _box = $$('div', { id: "U_MD_UI_Picture_picBox", className: 'U_MD_UI_Picture_picBox' }, _c);
  55. $$('div', { id: 'U_MD_UI_Picture_picArea', className: 'U_MD_UI_Picture_picArea' }, _box); /*创建控件第二层元素,也是拖拽时所要触发的元素*/
  56. var _functionalarea = $$('div', { style: { height: "60px", width: "100%", position: "absolute", bottom: "0", "background-color": "#ffffff", "text-align": "center" }, id: "U_MD_UI_Picture_fa" }, _c); /*创建控件的功能栏目*/
  57. new U.UF.UI.form(
  58. "<span id='U_MD_UI_Picture_imgInfo'></span> - <span id='U_MD_UI_Picture_r'>100%</span>",
  59. _c,
  60. { style: { width: document.documentElement.clientWidth / 1.5 + 'px', height: document.documentElement.clientHeight / 1.5 + 'px', position: "absolute" }, id: "U_MD_UI_PictureForm" },
  61. { isdrag: true, isstretching: true, isenlarge: true, isclose: true, isnarrow: true },
  62. { style: { "padding": "10px", "background-color": "#22508c"} }
  63. ); /*调用窗体*/
  64. _box.style.height = _box.clientHeight - 60 + 'px'; /*让可拖拽区域的宽-60px*/
  65. // 全局属性
  66. U.MD.UI.picture.DIVBOX = {
  67. "USERPICS": arr,
  68. "PICAREA": U.selectEl('#U_MD_UI_Picture_picArea')[0], /*获取拖拽的元素*/
  69. "PICBOX": U.selectEl('#U_MD_UI_Picture_picBox')[0], /*获取控件最外层元素*/
  70. 'FA': U.selectEl('#U_MD_UI_Picture_fa')[0] /*获取功能栏目元素*/
  71. };
  72. /*创建功能栏目子节点*/
  73. var _oDiv = $$('div', { style: { display: "inline-block", "line-height": "40px", "margin-top": "10px"} }, U.MD.UI.picture.DIVBOX.FA);
  74. /*循环功能栏目里面的图片*/
  75. for (var j = 0; j < U.MD.UI.picture.FA.length; j++) {
  76. /*创建span*/
  77. var _span = $$('span', { style: { "background-image": "url(" + U.MD.UI.picture.FA[j].src + ")", width: "40px", height: "40px", display: "inline-block", margin: "0 15px" }, id: U.MD.UI.picture.FA[j].id }, _oDiv);
  78. }
  79. U.MD.UI.picture.initloadPic(null, startPicNum);
  80. /*给上放大的点击事件*/
  81. U.selectEl('#U_MD_UI_Picture_add')[0].onclick = U.UF.C.apply(this, function () {
  82. this.changeSize(U.MD.UI.picture.scalingvalue, U.MD.UI.picture.rotatenum)
  83. });
  84. /*给上缩小的点击事件*/
  85. U.selectEl('#U_MD_UI_Picture_narrow')[0].onclick = U.UF.C.apply(this, function () {
  86. this.changeSize(-U.MD.UI.picture.scalingvalue, U.MD.UI.picture.rotatenum)
  87. });
  88. /*给上上一张图片的点击事件*/
  89. U.selectEl('#U_MD_UI_Picture_prev')[0].onclick = U.UF.C.apply(this, function () {
  90. this.prevPic(0)
  91. });
  92. /*给上下一张图片的点击事件*/
  93. U.selectEl('#U_MD_UI_Picture_next')[0].onclick = U.UF.C.apply(this, function () {
  94. this.prevPic(1)
  95. });
  96. /*给上左旋转的点击事件*/
  97. U.selectEl('#U_MD_UI_Picture_turnLeft')[0].onclick = U.UF.C.apply(this, function () {
  98. this.turn(1)
  99. });
  100. /*给上有旋转的点击事件*/
  101. U.selectEl('#U_MD_UI_Picture_turnRight')[0].onclick = U.UF.C.apply(this, function () {
  102. this.turn(0)
  103. });
  104. _c.onresize = function () {
  105. _box.style.height = _c.clientHeight - 60 + 'px';
  106. };
  107. _box.onresize = function () {
  108. U.MD.UI.picture.checkHeight();
  109. }
  110. }
  111. U.MD.UI.picture.initloadPic = function (arr, startPicNum) {
  112. if (arr) {
  113. U.MD.UI.picture.DIVBOX.PICAREA.innerHTML = "";
  114. U.MD.UI.picture.DIVBOX.PICAREA.style = null;
  115. U.MD.UI.picture.picnumber = startPicNum || 0; /*定义图片数*/
  116. U.MD.UI.picture.rotatenum = 0; /*定义旋转数*/
  117. U.MD.UI.picture.DIVBOX.USERPICS = arr
  118. } else {
  119. U.MD.UI.picture.picnumber = startPicNum >= 0 ? startPicNum : 0;
  120. }
  121. //调用创建方法
  122. U.MD.UI.picture.createPic();
  123. /*获取所有的图片并赋值*/
  124. U.MD.UI.picture.pics = U.selectEl('#U_MD_UI_Picture_picArea > ul > li > img');
  125. U.MD.UI.picture.pics[U.MD.UI.picture.picnumber].onload = U.UF.C.apply(this, function () {
  126. /*获取当前图片的原始大小并赋值*/
  127. this.originalSize = this.getImgNaturalDimensions(U.MD.UI.picture.pics[U.MD.UI.picture.picnumber]);
  128. /*调整大小,位置*/
  129. this.checkHeight();
  130. });
  131. }
  132. U.MD.UI.picture.createPic = function () {
  133. /*创建ul*/
  134. var _ul = $$('ul', { id: "ul" }, U.MD.UI.picture.DIVBOX.PICAREA);
  135. /*循环数据源*/
  136. for (var i = 0; i < U.MD.UI.picture.DIVBOX.USERPICS.length; i++) {
  137. /*创建li*/
  138. var _li = $$('li', {}, _ul);
  139. if (typeof U.MD.UI.picture.DIVBOX.USERPICS[i] === 'object') {
  140. /*如果是第一张,初始化控件一般都是第一张显示*/
  141. if (i === U.MD.UI.picture.picnumber) {
  142. $$('img', { "onerror": U.MD.C.imgError, src: U.MD.UI.picture.DIVBOX.USERPICS[i].src }, _li);
  143. U.MD.UI.picture.focuspicsrc = U.MD.UI.picture.DIVBOX.USERPICS[i].src;
  144. if (U.MD.UI.picture.DIVBOX.USERPICS[i].size)
  145. U.MD.UI.picture.focuspicsize = U.MD.UI.picture.DIVBOX.USERPICS[i].size;
  146. } else {
  147. $$('img', { "onerror": U.MD.C.imgError, src: U.MD.UI.picture.DIVBOX.USERPICS[i].src, style: { display: "none"} }, _li);
  148. }
  149. } else if (typeof U.MD.UI.picture.DIVBOX.USERPICS[i] === 'string') {
  150. if (i === U.MD.UI.picture.picnumber) {
  151. /*如果是第一张,初始化控件一般都是第一张显示*/
  152. $$('img', { "onerror": U.MD.C.imgError, src: U.MD.UI.picture.DIVBOX.USERPICS[i] }, _li);
  153. U.MD.UI.picture.focuspicsrc = U.MD.UI.picture.DIVBOX.USERPICS[i];
  154. }
  155. else {
  156. $$('img', { "onerror": U.MD.C.imgError, src: U.MD.UI.picture.DIVBOX.USERPICS[i], style: { display: "none"} }, _li);
  157. }
  158. }
  159. }
  160. }
  161. /**
  162. * 旋转检测
  163. * @param num 为整数
  164. * 如果是0也就是false,调用右旋转。如果是1也就是true,调用左旋转。因为有万恶的ie8,所以要滤镜旋转 0-0°,1-90°,2-180°,3-270°,如果需要任意角度旋转去学习阵矩。
  165. *
  166. */
  167. U.MD.UI.picture.turn = function (num) {
  168. /*判断是左旋,还是右旋,为什么要这么打应该晓得吧*/
  169. if (num) {
  170. U.MD.UI.picture.rotatenum--;
  171. U.MD.UI.picture.rotatenum < 0 ? U.MD.UI.picture.rotatenum = 3 : U.MD.UI.picture.rotatenum;
  172. } else {
  173. U.MD.UI.picture.rotatenum++;
  174. U.MD.UI.picture.rotatenum > 3 ? U.MD.UI.picture.rotatenum = 0 : U.MD.UI.picture.rotatenum;
  175. }
  176. U.MD.UI.picture.rotating();
  177. }
  178. /**
  179. * 执行旋转
  180. */
  181. U.MD.UI.picture.rotating = function () {
  182. /*定义常规游览器旋转属性字符*/
  183. var val = "rotate(" + U.MD.UI.picture.rotatenum * 90 + "deg)";
  184. /*U.MD.UI.picture.rotatenum % 2 !== 0 判断是否选转90°或者270°*/
  185. if (U.MD.UI.picture.rotatenum % 2 !== 0) {
  186. /*赋值大小,-20px是为了留间隙*/
  187. U.MD.UI.picture.DIVBOX.PICAREA.style.width = U.MD.UI.picture.DIVBOX.PICBOX.clientHeight - 20 + 'px';
  188. /*开始旋转*/
  189. U.MD.UI.picture.compatibleRotate(U.MD.UI.picture.DIVBOX.PICAREA, val);
  190. /*调整位置*/
  191. U.MD.UI.picture.DIVBOX.PICAREA.style.top = (U.MD.UI.picture.DIVBOX.PICAREA.clientWidth / 2 - U.MD.UI.picture.DIVBOX.PICAREA.clientHeight / 2) + 10 + 'px';
  192. /*调整位置,以至于中心缩放*/
  193. U.MD.UI.picture.DIVBOX.PICAREA.style.left = U.MD.UI.picture.DIVBOX.PICBOX.clientWidth / 2 - U.MD.UI.picture.DIVBOX.PICAREA.clientWidth / 2 + 'px';
  194. /*检测是否是ie8 or ie7*/
  195. if ((navigator.appName == "Microsoft Internet Explorer" && navigator.appVersion.match(/8./i) == "8.") || (navigator.appName == "Microsoft Internet Explorer" && navigator.appVersion.match(/7./i) == "7.")) {
  196. U.MD.UI.picture.DIVBOX.PICAREA.style.top = "10px";
  197. U.MD.UI.picture.DIVBOX.PICAREA.style.left = U.MD.UI.picture.DIVBOX.PICBOX.clientWidth / 2 - U.MD.UI.picture.DIVBOX.PICAREA.clientHeight / 2 + 'px';
  198. }
  199. } else {
  200. /*清空拖拽元素的style*/
  201. U.MD.UI.picture.DIVBOX.PICAREA.setAttribute('style', '');
  202. /*开始旋转*/
  203. U.MD.UI.picture.compatibleRotate(U.MD.UI.picture.DIVBOX.PICAREA, val);
  204. /*改变长度*/
  205. U.MD.UI.picture.DIVBOX.PICAREA.style.width = U.MD.UI.picture.originalSize[0] > U.MD.UI.picture.DIVBOX.PICAREA.clientWidth + 150 ? '1290px' : U.MD.UI.picture.DIVBOX.PICAREA.clientWidth + 150;
  206. /*调整图片大小*/
  207. U.MD.UI.picture.checkHeight();
  208. }
  209. /*改变左上角的title的缩放比*/
  210. U.selectEl('#U_MD_UI_Picture_r')[0].innerText = Math.round((U.MD.UI.picture.DIVBOX.PICAREA.clientHeight / U.MD.UI.picture.originalSize[1]) * 100) + '%';
  211. }
  212. /**
  213. * 兼容旋转
  214. * @param el 旋转的元素
  215. * @param val 旋转的属性值
  216. */
  217. U.MD.UI.picture.compatibleRotate = function (el, val) {
  218. //定义属性
  219. el.style.webkitTransform = val;
  220. el.style.MozTransform = val;
  221. el.style.mturnsTransform = val;
  222. el.style.OTransform = val;
  223. el.style.transform = val;
  224. /*检测是否是ie8 or ie7*/
  225. if ((navigator.appName == "Microsoft Internet Explorer" && navigator.appVersion.match(/8./i) == "8.") || (navigator.appName == "Microsoft Internet Explorer" && navigator.appVersion.match(/7./i) == "7."))
  226. el.style.filter = "progid:DXImageTransform.Microsoft.BasicImage(rotation=" + Math.abs(U.MD.UI.picture.rotatenum) + ")";
  227. }
  228. /**
  229. * 图片切换
  230. * @param num 整数orTRUE,FALSE
  231. * true代表下一张,false代表上一张
  232. */
  233. U.MD.UI.picture.prevPic = function (num) {
  234. /*在切换图片先把正在显示的图片隐藏掉*/
  235. U.MD.UI.picture.pics[U.MD.UI.picture.picnumber].style.display = 'none';
  236. if (num) {
  237. /*图片位置数++*/
  238. U.MD.UI.picture.picnumber++;
  239. /*如果是最后一张,跳到第一张*/
  240. if (U.MD.UI.picture.picnumber > U.MD.UI.picture.pics.length - 1)
  241. U.MD.UI.picture.picnumber = 0;
  242. } else {
  243. /*图片位置数--*/
  244. U.MD.UI.picture.picnumber--;
  245. /*如果是第一张,跳到最后一张*/
  246. if (U.MD.UI.picture.picnumber < 0)
  247. U.MD.UI.picture.picnumber = U.MD.UI.picture.pics.length - 1;
  248. }
  249. if (U.MD.UI.picture.DIVBOX.USERPICS[U.MD.UI.picture.picnumber].size)
  250. U.MD.UI.picture.focuspicsize = U.MD.UI.picture.DIVBOX.USERPICS[U.MD.UI.picture.picnumber].size;
  251. if (U.MD.UI.picture.DIVBOX.USERPICS[U.MD.UI.picture.picnumber].src)
  252. U.MD.UI.picture.focuspicsrc = U.MD.UI.picture.DIVBOX.USERPICS[U.MD.UI.picture.picnumber].src;
  253. /*显示需要显示的图片*/
  254. U.MD.UI.picture.pics[U.MD.UI.picture.picnumber].style.display = 'block';
  255. /*获取该图片的初始大小*/
  256. U.MD.UI.picture.originalSize = U.MD.UI.picture.getImgNaturalDimensions(U.MD.UI.picture.pics[U.MD.UI.picture.picnumber]);
  257. /*清空拖拽元素的style值*/
  258. U.MD.UI.picture.DIVBOX.PICAREA.setAttribute('style', '');
  259. /*调整大小,位置*/
  260. U.MD.UI.picture.checkHeight();
  261. /*旋转数初始0*/
  262. U.MD.UI.picture.rotatenum = 0;
  263. }
  264. /**
  265. * 功能:调整图片的大小,图片居中显示,改变左上角的title缩放比例
  266. */
  267. U.MD.UI.picture.checkHeight = function () {
  268. /*长宽比例*/
  269. var _proportion = U.MD.UI.picture.originalSize[0] / U.MD.UI.picture.originalSize[1];
  270. /*如果原始图片的宽比显示框的宽要大,进行调整*/
  271. if (U.MD.UI.picture.originalSize[1] > U.MD.UI.picture.DIVBOX.PICBOX.clientHeight - 40 || U.MD.UI.picture.originalSize[0] > U.MD.UI.picture.DIVBOX.PICBOX.clientWidth) {
  272. /*-40px上下留白*/
  273. U.MD.UI.picture.DIVBOX.PICAREA.style.width = Math.floor((U.MD.UI.picture.DIVBOX.PICBOX.clientHeight - 40) * _proportion) + 'px';
  274. } else {
  275. /*原比例显示*/
  276. U.MD.UI.picture.DIVBOX.PICAREA.style.width = U.MD.UI.picture.originalSize[0] + 'px';
  277. //U.MD.UI.picture.DIVBOX.PICAREA.style.height = U.MD.UI.picture.originalSize[1] + 'px';
  278. }
  279. /*居中显示*/
  280. U.MD.UI.picture.DIVBOX.PICAREA.style.left = (U.MD.UI.picture.DIVBOX.PICBOX.clientWidth - U.MD.UI.picture.DIVBOX.PICAREA.clientWidth) / 2 + 'px';
  281. U.MD.UI.picture.DIVBOX.PICAREA.style.top = (U.MD.UI.picture.DIVBOX.PICBOX.clientHeight - U.MD.UI.picture.DIVBOX.PICAREA.clientHeight) / 2 + 'px';
  282. if (typeof U.MD.UI.picture.DIVBOX.USERPICS[0] === 'object')
  283. U.selectEl('#U_MD_UI_Picture_imgInfo')[0].innerText = U.MD.UI.picture.focuspicsrc.toLowerCase().split('/').splice(-1)[0] + "(" + U.MD.UI.picture.originalSize[0] + "*" + U.MD.UI.picture.originalSize[1] + "px " + U.MD.UI.picture.focuspicsize + ")";
  284. else if (typeof U.MD.UI.picture.DIVBOX.USERPICS[0] === 'string')
  285. U.selectEl('#U_MD_UI_Picture_imgInfo')[0].innerText = U.MD.UI.picture.DIVBOX.USERPICS[U.MD.UI.picture.picnumber].toLowerCase().split('/').splice(-1)[0] + "(" + U.MD.UI.picture.originalSize[0] + "*" + U.MD.UI.picture.originalSize[1] + "px)";
  286. U.selectEl('#U_MD_UI_Picture_r')[0].innerText = Math.round((U.MD.UI.picture.DIVBOX.PICAREA.clientHeight / U.MD.UI.picture.originalSize[1]) * 100) + '%';
  287. }
  288. /**
  289. * 获取图片的原来的大小
  290. * @param img
  291. * @returns {*[]} 返回值1:原始长度,值2:原始宽度
  292. */
  293. U.MD.UI.picture.getImgNaturalDimensions = function (img) {
  294. var nWidth, nHeight;
  295. if (img.naturalWidth) { // 现代浏览器
  296. nWidth = img.naturalWidth;
  297. nHeight = img.naturalHeight
  298. } else { // IE6/7/8
  299. var image = new Image();
  300. image.src = img.src;
  301. nWidth = image.width;
  302. nHeight = image.height;
  303. }
  304. return [nWidth, nHeight]
  305. }
  306. /**
  307. * 拖拽
  308. * @param dragEl 拖拽的元素
  309. * @param bossEl 拖拽限制的范围元素
  310. */
  311. U.MD.UI.picture.drag = function (dragEl, bossEl) {
  312. dragEl.onmousedown = function (e) {
  313. U.MD.UI.picture.mDown(dragEl, bossEl, e)
  314. }
  315. }
  316. /**
  317. * 点击图片所触发的事件
  318. * @param {[object]} dragEl [拖拽的元素]
  319. * @param {[object]} bossEl [拖拽限制的范围元素]
  320. * @param {[event]} e []
  321. */
  322. U.MD.UI.picture.mDown = function (dragEl, bossEl, e) {
  323. e = e || window.event;
  324. /*定义拖拽元素到父级的X距离*/
  325. var _distanceX = e.clientX - dragEl.offsetLeft;
  326. /*定义拖拽元素到父级的Y距离*/
  327. var _distanceY = e.clientY - dragEl.offsetTop;
  328. bossEl.onmousemove = function (e) {
  329. U.MD.UI.picture.mMove(dragEl, bossEl, _distanceX, _distanceY, e);
  330. }
  331. /*鼠标放开,移除事件*/
  332. bossEl.onmouseup = function (e) {
  333. bossEl.onmousemove = null;
  334. bossEl.onmouseup = null;
  335. };
  336. /*禁用默认事件*/
  337. if (e.preventDefault) {
  338. /*常规游览器*/
  339. e.preventDefault();
  340. } else {
  341. /*ie8*/
  342. dragEl.ondragstart = function () {
  343. return false;
  344. };
  345. dragEl.onselectstart = function () {
  346. return false;
  347. };
  348. }
  349. }
  350. /**
  351. * 点击拖动图片时触发的事件
  352. * @param {[object]} dragEl [拖拽的元素]
  353. * @param {[object]} bossEl [拖拽限制的范围元素]
  354. * @param {[object]} _distanceX [拖拽元素到父级的X距离]
  355. * @param {[object]} _distanceY [拖拽元素到父级的Y距离]
  356. * @param {[event]} e []
  357. */
  358. U.MD.UI.picture.mMove = function (dragEl, bossEl, _distanceX, _distanceY, e) {
  359. e = e || window.event;
  360. /*移动距离*/
  361. var _left = e.clientX - _distanceX,
  362. _top = e.clientY - _distanceY;
  363. /*判断是否是最顶部,默认显示窗体宽的1/8*/
  364. _left = U.MD.UI.picture.rotatenum % 2 === 0 ? Math.min(bossEl.clientWidth - bossEl.clientWidth / 8, _left) : Math.min(bossEl.clientWidth - bossEl.clientWidth / 8 - dragEl.clientWidth / 2 + dragEl.clientHeight / 2, _left);
  365. /*判断是否是最底部,默认显示窗体宽的1/8*/
  366. _left = U.MD.UI.picture.rotatenum % 2 === 0 ? Math.max(-dragEl.clientWidth + bossEl.clientWidth / 8, _left) : Math.max(-dragEl.clientWidth + bossEl.clientWidth / 8 + dragEl.clientWidth / 2 - dragEl.clientHeight / 2, _left);
  367. /*判断是否是最左部,默认显示窗体长的1/8*/
  368. _top = U.MD.UI.picture.rotatenum % 2 === 0 ? Math.min(bossEl.clientHeight - bossEl.clientHeight / 8, _top) : Math.min(bossEl.clientHeight - bossEl.clientHeight / 8 + (dragEl.clientWidth - dragEl.clientHeight) / 2, _top);
  369. /*判断是否是最右部,默认显示窗体长的1/8*/
  370. _top = U.MD.UI.picture.rotatenum % 2 === 0 ? Math.max(-dragEl.clientHeight + bossEl.clientHeight / 8, _top) : Math.max(-dragEl.clientHeight + bossEl.clientHeight / 8 - (dragEl.clientWidth - dragEl.clientHeight) / 2, _top);
  371. /*赋值*/
  372. dragEl.style.left = _left + 'px';
  373. dragEl.style.top = _top + 'px';
  374. }
  375. /**
  376. * 滚轮事件
  377. */
  378. U.MD.UI.picture.wheel = function () {
  379. U.MD.UI.picture.DIVBOX.PICBOX.onmousewheel = function () {
  380. this.getWheelDalta()
  381. } .bind(this);
  382. }
  383. /**
  384. * 滚轮事件
  385. * @param e
  386. */
  387. U.MD.UI.picture.getWheelDalta = function (e) {
  388. e = e || window.event;
  389. var delta = 0;
  390. // delta的返回值为正时,表示向前滚动;delta的返回值为负时,表示向后滚动;
  391. if (e.wheelDelta) {
  392. // IE中,鼠标滚轮信息存放在wheelDelta属性中;向前滚动时,wheelDelta的值是120的倍数;向后滚动时,wheelDelta的值是-120的倍数;
  393. delta = e.wheelDelta / 120;
  394. if (window.opera) delta = -delta;
  395. } else if (e.detail) {
  396. // Firefox的鼠标滚轮信息存放在detail属性中;向前滚动,detail的值是-3的倍数;向后滚动,detail的值是3的倍数;
  397. delta = -e.detail / 3;
  398. }
  399. // 执行滚轮事件函数
  400. if (delta > 0) {
  401. U.MD.UI.picture.changeSize(U.MD.UI.picture.scalingvalue);
  402. } else {
  403. U.MD.UI.picture.changeSize(-U.MD.UI.picture.scalingvalue);
  404. }
  405. // 阻止默认行为,防止当页面本身就存在滚动条时出现的异常
  406. prevent(event);
  407. function prevent(evt) {
  408. if (evt.preventDefault) {
  409. evt.preventDefault();
  410. } else {
  411. evt.returnValue = false;
  412. }
  413. }
  414. }
  415. /**
  416. * 改变图片大小
  417. * @param multiple 缩放大小
  418. */
  419. U.MD.UI.picture.changeSize = function (multiple) {
  420. /*添加缩放后, 添加缩放前的宽, 添加缩放前的长*/
  421. var _addOver, _afterH, _afterW;
  422. /*赋值*/
  423. _afterH = U.MD.UI.picture.DIVBOX.PICAREA.clientHeight;
  424. _afterW = U.MD.UI.picture.DIVBOX.PICAREA.clientWidth;
  425. _addOver = U.MD.UI.picture.DIVBOX.PICAREA.clientWidth + multiple;
  426. /*最小缩放长400px*/
  427. if ((_addOver / U.MD.UI.picture.originalSize[0]) * 100 < 20 && _addOver < 200) {
  428. return;
  429. } else if (((U.MD.UI.picture.DIVBOX.PICAREA.clientHeight + multiple) / U.MD.UI.picture.originalSize[1]) * 100 > 1000) { /*最大缩放1000%*/
  430. return
  431. }
  432. /*赋值长*/
  433. U.MD.UI.picture.DIVBOX.PICAREA.style.width = _addOver + 'px';
  434. /*中心缩放*/
  435. U.MD.UI.picture.DIVBOX.PICAREA.style.top = U.MD.UI.picture.DIVBOX.PICAREA.offsetTop - (U.MD.UI.picture.DIVBOX.PICAREA.clientHeight - _afterH) / 2 + 'px';
  436. U.MD.UI.picture.DIVBOX.PICAREA.style.left = U.MD.UI.picture.DIVBOX.PICAREA.offsetLeft - multiple / 2 + 'px';
  437. /*ie8,7,中心缩放位移*/
  438. if (((navigator.appName == "Microsoft Internet Explorer" && navigator.appVersion.match(/8./i) == "8.") || (navigator.appName == "Microsoft Internet Explorer" && navigator.appVersion.match(/7./i) == "7.")) && U.MD.UI.picture.rotatenum % 2 !== 0) {
  439. U.MD.UI.picture.DIVBOX.PICAREA.style.left = U.MD.UI.picture.DIVBOX.PICBOX.clientWidth / 2 - U.MD.UI.picture.DIVBOX.PICAREA.clientHeight / 2 + 'px';
  440. U.MD.UI.picture.DIVBOX.PICAREA.style.top = U.MD.UI.picture.DIVBOX.PICBOX.clientHeight / 2 - U.MD.UI.picture.DIVBOX.PICAREA.clientWidth / 2 + 'px';
  441. }
  442. /*下面都是防止缩放时偏移到窗体外面*/
  443. var topY = U.MD.UI.picture.DIVBOX.PICAREA.clientHeight + U.MD.UI.picture.DIVBOX.PICAREA.offsetTop <= U.MD.UI.picture.DIVBOX.PICBOX.clientHeight / 8 ||
  444. ((U.MD.UI.picture.DIVBOX.PICAREA.clientWidth - (U.MD.UI.picture.DIVBOX.PICAREA.clientWidth - U.MD.UI.picture.DIVBOX.PICAREA.clientHeight) / 2 + U.MD.UI.picture.DIVBOX.PICAREA.offsetTop <= U.MD.UI.picture.DIVBOX.PICBOX.clientHeight / 8) && U.MD.UI.picture.rotatenum % 2 !== 0),
  445. bottomY = U.MD.UI.picture.DIVBOX.PICAREA.offsetTop >= U.MD.UI.picture.DIVBOX.PICBOX.clientHeight - U.MD.UI.picture.DIVBOX.PICBOX.clientHeight / 8 ||
  446. ((Math.abs(U.MD.UI.picture.DIVBOX.PICBOX.clientHeight - (U.MD.UI.picture.DIVBOX.PICAREA.clientWidth - U.MD.UI.picture.DIVBOX.PICAREA.clientHeight) / 2 - U.MD.UI.picture.DIVBOX.PICAREA.offsetTop) <= U.MD.UI.picture.DIVBOX.PICBOX.clientHeight / 8) && U.MD.UI.picture.rotatenum % 2 !== 0),
  447. leftX = U.MD.UI.picture.DIVBOX.PICAREA.clientWidth + U.MD.UI.picture.DIVBOX.PICAREA.offsetLeft <= U.MD.UI.picture.DIVBOX.PICBOX.clientWidth / 8 ||
  448. (U.MD.UI.picture.DIVBOX.PICAREA.clientHeight + (U.MD.UI.picture.DIVBOX.PICAREA.offsetLeft + (U.MD.UI.picture.DIVBOX.PICAREA.clientWidth - U.MD.UI.picture.DIVBOX.PICAREA.clientHeight) / 2 - U.MD.UI.picture.DIVBOX.PICAREA.clientHeight / 2) <= U.MD.UI.picture.DIVBOX.PICBOX.clientWidth / 8 && U.MD.UI.picture.rotatenum % 2 !== 0),
  449. rightX = U.MD.UI.picture.DIVBOX.PICAREA.offsetLeft >= U.MD.UI.picture.DIVBOX.PICBOX.clientWidth - U.MD.UI.picture.DIVBOX.PICBOX.clientWidth / 8 ||
  450. ((U.MD.UI.picture.DIVBOX.PICBOX.clientWidth - (U.MD.UI.picture.DIVBOX.PICAREA.clientWidth - U.MD.UI.picture.DIVBOX.PICAREA.clientHeight) / 2 - U.MD.UI.picture.DIVBOX.PICAREA.offsetLeft) <= U.MD.UI.picture.DIVBOX.PICBOX.clientWidth / 8 && U.MD.UI.picture.rotatenum % 2 !== 0);
  451. if (topY) {
  452. if (U.MD.UI.picture.rotatenum % 2 === 0) {
  453. U.MD.UI.picture.DIVBOX.PICAREA.style.top = U.MD.UI.picture.DIVBOX.PICAREA.offsetTop + (_afterH - U.MD.UI.picture.DIVBOX.PICAREA.clientHeight) / 2 + 'px';
  454. } else {
  455. U.MD.UI.picture.DIVBOX.PICAREA.style.top = U.MD.UI.picture.DIVBOX.PICAREA.offsetTop + (_afterW - U.MD.UI.picture.DIVBOX.PICAREA.clientWidth) / 2 + 'px';
  456. }
  457. }
  458. if (bottomY) {
  459. if (U.MD.UI.picture.rotatenum % 2 === 0) {
  460. U.MD.UI.picture.DIVBOX.PICAREA.style.top = U.MD.UI.picture.DIVBOX.PICAREA.offsetTop - (_afterH - U.MD.UI.picture.DIVBOX.PICAREA.clientHeight) / 2 + 'px';
  461. } else {
  462. U.MD.UI.picture.DIVBOX.PICAREA.style.top = U.MD.UI.picture.DIVBOX.PICAREA.offsetTop - (_afterW - U.MD.UI.picture.DIVBOX.PICAREA.clientWidth) / 2 + 'px';
  463. }
  464. }
  465. if (leftX) {
  466. if (U.MD.UI.picture.rotatenum % 2 === 0) {
  467. U.MD.UI.picture.DIVBOX.PICAREA.style.left = U.MD.UI.picture.DIVBOX.PICAREA.offsetLeft + (_afterW - U.MD.UI.picture.DIVBOX.PICAREA.clientWidth) / 2 + 'px';
  468. } else {
  469. U.MD.UI.picture.DIVBOX.PICAREA.style.left = U.MD.UI.picture.DIVBOX.PICAREA.offsetLeft + (_afterH - U.MD.UI.picture.DIVBOX.PICAREA.clientHeight) / 2 + 'px';
  470. }
  471. }
  472. if (rightX) {
  473. if (U.MD.UI.picture.rotatenum % 2 === 0) {
  474. U.MD.UI.picture.DIVBOX.PICAREA.style.left = U.MD.UI.picture.DIVBOX.PICAREA.offsetLeft - (_afterW - U.MD.UI.picture.DIVBOX.PICAREA.clientWidth) / 2 + 'px';
  475. } else {
  476. U.MD.UI.picture.DIVBOX.PICAREA.style.left = U.MD.UI.picture.DIVBOX.PICAREA.offsetLeft - (_afterH - U.MD.UI.picture.DIVBOX.PICAREA.clientHeight) / 2 + 'px';
  477. }
  478. }
  479. U.selectEl('#U_MD_UI_Picture_r')[0].innerText = Math.floor((U.MD.UI.picture.DIVBOX.PICAREA.clientWidth / U.MD.UI.picture.originalSize[0]) * 100) + '%';
  480. }