You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

507 lines
17 KiB

  1. /*
  2. * 版 本 Learun-ADMS V7.0.3 力软敏捷开发框架(http://www.learun.cn)
  3. * Copyright (c) 2013-2018 上海力 软信息技术有限公司
  4. * 创建人:力 软-前端开发组
  5. * 日 期:2018.04.19
  6. * 描 述:滚动条优化
  7. */
  8. (function ($, learun, window) {
  9. "use strict";
  10. var $move = null;
  11. var methods = {
  12. init: function ($this, callback) {
  13. var id = $this.attr('id');
  14. if (!id) {
  15. id = 'lr_' + learun.newGuid();
  16. $this.attr('id', id);
  17. }
  18. $this.addClass('lr-scroll-wrap');
  19. // 加载内容
  20. var $content = $this.children();
  21. var $scroll = $('<div class="lr-scroll-box" id="' + id + '_box" ></div>');
  22. $this.append($scroll);
  23. $scroll.append($content);
  24. // 加载y滚动条
  25. var $vertical = $('<div class="lr-scroll-vertical" ><div class="lr-scroll-vertical-block" id="' + id + '_vertical"></div></div>')
  26. $this.append($vertical);
  27. // 加载x滚动条
  28. var $horizontal = $('<div class="lr-scroll-horizontal" ><div class="lr-scroll-horizontal-block" id="' + id + '_horizontal"></div></div>')
  29. $this.append($horizontal);
  30. // 添加一个移动板
  31. if ($move === null) {
  32. $move = $('<div style="-moz-user-select: none;-webkit-user-select: none;-ms-user-select: none;-khtml-user-select: none;user-select: none;display: none;position: fixed;top: 0;left: 0;width: 100%;height: 100%;z-index: 9999;cursor: pointer;" ></div>');
  33. $('body').append($move);
  34. }
  35. // 初始化数据
  36. var sh = $scroll.innerHeight();
  37. var sw = $scroll.innerWidth();
  38. var h = $this.height();
  39. var w = $this.width();
  40. var data = {
  41. id: id,
  42. sy: 0,
  43. sx: 0,
  44. sh: sh,
  45. sw: sw,
  46. h: h,
  47. w: w,
  48. yh: 0,
  49. xw: 0,
  50. callback: callback
  51. };
  52. $this[0].op = data;
  53. methods.update($this);
  54. methods.bindEvent($this, $scroll, $vertical, $horizontal);
  55. $scroll = null;
  56. $content = null;
  57. $vertical = null;
  58. $horizontal = null;
  59. $this = null;
  60. },
  61. bindEvent: function ($this, $scroll, $vertical, $horizontal) { // 绑定监听事件
  62. // div大小变化
  63. $this.resize(function () {
  64. var $this = $(this);
  65. var op = $this[0].op;
  66. if (op) {
  67. var h = $this.height();
  68. var w = $this.width();
  69. if (h != op.h) {
  70. op.h = h;
  71. methods.updateY($this);
  72. }
  73. if (w != op.w) {
  74. op.w = w;
  75. methods.updateX($this);
  76. }
  77. }
  78. $this = null;
  79. });
  80. $scroll.resize(function () {
  81. var $this = $(this);
  82. var $scrollWrap = $this.parent();
  83. var op = $scrollWrap[0].op;
  84. if (op) {
  85. var sh = $this.innerHeight();
  86. var sw = $this.innerWidth();
  87. if (sh != op.sh) {
  88. op.sh = sh;
  89. methods.updateY($scrollWrap);
  90. }
  91. if (sw != op.sw) {
  92. op.sw = sw;
  93. methods.updateX($scrollWrap);
  94. }
  95. }
  96. $this = null;
  97. $scrollWrap = null;
  98. });
  99. // 监听鼠标滚动
  100. $this.mousewheel(function (event, delta, deltaX, deltaY) {
  101. var $this = $(this);
  102. var $select = $this.find('.lr-select-focus');
  103. if ($select.length > 0) {
  104. var selectId = "learun_select_option_" + $select.attr('id');
  105. $('#' + selectId).slideUp(150);
  106. $select.removeClass('lr-select-focus');
  107. }
  108. var _v = 4 + (Math.abs(delta) - 1) * 0.1 * event.deltaFactor;
  109. if (_v > 16 && _v < 300) {
  110. _v = 16;
  111. }
  112. else if (_v >= 300) {
  113. _v = 20;
  114. }
  115. var op = $this[0].op;
  116. var d = delta * _v;// var d = delta * 20;
  117. if (op.sh > op.h) {
  118. op.oldsy = op.sy;
  119. op.sy = op.sy - d;
  120. setTimeout(function () {
  121. methods.moveY($this, true, true);
  122. $this = null;
  123. });
  124. if (op.h + op.sy < op.sh && op.sy > 0) {
  125. return false;
  126. }
  127. } else if (op.sw > op.w) {
  128. op.oldsx = op.sx;
  129. op.sx = op.sx - d;
  130. setTimeout(function () {
  131. methods.moveX($this, true);
  132. $this = null;
  133. });
  134. return false;
  135. }
  136. });
  137. // 监听鼠标移动
  138. $vertical.find('.lr-scroll-vertical-block').on('mousedown', function (e) {
  139. $move.show();
  140. var $this = $(this).parent().parent();
  141. var op = $this[0].op;
  142. op.isYMousedown = true;
  143. op.yMousedown = e.pageY;
  144. $this.addClass('lr-scroll-active');
  145. $this = null;
  146. });
  147. $horizontal.find('.lr-scroll-horizontal-block').on('mousedown', function (e) {
  148. $move.show();
  149. var $this = $(this).parent().parent();
  150. var op = $this[0].op;
  151. op.isXMousedown = true;
  152. op.xMousedown = e.pageX;
  153. $this.addClass('lr-scroll-active');
  154. $this = null;
  155. });
  156. top.$(document).on('mousemove', { $obj: $this }, function (e) {
  157. var op = e.data.$obj[0].op;
  158. if (op.isYMousedown) {
  159. var $select = e.data.$obj.find('.lr-select-focus');
  160. if ($select.length > 0) {
  161. var selectId = "learun_select_option_" + $select.attr('id');
  162. $('#' + selectId).slideUp(150);
  163. $select.removeClass('lr-select-focus');
  164. }
  165. var y = e.pageY;
  166. var _yd = y - op.yMousedown;
  167. op.yMousedown = y;
  168. op.oldsy = op.sy;
  169. op.blockY = op.blockY + _yd;
  170. if ((op.blockY + op.yh) > op.h) {
  171. op.blockY = op.h - op.yh;
  172. }
  173. if (op.blockY < 0) {
  174. op.blockY = 0;
  175. }
  176. methods.getY(op);
  177. methods.moveY(e.data.$obj, true);
  178. }
  179. else if (op.isXMousedown) {
  180. var $select = e.data.$obj.find('.lr-select-focus');
  181. if ($select.length > 0) {
  182. var selectId = "learun_select_option_" + $select.attr('id');
  183. $('#' + selectId).slideUp(150);
  184. $select.removeClass('lr-select-focus');
  185. }
  186. var x = e.pageX;
  187. var _xd = x - op.xMousedown;
  188. op.xMousedown = x;
  189. op.oldsx = op.sx;
  190. op.blockX = op.blockX + _xd;
  191. if ((op.blockX + op.xw) > op.w) {
  192. op.blockX = op.w - op.xw;
  193. }
  194. if (op.blockX < 0) {
  195. op.blockX = 0;
  196. }
  197. methods.getX(op);
  198. methods.moveX(e.data.$obj);
  199. }
  200. }).on('mouseup', { $obj: $this }, function (e) {
  201. e.data.$obj[0].op.isYMousedown = false;
  202. e.data.$obj[0].op.isXMousedown = false;
  203. $move.hide();
  204. e.data.$obj.removeClass('lr-scroll-active');
  205. });
  206. },
  207. update: function ($this) { // 更新滚动条
  208. methods.updateY($this);
  209. methods.updateX($this);
  210. },
  211. updateY: function ($this) {
  212. var op = $this[0].op;
  213. var $scroll = $this.find('#' + op.id + '_box');
  214. var $vertical = $this.find('#' + op.id + '_vertical');
  215. if (op.sh > op.h) { // 出现纵向滚动条
  216. // 更新显示区域位置
  217. if ((op.sh - op.sy) < op.h) {
  218. var _sy = 0;
  219. op.sy = op.sh - op.h;
  220. if (op.sy < 0) {
  221. op.sy = 0;
  222. } else {
  223. _sy = 0 - op.sy;
  224. }
  225. $scroll.css('top', _sy + 'px');
  226. }
  227. // 更新滚动条高度
  228. var scrollH = parseInt(op.h * op.h / op.sh);
  229. scrollH = (scrollH < 30 ? 30 : scrollH);
  230. op.yh = scrollH;
  231. // 更新滚动条位置
  232. var _y = parseInt(op.sy * (op.h - scrollH) / (op.sh - op.h));
  233. if ((_y + scrollH) > op.h) {
  234. _y = op.h - scrollH;
  235. }
  236. if (_y < 0) {
  237. _y = 0;
  238. }
  239. op.blockY = _y;
  240. // 设置滚动块大小和位置
  241. $vertical.css({
  242. 'top': _y + 'px',
  243. 'height': scrollH + 'px'
  244. });
  245. } else {
  246. op.blockY = 0;
  247. op.sy = 0;
  248. $scroll.css('top', '0px');
  249. $vertical.css({
  250. 'top': '0px',
  251. 'height': '0px'
  252. });
  253. }
  254. op.callback && op.callback(op.sx, op.sy);
  255. $scroll = null;
  256. $vertical = null;
  257. },
  258. updateX: function ($this) {
  259. var op = $this[0].op;
  260. var $scroll = $this.find('#' + op.id + '_box');
  261. var $horizontal = $this.find('#' + op.id + '_horizontal');
  262. if (op.sw > op.w) {
  263. // 更新显示区域位置
  264. if ((op.sw - op.sx) < op.w) {
  265. var _sx = 0;
  266. op.sx = op.sw - op.w;
  267. if (op.sx < 0) {
  268. op.sx = 0;
  269. } else {
  270. _sx = 0 - op.sx;
  271. }
  272. $scroll.css('left', _sx + 'px');
  273. }
  274. // 更新滚动条高度
  275. var scrollW = parseInt(op.w * op.w / op.sw);
  276. scrollW = (scrollW < 30 ? 30 : scrollW);
  277. op.xw = scrollW;
  278. // 更新滚动条位置
  279. var _x = parseInt(op.sx * (op.w - scrollW) / (op.sw - op.w));
  280. if ((_x + scrollW) > op.w) {
  281. _x = op.w - scrollW;
  282. }
  283. if (_x < 0) {
  284. _x = 0;
  285. }
  286. op.blockX = _x;
  287. // 设置滚动块大小和位置
  288. $horizontal.css({
  289. 'left': _x + 'px',
  290. 'width': scrollW + 'px'
  291. });
  292. } else {
  293. op.sx = 0;
  294. op.blockX = 0;
  295. $scroll.css('left', '0px');
  296. $horizontal.css({
  297. 'left': '0px',
  298. 'width': '0px'
  299. });
  300. }
  301. op.callback && op.callback(op.sx, op.sy);
  302. $scroll = null;
  303. $horizontal = null;
  304. },
  305. moveY: function ($this, isMousewheel, isCallBack) {
  306. var op = $this[0].op;
  307. var $scroll = $this.find('#' + op.id + '_box');
  308. var $vertical = $this.find('#' + op.id + '_vertical');
  309. // 更新显示区域位置
  310. var _sy = 0;
  311. if (op.sy < 0) {
  312. op.sy = 0;
  313. } else if (op.sy + op.h > op.sh) {
  314. op.sy = op.sh - op.h;
  315. _sy = 0 - op.sy;
  316. } else {
  317. _sy = 0 - op.sy;
  318. }
  319. if (isMousewheel) {
  320. var _y = methods.getBlockY(op);
  321. if (_y == 0 && op.sy != 0) {
  322. op.sy = 0;
  323. _sy = 0;
  324. }
  325. op.blockY = _y;
  326. // 设置滚动块位置
  327. //var d = Math.abs(op.sy - op.oldsy) * 100 / 4;
  328. $scroll.css({
  329. 'top': _sy + 'px'
  330. });
  331. $vertical.css({
  332. 'top': _y + 'px'
  333. });
  334. } else {
  335. $scroll.css({
  336. 'top': _sy + 'px'
  337. });
  338. $vertical.css({
  339. 'top': op.blockY + 'px'
  340. });
  341. }
  342. if (isCallBack) {
  343. op.callback && op.callback(op.sx, op.sy);
  344. }
  345. $scroll = null;
  346. $vertical = null;
  347. },
  348. moveX: function ($this, isMousewheel) {
  349. var op = $this[0].op;
  350. var $scroll = $this.find('#' + op.id + '_box');
  351. var $horizontal = $this.find('#' + op.id + '_horizontal');
  352. // 更新显示区域位置
  353. var _sx = 0;
  354. if (op.sx < 0) {
  355. op.sx = 0;
  356. } else if (op.sx + op.w > op.sw) {
  357. op.sx = op.sw - op.w;
  358. _sx = 0 - op.sx;
  359. } else {
  360. _sx = 0 - op.sx;
  361. }
  362. if (isMousewheel) {
  363. // 更新滑块的位置
  364. var _x = methods.getBlockX(op);
  365. if (_x == 0 && op.sx != 0) {
  366. op.sx = 0;
  367. _sx = 0;
  368. }
  369. op.blockX = _x;
  370. // 设置滚动块位置
  371. //var d = Math.abs(op.sx - op.oldsx) * 100 / 4;
  372. $scroll.css({
  373. 'left': _sx + 'px'
  374. });
  375. $horizontal.css({
  376. 'left': _x + 'px'
  377. });
  378. } else {
  379. $scroll.css({
  380. 'left': _sx + 'px'
  381. });
  382. $horizontal.css({
  383. 'left': op.blockX + 'px'
  384. });
  385. }
  386. op.callback && op.callback(op.sx, op.sy);
  387. $scroll = null;
  388. $horizontal = null;
  389. },
  390. getBlockY: function (op) {
  391. var _y = parseFloat(op.sy * (op.h - op.yh) / (op.sh - op.h));
  392. if ((_y + op.yh) > op.h) {
  393. _y = op.h - op.yh;
  394. }
  395. if (_y < 0) {
  396. _y = 0;
  397. }
  398. return _y;
  399. },
  400. getY: function (op) {
  401. op.sy = parseInt(op.blockY * (op.sh - op.h) / (op.h - op.yh));
  402. if ((op.sy + op.h) > op.sh) {
  403. op.sy = op.sh - op.h;
  404. }
  405. if (op.sy < 0) {
  406. op.sy = 0;
  407. }
  408. },
  409. getBlockX: function (op) {
  410. var _x = parseFloat(op.sx * (op.w - op.xw) / (op.sw - op.w));
  411. if ((_x + op.xw) > op.w) {
  412. _x = op.w - op.xw;
  413. }
  414. if (_x < 0) {
  415. _x = 0;
  416. }
  417. return _x;
  418. },
  419. getX: function (op) {
  420. op.sx = parseInt(op.blockX * (op.sw - op.w) / (op.w - op.xw));
  421. if ((op.sx + op.w) > op.sw) {
  422. op.sx = op.sw - op.w;
  423. }
  424. if (op.sx < 0) {
  425. op.sx = 0;
  426. }
  427. },
  428. };
  429. $.fn.lrscroll = function (callback) {
  430. $(this).each(function () {
  431. var $this = $(this);
  432. methods.init($this, callback);
  433. });
  434. }
  435. $.fn.lrscrollSet = function (name, data, isCallBack) {
  436. var $this = $(this);
  437. if (!$this[0] || !$this[0].op) {
  438. return;
  439. }
  440. switch (name) {
  441. case 'moveRight':
  442. setTimeout(function () {
  443. var op = $this[0].op;
  444. op.oldsx = op.sx;
  445. op.sx = op.sw - op.w;
  446. methods.moveX($this, true);
  447. $this = null;
  448. }, 250);
  449. break;
  450. case 'moveBottom':
  451. setTimeout(function () {
  452. var op = $this[0].op;
  453. op.oldsy = op.sy;
  454. op.sy = op.sh - op.h;
  455. methods.moveY($this, true, isCallBack);
  456. $this = null;
  457. }, 250);
  458. break;
  459. case 'moveY':
  460. setTimeout(function () {
  461. var op = $this[0].op;
  462. op.oldsy = op.sy;
  463. op.sy = data;
  464. methods.moveY($this, true, isCallBack);
  465. $this = null;
  466. });
  467. break;
  468. case 'moveX':
  469. break;
  470. }
  471. }
  472. })(window.jQuery, top.learun, window);