commonPageController.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. /**
  2. *
  3. * @authors Your Name (you@example.org)
  4. * @date 2017-07-28 18:35:19
  5. * @version $Id$
  6. */
  7. var commonPageInit = function () {
  8. var createScrollBar = function () {
  9. var indicatorEl = $('.scroll-indicator');
  10. var scrollHeight = $('.content-height').height();
  11. var contentWrapHeight = $('.content-wrap').height() - $('.top-wrap').css('margin-top').match(/\d+/)[0];
  12. // 页面滚动时,指示器同步移动
  13. $('.content-wrap').scroll(function () {
  14. var scrollTop = $(this).scrollTop();
  15. scrollHeight = $('.content-height').height();
  16. indicatorEl.css('top', scrollTop * 100/(scrollHeight - contentWrapHeight) + '%');
  17. });
  18. var clientLastY = 0;
  19. var mouseMoveHandler = function (evt) {
  20. var eleTop = indicatorEl.css('top').match(/\d+/)[0];
  21. var distance = clientLastY - evt.clientY;
  22. // 边界判定
  23. if (eleTop >= $('.scroll-bar').height()) {
  24. $('body').off('mousemove', mouseMoveHandler);
  25. indicatorEl.css('top', '100%');
  26. return;
  27. } else if (eleTop < 0) {
  28. $('body').off('mousemove', mouseMoveHandler);
  29. indicatorEl.css('top', '0');
  30. return;
  31. }
  32. indicatorEl.css('top', parseInt(eleTop) - distance + 'px');
  33. // 内容移动距离:内容高度*(指示器距离顶端距离/指示器高度)
  34. $('.content-wrap').scrollTop((scrollHeight - contentWrapHeight) * (indicatorEl.css('top').match(/\d+/)[0] / $('.scroll-bar').height()));
  35. clientLastY = evt.clientY;
  36. }
  37. $('body').on('mousedown', function (evt) {
  38. clientLastY = evt.clientY;
  39. $('body').on('mousemove', mouseMoveHandler);
  40. })
  41. $('body').on('mouseup', function (evt) {
  42. $('body').off('mousemove', mouseMoveHandler)
  43. })
  44. $('.scroll-top').on('click', function () {
  45. $('.content-wrap').scrollTop(0);
  46. indicatorEl.css('top', '0');
  47. })
  48. }
  49. createScrollBar();
  50. }
  51. $(document).ready(function () {
  52. commonPageInit();
  53. TD.imgPreload(function () {
  54. $('.m-loading').fadeOut(300);
  55. })
  56. });