heat_map.html 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. {extend name="base"/}
  2. {block name="resources"}
  3. <link rel="stylesheet" href="SHOP_CSS/jquery-ui.css">
  4. <style>
  5. .content-box {position: relative;width: 800px;height: 400px;background-color: rgb(242, 244, 244);top:50%;left:50%;transform: translate(-50%,8%);border: 1px dashed #333;background-size:100% 100%;}
  6. .box {width: 100px;height: 100px;background-color: rgba(255,255,255,0.7);border: 1px solid #ccc;position: absolute;left: 0px;top: 0px;}
  7. .box1,.box2,.box3,.box4 {width: 10px;height: 10px;background-color: #fff;position: absolute;border-radius: 50%; border: 1px solid #333;}
  8. .box1 {top: -5px;left: -5px;cursor: nw-resize;}
  9. .box2 {top: -5px;right: -5px;cursor: ne-resize;}
  10. .box3 {left: -5px;bottom: -5px;cursor: sw-resize;}
  11. .box4 {bottom: -5px;right: -5px;cursor: se-resize;}
  12. .box .link-opt{padding:2px 0px 0px 6px;}
  13. .box .link-opt span{cursor: pointer;padding-right:4px;color:blue;}
  14. .add-box-btn{position: fixed;left:10px;top:10px;}
  15. .link-res{padding-left:6px;line-height:14px;}
  16. </style>
  17. {/block}
  18. {block name="body"}
  19. <div class="layui-form">
  20. <div class="content-box" id="content_box"></div>
  21. <button class="layui-btn add-box-btn" id="addBox">增加热区</button>
  22. </div>
  23. {/block}
  24. {block name="script"}
  25. <script>
  26. //热区设置数据
  27. let imageData = JSON.parse(sessionStorage.getItem('imageData'));
  28. let dragBoxArr = {};
  29. let contentBoxWidth = 600;
  30. let contentBoxHeight = 600;
  31. //初始化内容盒子
  32. $(function(){
  33. initContentBox();
  34. });
  35. //增加标记点
  36. $("#addBox").click(function(){
  37. let dragBox = new DragBox();
  38. dragBoxArr[dragBox.getIndex()] = dragBox;
  39. });
  40. //拖动类
  41. class DragBox {
  42. constructor(param) {
  43. //初始化数据
  44. this.index = null;
  45. this.oDiv = null;
  46. //相关处理
  47. this.createBox(param);
  48. this.bindDragEvent();
  49. this.bindResizeEvent();
  50. this.bindDeleteEvent();
  51. this.bindSelectLinkEvent();
  52. }
  53. //创建盒子
  54. createBox(param){
  55. let that = this;
  56. //创建盒子的索引
  57. let index = 0;
  58. let index_arr = Object.keys(dragBoxArr);
  59. if(index_arr.length > 0){
  60. index = Number(index_arr[index_arr.length - 1]) + 1;
  61. }
  62. let id = 'box_' + index;
  63. //初始化盒子数据
  64. let box_data = {
  65. left: '0px',
  66. top: '0px',
  67. width: '100px',
  68. height: '100px',
  69. link: null,
  70. };
  71. if(param && param.init_data){
  72. box_data = param.init_data;
  73. box_data.width += '%';
  74. box_data.height += '%';
  75. box_data.left += '%';
  76. box_data.top += '%';
  77. }
  78. let link_title = box_data.link ? box_data.link.title : '';
  79. that.link = box_data.link;
  80. let box_html = `
  81. <div class="box" id="${id}" style="width:${box_data.width};height:${box_data.height};left:${box_data.left};top:${box_data.top};">
  82. <span class="box1"></span>
  83. <span class="box2"></span>
  84. <span class="box3"></span>
  85. <span class="box4"></span>
  86. <div class="link-opt">
  87. <span class="select">选择</span>
  88. <span class="delete">删除</span>
  89. </div>
  90. <div class="link-res">${link_title}</div>
  91. </div>`;
  92. $("#content_box").append(box_html);
  93. that.oDiv = document.getElementById(id);
  94. that.index = index;
  95. }
  96. //删除盒子
  97. bindDeleteEvent() {
  98. let that = this;
  99. let index = that.index;
  100. $("#box_"+ index +" .delete").click(function(){
  101. $("#box_" + index).remove();
  102. delete dragBoxArr[index];
  103. })
  104. }
  105. //选择链接
  106. bindSelectLinkEvent() {
  107. let that = this;
  108. let index = that.index;
  109. $("#box_" + index + " .select").click(function () {
  110. let link = that.link || {};
  111. ns.select_link(link, function (data) {
  112. that.link = data;
  113. $("#box_" + index + " .link-res").html(data.title);
  114. });
  115. })
  116. }
  117. //绑定拖动事件
  118. bindDragEvent() {
  119. const that = this;
  120. let disX, disY;
  121. this.oDiv.onmousedown = function (e) {
  122. disX = e.clientX - that.oDiv.offsetLeft;
  123. disY = e.clientY - that.oDiv.offsetTop;
  124. //鼠标移动时
  125. document.onmousemove = function (e) {
  126. var e = e || window.event;
  127. that.oDiv.style.left = e.clientX - disX + 'px';
  128. that.oDiv.style.top = e.clientY - disY + 'px';
  129. //边界判断
  130. if(e.clientX - disX < 0) {
  131. that.oDiv.style.left = 0;
  132. }
  133. if(e.clientX - disX > contentBoxWidth - that.oDiv.offsetWidth) {
  134. that.oDiv.style.left = contentBoxWidth - that.oDiv.offsetWidth + 'px';
  135. }
  136. if(e.clientY - disY< 0) {
  137. that.oDiv.style.top = 0;
  138. }
  139. if(e.clientY - disY > contentBoxHeight - that.oDiv.offsetHeight) {
  140. that.oDiv.style.top = contentBoxHeight - that.oDiv.offsetHeight + 'px';
  141. }
  142. // console.log('left:',that.oDiv.style.left,'top:',that.oDiv.style.top);
  143. }
  144. //鼠标抬起时
  145. document.onmouseup = function (e) {
  146. document.onmousemove = null;
  147. }
  148. }
  149. }
  150. //绑定调整大小事件
  151. bindResizeEvent() {
  152. const that = this;
  153. let aSpan = that.oDiv.getElementsByTagName('span');
  154. for(let i = 0; i < aSpan.length; i++) {
  155. that.dragFn(aSpan[i]);
  156. }
  157. }
  158. //单个角拖动事件
  159. dragFn(obj) {
  160. const that = this;
  161. obj.onmousedown = function (e) {
  162. var oEv = e || window.event;
  163. // 阻止事件的冒泡
  164. oEv.stopPropagation();
  165. // 获取移动前盒子的宽高,
  166. var oldWidth = that.oDiv.offsetWidth;
  167. var oldHeight = that.oDiv.offsetHeight;
  168. // 获取鼠标距离屏幕的left和top值
  169. var oldX = oEv.clientX;
  170. var oldY = oEv.clientY;
  171. // 元素相对于最近的父级定位
  172. var oldLeft = that.oDiv.offsetLeft;
  173. var oldTop = that.oDiv.offsetTop;
  174. // 设置最小的宽度
  175. var minWidth = 20
  176. var minHeight = 20
  177. document.onmousemove = function (e) {
  178. var oEv = e || event;
  179. // 左上角
  180. if (obj.className == "box1") {
  181. if (minWidth > oldWidth - (oEv.clientX - oldX)) {
  182. return
  183. } else if (minHeight > oldHeight - (oEv.clientY - oldY)) {
  184. return
  185. }
  186. // 移动后盒子的宽
  187. that.oDiv.style.width = oldWidth - (oEv.clientX - oldX) + 'px';
  188. // 高度同理
  189. that.oDiv.style.left = oldLeft + (oEv.clientX - oldX) + 'px';
  190. //左上 宽
  191. if(oldLeft + (oEv.clientX - oldX) < 0) {
  192. that.oDiv.style.left = 0;
  193. that.oDiv.style.width = oldWidth - (oEv.clientX - oldX) + (oldLeft + (oEv.clientX - oldX)) + 'px';
  194. }
  195. that.oDiv.style.height = oldHeight - (oEv.clientY - oldY) + 'px';
  196. that.oDiv.style.top = oldTop + (oEv.clientY - oldY) + 'px';
  197. //左上 高
  198. if(oldTop + (oEv.clientY - oldY) < 0) {
  199. that.oDiv.style.top = 0;
  200. that.oDiv.style.height = oldTop + (oEv.clientY - oldY) + (oldHeight - (oEv.clientY - oldY)) + 'px';
  201. }
  202. } else if (obj.className == "box2") {
  203. // 右上角
  204. if (minWidth > oldWidth - (oEv.clientX - oldX)) {
  205. return
  206. } else if (minHeight > oldHeight - (oEv.clientY - oldY)) {
  207. return
  208. }
  209. that.oDiv.style.width = oldWidth + (oEv.clientX - oldX) + 'px';
  210. that.oDiv.style.right = oldLeft - (oEv.clientX - oldX) + 'px';
  211. let nowWidth = oldWidth + (oEv.clientX - oldX) + oldLeft;
  212. //右上 宽
  213. if(nowWidth > contentBoxWidth) {
  214. that.oDiv.style.width = contentBoxWidth - oldLeft + 'px';
  215. }
  216. that.oDiv.style.height = oldHeight - (oEv.clientY - oldY) + 'px';
  217. that.oDiv.style.top = oldTop + (oEv.clientY - oldY) + 'px';
  218. //右上 高
  219. if(oldTop + (oEv.clientY - oldY) < 0) {
  220. that.oDiv.style.top = 0;
  221. that.oDiv.style.height = oldTop + (oEv.clientY - oldY) + (oldHeight - (oEv.clientY - oldY)) + 'px';
  222. }
  223. } else if (obj.className == "box3") {
  224. // 左下角
  225. if (minWidth > oldWidth - (oEv.clientX - oldX)) {
  226. return
  227. } else if (minHeight > oldHeight - (oEv.clientY - oldY)) {
  228. return
  229. }
  230. that.oDiv.style.width = oldWidth - (oEv.clientX - oldX) + 'px';
  231. that.oDiv.style.left = oldLeft + (oEv.clientX - oldX) + 'px';
  232. //左下
  233. if(oldLeft + (oEv.clientX - oldX) < 0) {
  234. that.oDiv.style.left = 0;
  235. that.oDiv.style.width = oldWidth - (oEv.clientX - oldX) + (oldLeft + (oEv.clientX - oldX)) + 'px';
  236. }
  237. that.oDiv.style.height = oldHeight + (oEv.clientY - oldY) + 'px';
  238. that.oDiv.style.bottom = oldTop + (oEv.clientY + oldY) + 'px';
  239. //左下
  240. if(oldTop + oldHeight + (oEv.clientY - oldY) > contentBoxHeight) {
  241. that.oDiv.style.height = contentBoxHeight - oldTop + 'px';
  242. }
  243. } else if (obj.className == "box4") {
  244. // 右下角
  245. if (minWidth > oldWidth - (oEv.clientX - oldX)) {
  246. return
  247. } else if (minHeight > oldHeight - (oEv.clientY - oldY)) {
  248. return
  249. }
  250. that.oDiv.style.width = oldWidth + (oEv.clientX - oldX) + 'px';
  251. that.oDiv.style.right = oldLeft - (oEv.clientX - oldX) + 'px';
  252. let nowWidth = oldWidth + (oEv.clientX - oldX) + oldLeft;
  253. //右下 宽
  254. if(nowWidth > contentBoxWidth) {
  255. //that.oDiv.style.left = 0;
  256. that.oDiv.style.width = contentBoxWidth - oldLeft + 'px';
  257. }
  258. that.oDiv.style.height = oldHeight + (oEv.clientY - oldY) + 'px';
  259. that.oDiv.style.bottom = oldTop + (oEv.clientY + oldY) + 'px';
  260. //左下
  261. if(oldTop + oldHeight + (oEv.clientY - oldY) > contentBoxHeight) {
  262. that.oDiv.style.height = contentBoxHeight - oldTop + 'px';
  263. }
  264. }
  265. }
  266. //鼠标抬起时
  267. document.onmouseup = function () {
  268. document.onmousemove = null;
  269. document.onmouseup = null;
  270. }
  271. }
  272. }
  273. //获取索引
  274. getIndex(){
  275. return this.index;
  276. }
  277. //获取数据
  278. getData() {
  279. let that = this;
  280. let index = that.index;
  281. let dom = $("#box_" + index);
  282. let position = dom.position();
  283. let data = {
  284. width : dom.width() / contentBoxWidth * 100,
  285. height : dom.height() / contentBoxHeight * 100,
  286. left : position.left / contentBoxWidth * 100,
  287. top : position.top / contentBoxHeight * 100,
  288. link : that.link,
  289. }
  290. return data;
  291. }
  292. }
  293. //初始化盒子
  294. function initContentBox(){
  295. if(imageData == null) return;
  296. if(imageData.imgWidth > imageData.imgHeight){
  297. contentBoxHeight = contentBoxWidth * imageData.imgHeight / imageData.imgWidth;
  298. }else{
  299. contentBoxWidth = contentBoxHeight * imageData.imgWidth / imageData.imgHeight;
  300. }
  301. $("#content_box").css({backgroundImage: "url("+ ns.img(imageData.imageUrl) +")", width: contentBoxWidth + 'px', height: contentBoxHeight + 'px'});
  302. if(imageData.heatMapData){
  303. $.each(imageData.heatMapData, function(index, item){
  304. let dragBox = new DragBox({
  305. init_data : item,
  306. });
  307. dragBoxArr[dragBox.getIndex()] = dragBox;
  308. })
  309. }
  310. }
  311. //获取操作结果
  312. function getIframeRes(callback){
  313. let heatMapData = [];
  314. let checkRes = true;
  315. $.each(dragBoxArr, function(index, item){
  316. var data = item.getData();
  317. if(!data.link){
  318. parent.layer.msg('请选择热区链接');
  319. checkRes = false;
  320. return false;
  321. }
  322. heatMapData.push(data);
  323. })
  324. if(!checkRes) return;
  325. imageData.heatMapData = heatMapData;
  326. console.log(heatMapData);
  327. if(typeof callback == 'function'){
  328. callback(imageData);
  329. }
  330. }
  331. </script>
  332. {/block}