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.

scroll - 副本.js 15 KiB

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