123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110 |
- (function() {
- function recalculate(_container, _bottomThreshold) {
- let $container = $(_container);
- let width = $container.width(), scrollWidth = _container.scrollWidth, scrollLeft = _container.scrollLeft,
- stagScrollbarUI = $container.find('>.stag-scrollbar-horiz-container').first();
- if(Math.ceil(scrollWidth) > Math.ceil(width)) {
- $container.attr({
- 'stag-h-scrollbar': 1,
- 'stag-h-scroll-left': scrollLeft,
- 'stag-h-scroll-width': scrollWidth,
- 'stag-h-width': width,
- });
- if($container.offset().top + $container.outerHeight() > $(window).height() + $(window).scrollTop() - _bottomThreshold) {
- stagScrollbarUI.css({
- display: 'block',
- left: ($container.offset().left + 4) + 'px',
- width: ($container.width() - 4) + 'px',
- bottom: _bottomThreshold + 'px',
- });
- let trackerWidth = Math.floor((width/scrollWidth)*stagScrollbarUI.width()) + 'px', // ((width/scrollwidth) * 100) %,
- trackerLeft = Math.ceil((scrollLeft/scrollWidth)*stagScrollbarUI.width()) + 'px'; // // ((scrollLeft/scrollwidth) * 100) %,
- stagScrollbarUI.find('.stag-scrollbar-horiz-tracker').css({
- marginLeft: trackerLeft,
- width: trackerWidth,
- });
- }
- else {
- stagScrollbarUI.css({
- display: 'none'
- });
- }
- }
- else {
- $container.removeAttr('stag-h-scrollbar');
- }
- }
- window.initStagScrollbar = function(_bottomThreshold = 0) {
- $('.stag-scrollbar:not([stag-scrollbar-initialized])').each(function() {
- let scrollbarID = 'stag-scrollbar-' + (Math.random() * 10000);
- let stagScrollbarUI = $('<div/>').addClass('stag-scrollbar-horiz-container');
- stagScrollbarUI.append($('<div/>').addClass('stag-scrollbar-horiz-tracker'))
- $(this).append(stagScrollbarUI);
- recalculate(this, _bottomThreshold);
- let self = this;
- $(this)
- .off('mouseenter.stag-scrollbar')
- .on('mouseenter.stag-scrollbar', function() {
- if(!scrolling) recalculate(this, _bottomThreshold);
- })
- .off('scroll.stag-scrollbar')
- .on('scroll.stag-scrollbar', function() {
- if(!scrolling) recalculate(this, _bottomThreshold);
- })
- .off('resize.stag-scrollbar')
- .on('resize.stag-scrollbar', function() {
- recalculate(this, _bottomThreshold);
- });
- $(window).on('resize', function() {
- recalculate(self, _bottomThreshold);
- });
- $(window).on('scroll', function() {
- recalculate(self, _bottomThreshold);
- });
- $(document).on('v-splitter-change', function() {
- recalculate(self, _bottomThreshold);
- });
- let origX = 0, origLeft = 0, scrolling = false, tracker = $(this).find('.stag-scrollbar-horiz-tracker');
- tracker
- .off('mousedown.' + scrollbarID)
- .on('mousedown.' + scrollbarID, function (_e) {
- origLeft = tracker.offset().left - stagScrollbarUI.offset().left;
- origX = _e.screenX;
- scrolling = true;
- $('body').addClass('stag-scrollbar-scrolling');
- return false;
- });
- $(document)
- .off('mousemove.' + scrollbarID)
- .on('mousemove.' + scrollbarID, function (_e) {
- if (!scrolling) return;
- let delta = _e.screenX - origX, finalML = origLeft + delta;
- if(finalML < 0) {
- finalML = 0;
- }
- else if(finalML + tracker.width() > stagScrollbarUI.width()) {
- finalML = stagScrollbarUI.width() - tracker.width();
- }
- tracker.css({
- marginLeft: finalML + 'px'
- });
- let percentScrolled = delta/(stagScrollbarUI.width() - tracker.width());
- $(self).scrollLeft((self.scrollWidth - $(self).width()) * percentScrolled);
- })
- .off('mouseup.' + scrollbarID)
- .on('mouseup.' + scrollbarID, function (_e) {
- if (!scrolling) return;
- scrolling = false;
- $('body').removeClass('stag-scrollbar-scrolling');
- recalculate(self, _bottomThreshold);
- });
- $(this).attr('stag-scrollbar-id', scrollbarID).attr('stag-scrollbar-initialized', 1);
- });
- }
- // addMCInitializer('stag-scrollbar', initStagScrollbar);
- }).call(window);
|