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.
 
 
 
 
 
 

453 lines
15 KiB

  1. /*
  2. * 版 本 Learun-ADMS V7.0.0 数字化智慧校园(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. var h = $this.height();
  67. var w = $this.width();
  68. if (h != op.h) {
  69. op.h = h;
  70. methods.updateY($this);
  71. }
  72. if (w != op.w) {
  73. op.w = w;
  74. methods.updateX($this);
  75. }
  76. $this = null;
  77. });
  78. $scroll.resize(function () {
  79. var $this = $(this);
  80. var $scrollWrap = $this.parent();
  81. var op = $scrollWrap[0].op;
  82. var sh = $this.innerHeight();
  83. var sw = $this.innerWidth();
  84. if (op != null && op != undefined) {
  85. if (sh != op.sh) {
  86. op.sh = sh;
  87. methods.updateY($scrollWrap);
  88. }
  89. if (sw != op.sw) {
  90. op.sw = sw;
  91. methods.updateX($scrollWrap);
  92. }
  93. }
  94. $this = null;
  95. $scrollWrap = null;
  96. });
  97. // 监听鼠标滚动
  98. $this.mousewheel(function (event, delta, deltaX, deltaY) {
  99. var $this = $(this);
  100. var op = $this[0].op;
  101. var d = delta * 18;
  102. if (op.sh > op.h) {
  103. op.oldsy = op.sy;
  104. op.sy = op.sy - d;
  105. methods.moveY($this, true);
  106. $this = null;
  107. return false;
  108. } else if (op.sw > op.w) {
  109. op.oldsx = op.sx;
  110. op.sx = op.sx - d;
  111. methods.moveX($this, true);
  112. $this = null;
  113. return false;
  114. }
  115. });
  116. // 监听鼠标移动
  117. $vertical.find('.lr-scroll-vertical-block').on('mousedown', function (e) {
  118. $move.show();
  119. var $this = $(this).parent().parent();
  120. var op = $this[0].op;
  121. op.isYMousedown = true;
  122. op.yMousedown = e.pageY;
  123. $this.addClass('lr-scroll-active');
  124. $this = null;
  125. });
  126. $horizontal.find('.lr-scroll-horizontal-block').on('mousedown', function (e) {
  127. $move.show();
  128. var $this = $(this).parent().parent();
  129. var op = $this[0].op;
  130. op.isXMousedown = true;
  131. op.xMousedown = e.pageX;
  132. $this.addClass('lr-scroll-active');
  133. $this = null;
  134. });
  135. top.$(document).on('mousemove', { $obj: $this }, function (e) {
  136. var op = e.data.$obj[0].op;
  137. if (op.isYMousedown) {
  138. var y = e.pageY;
  139. var _yd = y - op.yMousedown;
  140. op.yMousedown = y;
  141. op.oldsy = op.sy;
  142. op.blockY = op.blockY + _yd;
  143. if ((op.blockY + op.yh) > op.h) {
  144. op.blockY = op.h - op.yh;
  145. }
  146. if (op.blockY < 0) {
  147. op.blockY = 0;
  148. }
  149. methods.getY(op);
  150. methods.moveY(e.data.$obj);
  151. }
  152. else if (op.isXMousedown) {
  153. var op = e.data.$obj[0].op;
  154. var x = e.pageX;
  155. var _xd = x - op.xMousedown;
  156. op.xMousedown = x;
  157. op.oldsx = op.sx;
  158. op.blockX = op.blockX + _xd;
  159. if ((op.blockX + op.xw) > op.w) {
  160. op.blockX = op.w - op.xw;
  161. }
  162. if (op.blockX < 0) {
  163. op.blockX = 0;
  164. }
  165. methods.getX(op);
  166. methods.moveX(e.data.$obj);
  167. }
  168. }).on('mouseup', { $obj: $this }, function (e) {
  169. e.data.$obj[0].op.isYMousedown = false;
  170. e.data.$obj[0].op.isXMousedown = false;
  171. $move.hide();
  172. e.data.$obj.removeClass('lr-scroll-active');
  173. });
  174. },
  175. update: function ($this) { // 更新滚动条
  176. methods.updateY($this);
  177. methods.updateX($this);
  178. },
  179. updateY: function ($this) {
  180. var op = $this[0].op;
  181. var $scroll = $this.find('#' + op.id + '_box');
  182. var $vertical = $this.find('#' + op.id + '_vertical');
  183. if (op.sh > op.h) { // 出现纵向滚动条
  184. // 更新显示区域位置
  185. if ((op.sh - op.sy) < op.h) {
  186. var _sy = 0;
  187. op.sy = op.sh - op.h;
  188. if (op.sy < 0) {
  189. op.sy = 0;
  190. } else {
  191. _sy = 0 - op.sy;
  192. }
  193. $scroll.css('top', _sy + 'px');
  194. }
  195. // 更新滚动条高度
  196. var scrollH = parseInt(op.h * op.h / op.sh);
  197. scrollH = (scrollH < 30 ? 30 : scrollH);
  198. op.yh = scrollH;
  199. // 更新滚动条位置
  200. var _y = parseInt(op.sy * (op.h - scrollH) / (op.sh - op.h));
  201. if ((_y + scrollH) > op.h) {
  202. _y = op.h - scrollH;
  203. }
  204. if (_y < 0) {
  205. _y = 0;
  206. }
  207. op.blockY = _y;
  208. // 设置滚动块大小和位置
  209. $vertical.css({
  210. 'top': _y + 'px',
  211. 'height': scrollH + 'px'
  212. });
  213. } else {
  214. op.blockY = 0;
  215. op.sy = 0;
  216. $scroll.css('top', '0px');
  217. $vertical.css({
  218. 'top': '0px',
  219. 'height': '0px'
  220. });
  221. }
  222. op.callback && op.callback(op.sx, op.sy);
  223. $scroll = null;
  224. $vertical = null;
  225. },
  226. updateX: function ($this) {
  227. var op = $this[0].op;
  228. var $scroll = $this.find('#' + op.id + '_box');
  229. var $horizontal = $this.find('#' + op.id + '_horizontal');
  230. if (op.sw > op.w) {
  231. // 更新显示区域位置
  232. if ((op.sw - op.sx) < op.w) {
  233. var _sx = 0;
  234. op.sx = op.sw - op.w;
  235. if (op.sx < 0) {
  236. op.sx = 0;
  237. } else {
  238. _sx = 0 - op.sx;
  239. }
  240. $scroll.css('left', _sx + 'px');
  241. }
  242. // 更新滚动条高度
  243. var scrollW = parseInt(op.w * op.w / op.sw);
  244. scrollW = (scrollW < 30 ? 30 : scrollW);
  245. op.xw = scrollW;
  246. // 更新滚动条位置
  247. var _x = parseInt(op.sx * (op.w - scrollW) / (op.sw - op.w));
  248. if ((_x + scrollW) > op.w) {
  249. _x = op.w - scrollW;
  250. }
  251. if (_x < 0) {
  252. _x = 0;
  253. }
  254. op.blockX = _x;
  255. // 设置滚动块大小和位置
  256. $horizontal.css({
  257. 'left': _x + 'px',
  258. 'width': scrollW + 'px'
  259. });
  260. } else {
  261. op.sx = 0;
  262. op.blockX = 0;
  263. $scroll.css('left', '0px');
  264. $horizontal.css({
  265. 'left': '0px',
  266. 'width': '0px'
  267. });
  268. }
  269. op.callback && op.callback(op.sx, op.sy);
  270. $scroll = null;
  271. $horizontal = null;
  272. },
  273. moveY: function ($this, isMousewheel) {
  274. var op = $this[0].op;
  275. var $scroll = $this.find('#' + op.id + '_box');
  276. var $vertical = $this.find('#' + op.id + '_vertical');
  277. // 更新显示区域位置
  278. var _sy = 0;
  279. if (op.sy < 0) {
  280. op.sy = 0;
  281. } else if (op.sy + op.h > op.sh) {
  282. op.sy = op.sh - op.h;
  283. _sy = 0 - op.sy;
  284. } else {
  285. _sy = 0 - op.sy;
  286. }
  287. if (isMousewheel) {
  288. var _y = methods.getBlockY(op);
  289. if (_y == 0 && op.sy != 0) {
  290. op.sy = 0;
  291. _sy = 0;
  292. }
  293. op.blockY = _y;
  294. // 设置滚动块位置
  295. //var d = Math.abs(op.sy - op.oldsy) * 100 / 4;
  296. $scroll.css({
  297. 'top': _sy + 'px'
  298. });
  299. $vertical.css({
  300. 'top': _y + 'px'
  301. });
  302. } else {
  303. $scroll.css({
  304. 'top': _sy + 'px'
  305. });
  306. $vertical.css({
  307. 'top': op.blockY + 'px'
  308. });
  309. }
  310. op.callback && op.callback(op.sx, op.sy);
  311. $scroll = null;
  312. $vertical = null;
  313. },
  314. moveX: function ($this, isMousewheel) {
  315. var op = $this[0].op;
  316. var $scroll = $this.find('#' + op.id + '_box');
  317. var $horizontal = $this.find('#' + op.id + '_horizontal');
  318. // 更新显示区域位置
  319. var _sx = 0;
  320. if (op.sx < 0) {
  321. op.sx = 0;
  322. } else if (op.sx + op.w > op.sw) {
  323. op.sx = op.sw - op.w;
  324. _sx = 0 - op.sx;
  325. } else {
  326. _sx = 0 - op.sx;
  327. }
  328. if (isMousewheel) {
  329. // 更新滑块的位置
  330. var _x = methods.getBlockX(op);
  331. if (_x == 0 && op.sx != 0) {
  332. op.sx = 0;
  333. _sx = 0;
  334. }
  335. op.blockX = _x;
  336. // 设置滚动块位置
  337. //var d = Math.abs(op.sx - op.oldsx) * 100 / 4;
  338. $scroll.css({
  339. 'left': _sx + 'px'
  340. });
  341. $horizontal.css({
  342. 'left': _x + 'px'
  343. });
  344. } else {
  345. $scroll.css({
  346. 'left': _sx + 'px'
  347. });
  348. $horizontal.css({
  349. 'left': op.blockX + 'px'
  350. });
  351. }
  352. op.callback && op.callback(op.sx, op.sy);
  353. $scroll = null;
  354. $horizontal = null;
  355. },
  356. getBlockY: function (op) {
  357. var _y = parseFloat(op.sy * (op.h - op.yh) / (op.sh - op.h));
  358. if ((_y + op.yh) > op.h) {
  359. _y = op.h - op.yh;
  360. }
  361. if (_y < 0) {
  362. _y = 0;
  363. }
  364. return _y;
  365. },
  366. getY: function (op) {
  367. op.sy = parseInt(op.blockY * (op.sh - op.h) / (op.h - op.yh));
  368. if ((op.sy + op.h) > op.sh) {
  369. op.sy = op.sh - op.h;
  370. }
  371. if (op.sy < 0) {
  372. op.sy = 0;
  373. }
  374. },
  375. getBlockX: function (op) {
  376. var _x = parseFloat(op.sx * (op.w - op.xw) / (op.sw - op.w));
  377. if ((_x + op.xw) > op.w) {
  378. _x = op.w - op.xw;
  379. }
  380. if (_x < 0) {
  381. _x = 0;
  382. }
  383. return _x;
  384. },
  385. getX: function (op) {
  386. op.sx = parseInt(op.blockX * (op.sw - op.w) / (op.w - op.xw));
  387. if ((op.sx + op.w) > op.sw) {
  388. op.sx = op.sw - op.w;
  389. }
  390. if (op.sx < 0) {
  391. op.sx = 0;
  392. }
  393. },
  394. };
  395. $.fn.lrscroll = function (callback) {
  396. $(this).each(function () {
  397. var $this = $(this);
  398. methods.init($this, callback);
  399. });
  400. }
  401. $.fn.lrscrollSet = function (name, data) {
  402. switch (name) {
  403. case 'moveRight':
  404. var $this = $(this);
  405. setTimeout(function () {
  406. var op = $this[0].op;
  407. op.oldsx = op.sx;
  408. op.sx = op.sw - op.w;
  409. methods.moveX($this, true);
  410. $this = null;
  411. }, 250);
  412. break;
  413. case 'moveBottom':
  414. var $this = $(this);
  415. setTimeout(function () {
  416. var op = $this[0].op;
  417. op.oldsy = op.sx;
  418. op.sy = op.sh - op.h;
  419. methods.moveY($this, true);
  420. $this = null;
  421. }, 250);
  422. break;
  423. }
  424. }
  425. })(window.jQuery, top.learun, window);