wdDragSmallHeight.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611
  1. /**
  2. * by SPACE ^_^
  3. * 2019年5月8日
  4. */
  5. var dragHeight = {};
  6. var lastRecord = {};
  7. // 设定监听回调函数
  8. var observer = new MutationObserver(function(mutations) {
  9. if (mutations.length > 1) {
  10. return;
  11. }
  12. initDragHeight();
  13. var div = $(mutations[0].target);
  14. var divAll = $(div).nextAll("div.scrollbar");
  15. var childerAry = $(div).children("div");
  16. var childerAryLength = $(childerAry).length;
  17. // 自己是最后一个
  18. var isBottomFlag = false; // 当前元素如果是最后一个,为真,否则为假
  19. var ulHeight = getUlTop(); //UL容器高度
  20. if (divAll.length == 0) {
  21. isBottomFlag = true;
  22. }
  23. // 没到底,自己自增高度
  24. if (dragHeight.maxHeight > ulHeight) {
  25. // 新增节点
  26. if (mutations[0].addedNodes.length > mutations[0].removedNodes.length) {
  27. var countHeight = childerAryLength * $(childerAry[0]).height();
  28. // 出现滚动条,就增加高度,否则不增加高度,由样式控制
  29. var h = $(div).height();
  30. if (countHeight >= h) {
  31. } else {
  32. $(div).height(($(div).height() + lastRecord.height) + "px");
  33. }
  34. }
  35. // 删除节点
  36. else {
  37. // 自己设置了高度,就判断子元素总高度是否大于当前高度
  38. if (style.indexOf("height") != -1) {
  39. var countHeight = childerAryLength * lastRecord.height;
  40. // 总高度大于当前元素高度,不做处理
  41. if (countHeight > $(div).height()) {}
  42. // 总高度小于当前元素高度,删除掉高度样式
  43. else {
  44. $(div).css({
  45. height: ""
  46. });
  47. }
  48. }
  49. }
  50. }
  51. // 到底了,缩小上面的同辈元素
  52. else {
  53. var allBt = false; // 是否全部元素已经缩到最小,全部缩小为真,否则为假
  54. var comp = function(divAll, allBt) {
  55. for (var i = 0; i < divAll.length; i++) {
  56. var height = $(divAll[i]).height();
  57. // 当前元素不能被缩放
  58. if ((height <= lastRecord.height) || ((height - lastRecord.height) < lastRecord.height)) {
  59. // 没有到底,且所有元素都已经缩到最小,就只放大自己
  60. var ulHeight2 = getUlTop(); //UL容器高度
  61. if (dragHeight.maxHeight > ulHeight2 && i == divAll.length - 1) {}
  62. // 到底,啥都不做
  63. else {
  64. continue;
  65. }
  66. } else {
  67. allBt = true;
  68. $(divAll[i]).height((height - lastRecord.height) + "px");
  69. break;
  70. }
  71. }
  72. }
  73. // 自己是最后一个
  74. if (isBottomFlag) {
  75. divAll = $(div).prevAll("div.scrollbar");
  76. comp(divAll, allBt);
  77. }
  78. // 不是最后一个
  79. else {
  80. divAll = $(div).nextAll("div.scrollbar");
  81. comp(divAll, allBt);
  82. // 下面的都不能缩小了,就缩小上面的
  83. if (!allBt) {
  84. divAll = $(div).prevAll("div.scrollbar");
  85. // 上面没有元素了
  86. if (divAll.length == 0) {
  87. allBt = true;
  88. } else {
  89. comp(divAll, allBt);
  90. }
  91. }
  92. }
  93. // 全部都不能再缩小了,就给自己添加一个高度
  94. if (allBt) {
  95. $(div).height($(div).height() + "px");
  96. }
  97. }
  98. $(div).next("div.plugin-dragHeight").remove();
  99. if (isBottomFlag || childerAryLength == 0 || childerAryLength == 1) {
  100. $(div).after('<div class="plugin-dragHeight" style="display:none;"><span></span></div>');
  101. } else {
  102. $(div).after('<div class="plugin-dragHeight"><span></span></div>');
  103. }
  104. initAllEvent();
  105. });
  106. $(document).ready(function() {
  107. $("div.iconList-div").on('click', function() {
  108. console.log("div.iconList-div ——————>")
  109. setTimeout(function() {
  110. initDragHeight();
  111. }, 100);
  112. })
  113. $("div.cardList-div").on('click', function() {
  114. console.log("div.cardList-div ——————>")
  115. setTimeout(function() {
  116. initDragHeight();
  117. }, 100);
  118. })
  119. $("body").on('click', "div.td_div", function() {
  120. console.log("div.td_div-div ——————>")
  121. setTimeout(function() {
  122. initDragHeight();
  123. }, 100);
  124. })
  125. var target = $("[ssRbar] div.scrollbar"); // ("[wdRightBar]。Lin
  126. for (var i = 0; i < target.length; i++) {
  127. observer.observe($(target[i])[0], {
  128. childList: true,
  129. })
  130. }
  131. initMaxMinTitle();
  132. })
  133. function initDragHeight() {
  134. var dragHeightAry = $("[ssRbar]"); // ("[wdRightBar]")。Lin
  135. if (dragHeightAry.length == 0) {
  136. console.log("未发现 ssRbar 标记");
  137. return false;
  138. }
  139. dragHeight.contentDom = $(dragHeightAry[0]);
  140. dragHeight.ulDom = $(dragHeight.contentDom).children("ul")[0];
  141. dragHeight.initDragResult = function() {
  142. $(dragHeight.ulDom).find("div.plugin-dragHeight").remove();
  143. var cDivAry = $(dragHeight.ulDom).children("div.scrollbar");
  144. for (var i = 0; i < cDivAry.length; i++) {
  145. var childerAry = $(cDivAry[i]).children("div.sideList");
  146. var childerAryLength = $(childerAry).length;
  147. if (childerAryLength == 0) {
  148. childerAry = $(cDivAry[i]).children("span.sideList");
  149. childerAryLength = $(childerAry).length;
  150. }
  151. if (childerAryLength == 0) {
  152. $(cDivAry[i]).height("0px");
  153. $(cDivAry[i]).css({
  154. height: ""
  155. });
  156. } else {
  157. lastRecord.height = $(childerAry[0]).height();
  158. if (childerAryLength < 3) {
  159. $(cDivAry[i]).height(($(childerAry[0]).height() * childerAryLength) + "px");
  160. } else {
  161. $(cDivAry[i]).height(($(childerAry[0]).height() * 3) + "px");
  162. }
  163. }
  164. if (i == (cDivAry.length - 1) || childerAryLength == 0 || childerAryLength == 1) {
  165. $(cDivAry[i]).after('<div class="plugin-dragHeight" style="display:none;"><span class="handlebtn"></span></div>');
  166. continue;
  167. }
  168. $(cDivAry[i]).after('<div class="plugin-dragHeight"><span class="handlebtn"></span></div>');
  169. }
  170. return true;
  171. };
  172. dragHeight.initDragResult();
  173. dragHeight.maxHeight = $(dragHeight.contentDom).height();
  174. dragHeight.dragAry = $(dragHeight.ulDom).children("div.plugin-dragHeight");
  175. for (var i = 0; i < dragHeight.dragAry.length; i++) {
  176. initEvent(dragHeight.dragAry[i], i);
  177. }
  178. // 高度自适应调整
  179. initAutoHeight();
  180. }
  181. function initAutoHeight() {
  182. dragHeight.ulDom = $(dragHeight.contentDom).children("ul")[0];
  183. var cDivAry = $(dragHeight.ulDom).children("div.scrollbar");
  184. console.log("dragHeight.maxHeight=" + dragHeight.maxHeight);
  185. var ulHeight = getUlTop(); //UL容器高度
  186. for (var i = 0; i < cDivAry.length; i++) {
  187. var _height = $(cDivAry[i]).height();
  188. var _childerAry = $(cDivAry[i]).children("div.sideList");
  189. if (_height == 0 || _childerAry.length == 0) {
  190. continue;
  191. }
  192. var _childrenAryLength = $(_childerAry).length;
  193. var _childrenHeight = $(_childerAry[0]).height();
  194. var _countHeight = _childrenAryLength * _childrenHeight; // 理论总高度
  195. var autoFlag = false; // 处理完成为真,否则为假
  196. // 出现滚动条
  197. if (_countHeight > _height) {
  198. if (autoFlag) {
  199. continue;
  200. }
  201. // 误差高度
  202. var wc = _countHeight - _height;
  203. // 获取误差高度加上之后的总高度
  204. var _totalHeight = ulHeight + wc;
  205. if (_totalHeight > dragHeight.maxHeight) { // 大于最大高度,裁剪部分高度
  206. autoFlag = true;
  207. var h = $(cDivAry[i]).height() + (dragHeight.maxHeight - ulHeight);
  208. if (h < _childrenHeight) {
  209. h = _childrenHeight;
  210. }
  211. $(cDivAry[i]).height(h + "px");
  212. // break;
  213. }
  214. // 小于最大高度,全部展开
  215. else {
  216. $(cDivAry[i]).height(_countHeight + "px");
  217. ulHeight = getUlTop();
  218. }
  219. } else {
  220. $(cDivAry[i]).css({
  221. height: ""
  222. });
  223. }
  224. }
  225. }
  226. function initAllEvent() {
  227. dragHeight.dragAry = $(dragHeight.ulDom).children("div.plugin-dragHeight");
  228. for (var i = 0; i < dragHeight.dragAry.length; i++) {
  229. initEvent(dragHeight.dragAry[i], i);
  230. }
  231. }
  232. function initEvent(obj, index) {
  233. obj.onmousedown = function(ev) {
  234. var ev = ev || window.event;
  235. var locaY = ev.clientY; // Y轴坐标
  236. var divObj = $(obj).prev("div.scrollbar"); // 内容容器
  237. var objHeight = $(divObj).height(); // 当前所属容器的高度
  238. var objSize = getMaxMinHeight(divObj); // 当前所属容器的最大高度和最小高度
  239. var nextDom = getNextDivObj(index); // 当前元素的下一个有内容的元素
  240. document.onmousemove = function(ev) {
  241. dragHeight.ulHeight = getUlTop(); // 获取外层容器高度
  242. var ev = ev || window.event;
  243. var curHeight = ev.clientY - locaY + objHeight;
  244. var isMinHeight = false; // 如果最小高度,为真,否则为假
  245. if (curHeight < objSize.min) {
  246. curHeight = objSize.min;
  247. isMinHeight = true;
  248. }
  249. if (curHeight > objSize.max) {
  250. curHeight = objSize.max;
  251. }
  252. if (curHeight < objHeight) {
  253. for (var i = index; i < dragHeight.dragAry.length; i++) {
  254. // 当前元素的下一个有内容的元素
  255. var _nextDom = getNextAutoDivObj(i);
  256. if (_nextDom == undefined) { // 元素为空
  257. // 自己还可以缩小
  258. if (!isMinHeight) {
  259. $(divObj).height(curHeight + 'px');
  260. }
  261. continue;
  262. }
  263. // 最后一个且最大高度
  264. if (_nextDom.index == dragHeight.dragAry.length - 1 && _nextDom.height >= _nextDom.max) {
  265. $(divObj).height(curHeight + 'px');
  266. continue;
  267. }
  268. if (_nextDom.height >= _nextDom.max) {
  269. continue;
  270. }
  271. var _h = 0;
  272. _h = _nextDom.height + ($(divObj).height() - curHeight);
  273. if (_h <= _nextDom.min) {
  274. _h = _nextDom.min;
  275. } else if (_h >= _nextDom.max) {
  276. _h = _nextDom.max;
  277. }
  278. $(dragHeight.dragAry[_nextDom.index]).prev("div.scrollbar").height(_h + "px");
  279. $(divObj).height(curHeight + 'px');
  280. }
  281. return false;
  282. }
  283. var nextDomHeight = 0;
  284. if (nextDom != undefined) {
  285. nextDomHeight = nextDom.height + (objHeight - curHeight);
  286. if (nextDomHeight < nextDom.min) {
  287. nextDomHeight = nextDom.min;
  288. }
  289. if (nextDomHeight > nextDom.max) {
  290. nextDomHeight = nextDom.max;
  291. }
  292. }
  293. // 碰到底部啦
  294. if ((dragHeight.ulHeight > dragHeight.maxHeight && nextDom == undefined && curHeight >= $(divObj).height()) ||
  295. (dragHeight.ulHeight > dragHeight.maxHeight && nextDom != undefined && nextDomHeight <= nextDom.min)) {
  296. console.log("bottom");
  297. for (var i = index + 1; i < dragHeight.dragAry.length; i++) {
  298. // 当前元素的下一个有内容的元素
  299. var _nextDom = getNextAutoDivObj(i);
  300. if (_nextDom == undefined) { // 元素为空
  301. continue;
  302. }
  303. if (_nextDom.height <= _nextDom.min) {
  304. continue;
  305. }
  306. var _h = 0;
  307. _h = _nextDom.height + ($(divObj).height() - curHeight);
  308. if (_h <= _nextDom.min) {
  309. _h = _nextDom.min;
  310. } else if (_h >= _nextDom.max) {
  311. _h = _nextDom.max;
  312. }
  313. $(dragHeight.dragAry[_nextDom.index]).prev("div.scrollbar").height(_h + "px");
  314. $(divObj).height(curHeight + 'px');
  315. }
  316. return false;
  317. }
  318. if (nextDom != undefined) {
  319. $(dragHeight.dragAry[nextDom.index]).prev("div.scrollbar").height(nextDomHeight + "px");
  320. }
  321. $(divObj).height(curHeight + 'px');
  322. }
  323. document.onmouseup = function() {
  324. var _t = getUlTop();
  325. if (_t > dragHeight.maxHeight) {
  326. var _h = $(divObj).height();
  327. _h = _h - (_t - dragHeight.maxHeight);
  328. var size = getMaxMinHeight(divObj);
  329. // 不能再减了!
  330. if (_h <= size.min) {
  331. for (var i = index + 1; i < dragHeight.dragAry.length; i++) {
  332. var _nextDom = getNextAutoDivObj(i);
  333. var _oS = (_t - dragHeight.maxHeight);
  334. if ((_nextDom.height - _oS) < _nextDom.min) {
  335. continue;
  336. } else {
  337. $(dragHeight.dragAry[_nextDom.index]).prev("div.scrollbar").height(_nextDom.height - _oS + "px");
  338. break;
  339. }
  340. }
  341. } else {
  342. $(divObj).height(_h + "px");
  343. }
  344. }
  345. document.onmousemove = null;
  346. document.onmouseup = null;
  347. }
  348. return false;
  349. }
  350. }
  351. /**
  352. * 获取一个容器的最大宽度和最小宽度
  353. * @param {Object} obj
  354. */
  355. function getMaxMinHeight(obj) {
  356. var cAry = $(obj).children("div");
  357. if (cAry.length == 0) {
  358. return {
  359. max: 0,
  360. min: 0
  361. }
  362. }
  363. return {
  364. max: ($(cAry[0]).height()) * cAry.length,
  365. min: $(cAry[0]).height()
  366. }
  367. }
  368. /**
  369. * 获取当前元素的下一个有内容的Div对象
  370. * @param {Object} obj
  371. * @param {Object} index
  372. */
  373. function getNextDivObj(index) {
  374. var dom;
  375. index += 1;
  376. for (var i = index; i < dragHeight.dragAry.length; i++) {
  377. // DIV内容 容器
  378. var nextDivObjDom = $(dragHeight.dragAry[i]).prev("div.scrollbar");
  379. // 容器元素数组
  380. var cAry = $(nextDivObjDom).children("div");
  381. if (cAry.length == 0) {
  382. index = i;
  383. continue;
  384. } else {
  385. dom = getMaxMinHeight(nextDivObjDom);
  386. dom.index = i;
  387. dom.height = $(nextDivObjDom).height();
  388. return dom;
  389. }
  390. }
  391. return undefined;
  392. }
  393. /**
  394. * 获取当前元素的下一个有内容的Div对象,且高度未达到最小高度
  395. * @param {Object} obj
  396. * @param {Object} index
  397. */
  398. function getNextAutoDivObj(index) {
  399. var dom;
  400. index += 1;
  401. for (var i = index; i < dragHeight.dragAry.length; i++) {
  402. // DIV内容 容器
  403. var nextDivObjDom = $(dragHeight.dragAry[i]).prev("div.scrollbar");
  404. // 容器元素数组
  405. var cAry = $(nextDivObjDom).children("div");
  406. if (cAry.length == 0) {
  407. index = i;
  408. continue;
  409. }
  410. dom = getMaxMinHeight(nextDivObjDom);
  411. if (dom.height <= dom.min) {
  412. dom = undefined;
  413. continue;
  414. }
  415. dom.index = i;
  416. dom.height = $(nextDivObjDom).height();
  417. return dom;
  418. }
  419. return undefined;
  420. }
  421. /**
  422. * 获取当前父容器的高度
  423. */
  424. function getUlTop() {
  425. return $(dragHeight.ulDom).height();
  426. }
  427. function initMaxMinTitle() {
  428. $("li.hLine").on("dblclick", function() {
  429. var status = $(this).attr("winStatus");
  430. if (!status) {
  431. status = "min";
  432. }
  433. if (status == "min") {
  434. var ulHeight = getUlTop(); //UL容器高度
  435. $(dragHeight.ulDom).find("li.hLine").hide();
  436. $(dragHeight.ulDom).find("div.scrollbar").hide();
  437. var dragDiv = $(dragHeight.ulDom).find("div.plugin-dragHeight");
  438. for (var i = 0; i < dragDiv.length; i++) {
  439. if(!$(dragDiv[i]).is(":hidden")) {
  440. $(dragDiv[i]).attr("status", "show");
  441. }
  442. }
  443. $(dragHeight.ulDom).find("div.plugin-dragHeight").hide();
  444. var cont_div = $(this).next("div.scrollbar");
  445. $(this).show();
  446. $(cont_div).show();
  447. $(this).attr("oldHeight", $(cont_div).height()); // 原高度
  448. $(this).attr("winStatus", "max"); // 代表已经最大了
  449. var thisHeight = $(this).height(); // 当前标题的高度
  450. var contDivHeight = ulHeight - thisHeight;
  451. if($(cont_div).attr("wdDragSmallMaxHeight")){
  452. //放大后的高度
  453. contDivHeight=$(cont_div).attr("wdDragSmallMaxHeight");
  454. $(cont_div).css({"max-height":contDivHeight+"px"});
  455. }
  456. $(cont_div).height(contDivHeight + "px");
  457. } else {
  458. var oldHeight = $(this).attr("oldHeight"); // 原高度
  459. $(this).attr("winStatus", "min"); // 代表是正常状态
  460. var cont_div = $(this).next("div.scrollbar");
  461. $(cont_div).height(oldHeight + "px");
  462. $(cont_div).css({"max-height":null});
  463. $(dragHeight.ulDom).find("li.hLine").show();
  464. $(dragHeight.ulDom).find("div.scrollbar").show();
  465. $(dragHeight.ulDom).find("div.plugin-dragHeight[status='show']").show();
  466. }
  467. });
  468. };