stag-scrollbar.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. (function() {
  2. function recalculate(_container, _bottomThreshold) {
  3. let $container = $(_container);
  4. let width = $container.width(), scrollWidth = _container.scrollWidth, scrollLeft = _container.scrollLeft,
  5. stagScrollbarUI = $container.find('>.stag-scrollbar-horiz-container').first();
  6. if(Math.ceil(scrollWidth) > Math.ceil(width)) {
  7. $container.attr({
  8. 'stag-h-scrollbar': 1,
  9. 'stag-h-scroll-left': scrollLeft,
  10. 'stag-h-scroll-width': scrollWidth,
  11. 'stag-h-width': width,
  12. });
  13. if($container.offset().top + $container.outerHeight() > $(window).height() + $(window).scrollTop() - _bottomThreshold) {
  14. stagScrollbarUI.css({
  15. display: 'block',
  16. left: ($container.offset().left + 4) + 'px',
  17. width: ($container.width() - 4) + 'px',
  18. bottom: _bottomThreshold + 'px',
  19. });
  20. let trackerWidth = Math.floor((width/scrollWidth)*stagScrollbarUI.width()) + 'px', // ((width/scrollwidth) * 100) %,
  21. trackerLeft = Math.ceil((scrollLeft/scrollWidth)*stagScrollbarUI.width()) + 'px'; // // ((scrollLeft/scrollwidth) * 100) %,
  22. stagScrollbarUI.find('.stag-scrollbar-horiz-tracker').css({
  23. marginLeft: trackerLeft,
  24. width: trackerWidth,
  25. });
  26. }
  27. else {
  28. stagScrollbarUI.css({
  29. display: 'none'
  30. });
  31. }
  32. }
  33. else {
  34. $container.removeAttr('stag-h-scrollbar');
  35. }
  36. }
  37. window.initStagScrollbar = function(_bottomThreshold = 0) {
  38. $('.stag-scrollbar:not([stag-scrollbar-initialized])').each(function() {
  39. let scrollbarID = 'stag-scrollbar-' + (Math.random() * 10000);
  40. let stagScrollbarUI = $('<div/>').addClass('stag-scrollbar-horiz-container');
  41. stagScrollbarUI.append($('<div/>').addClass('stag-scrollbar-horiz-tracker'))
  42. $(this).append(stagScrollbarUI);
  43. recalculate(this, _bottomThreshold);
  44. let self = this;
  45. $(this)
  46. .off('mouseenter.stag-scrollbar')
  47. .on('mouseenter.stag-scrollbar', function() {
  48. if(!scrolling) recalculate(this, _bottomThreshold);
  49. })
  50. .off('scroll.stag-scrollbar')
  51. .on('scroll.stag-scrollbar', function() {
  52. if(!scrolling) recalculate(this, _bottomThreshold);
  53. })
  54. .off('resize.stag-scrollbar')
  55. .on('resize.stag-scrollbar', function() {
  56. recalculate(this, _bottomThreshold);
  57. });
  58. $(window).on('resize', function() {
  59. recalculate(self, _bottomThreshold);
  60. });
  61. $(window).on('scroll', function() {
  62. recalculate(self, _bottomThreshold);
  63. });
  64. $(document).on('v-splitter-change', function() {
  65. recalculate(self, _bottomThreshold);
  66. });
  67. let origX = 0, origLeft = 0, scrolling = false, tracker = $(this).find('.stag-scrollbar-horiz-tracker');
  68. tracker
  69. .off('mousedown.' + scrollbarID)
  70. .on('mousedown.' + scrollbarID, function (_e) {
  71. origLeft = tracker.offset().left - stagScrollbarUI.offset().left;
  72. origX = _e.screenX;
  73. scrolling = true;
  74. $('body').addClass('stag-scrollbar-scrolling');
  75. return false;
  76. });
  77. $(document)
  78. .off('mousemove.' + scrollbarID)
  79. .on('mousemove.' + scrollbarID, function (_e) {
  80. if (!scrolling) return;
  81. let delta = _e.screenX - origX, finalML = origLeft + delta;
  82. if(finalML < 0) {
  83. finalML = 0;
  84. }
  85. else if(finalML + tracker.width() > stagScrollbarUI.width()) {
  86. finalML = stagScrollbarUI.width() - tracker.width();
  87. }
  88. tracker.css({
  89. marginLeft: finalML + 'px'
  90. });
  91. let percentScrolled = delta/(stagScrollbarUI.width() - tracker.width());
  92. $(self).scrollLeft((self.scrollWidth - $(self).width()) * percentScrolled);
  93. })
  94. .off('mouseup.' + scrollbarID)
  95. .on('mouseup.' + scrollbarID, function (_e) {
  96. if (!scrolling) return;
  97. scrolling = false;
  98. $('body').removeClass('stag-scrollbar-scrolling');
  99. recalculate(self, _bottomThreshold);
  100. });
  101. $(this).attr('stag-scrollbar-id', scrollbarID).attr('stag-scrollbar-initialized', 1);
  102. });
  103. }
  104. // addMCInitializer('stag-scrollbar', initStagScrollbar);
  105. }).call(window);