Nolesh

Adapted PerfectScrollbar for BODY tag.

Mar 19th, 2014
612
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*! Copyright (c) 2011 Brandon Aaron (http://brandonaaron.net)
  2.  * Licensed under the MIT License (LICENSE.txt).
  3.  *
  4.  * Thanks to: http://adomas.org/javascript-mouse-wheel/ for some pointers.
  5.  * Thanks to: Mathias Bank(http://www.mathias-bank.de) for a scope bug fix.
  6.  * Thanks to: Seamus Leahy for adding deltaX and deltaY
  7.  *
  8.  * Version: 3.0.6
  9.  *
  10.  * Requires: 1.2.2+
  11.  */
  12.  
  13. (function($) {
  14.  
  15. var types = ['DOMMouseScroll', 'mousewheel'];
  16.  
  17. if ($.event.fixHooks) {
  18.     for ( var i=types.length; i; ) {
  19.         $.event.fixHooks[ types[--i] ] = $.event.mouseHooks;
  20.     }
  21. }
  22.  
  23. $.event.special.mousewheel = {
  24.     setup: function() {
  25.         if ( this.addEventListener ) {
  26.             for ( var i=types.length; i; ) {
  27.                 this.addEventListener( types[--i], handler, false );
  28.             }
  29.         } else {
  30.             this.onmousewheel = handler;
  31.         }
  32.     },
  33.    
  34.     teardown: function() {
  35.         if ( this.removeEventListener ) {
  36.             for ( var i=types.length; i; ) {
  37.                 this.removeEventListener( types[--i], handler, false );
  38.             }
  39.         } else {
  40.             this.onmousewheel = null;
  41.         }
  42.     }
  43. };
  44.  
  45. $.fn.extend({
  46.     mousewheel: function(fn) {
  47.         return fn ? this.bind("mousewheel", fn) : this.trigger("mousewheel");
  48.     },
  49.    
  50.     unmousewheel: function(fn) {
  51.         return this.unbind("mousewheel", fn);
  52.     }
  53. });
  54.  
  55.  
  56. function handler(event) {
  57.     var orgEvent = event || window.event, args = [].slice.call( arguments, 1 ), delta = 0, returnValue = true, deltaX = 0, deltaY = 0;
  58.     event = $.event.fix(orgEvent);
  59.     event.type = "mousewheel";
  60.    
  61.     // Old school scrollwheel delta
  62.     if ( orgEvent.wheelDelta ) { delta = orgEvent.wheelDelta/120; }
  63.     if ( orgEvent.detail     ) { delta = -orgEvent.detail/3; }
  64.    
  65.     // New school multidimensional scroll (touchpads) deltas
  66.     deltaY = delta;
  67.    
  68.     // Gecko
  69.     if ( orgEvent.axis !== undefined && orgEvent.axis === orgEvent.HORIZONTAL_AXIS ) {
  70.         deltaY = 0;
  71.         deltaX = -1*delta;
  72.     }
  73.    
  74.     // Webkit
  75.     if ( orgEvent.wheelDeltaY !== undefined ) { deltaY = orgEvent.wheelDeltaY/120; }
  76.     if ( orgEvent.wheelDeltaX !== undefined ) { deltaX = -1*orgEvent.wheelDeltaX/120; }
  77.    
  78.     // Add event and delta to the front of the arguments
  79.     args.unshift(event, delta, deltaX, deltaY);
  80.    
  81.     return ($.event.dispatch || $.event.handle).apply(this, args);
  82. }
  83.  
  84. })(jQuery);
  85.  
  86. /************************************************************************************************************************************************/
  87.  
  88. /* Copyright (c) 2012 HyeonJe Jun (http://github.com/noraesae)
  89.  * Licensed under the MIT License
  90.  * Updated by Nolesh Feb 2014. Now it can be used for BODY and HTML tags.
  91.  * How to use:
  92.  * $('html').css({"overflow":"hidden"});
  93.  * $('html, body').perfectScrollbar({wheelSpeed:50});
  94.  * $('body').perfectScrollbar('update'); //to update
  95.  */
  96. 'use strict';
  97. (function (factory) {
  98.   if (typeof define === 'function' && define.amd) {
  99.     // AMD. Register as an anonymous module.
  100.     define(['jquery'], factory);
  101.   } else {
  102.     // Browser globals
  103.     factory(jQuery);
  104.   }
  105. }(function ($) {
  106.  
  107.   // The default settings for the plugin
  108.   var defaultSettings = {
  109.     wheelSpeed: 10,
  110.     wheelPropagation: false,
  111.     minScrollbarLength: null,
  112.     useBothWheelAxes: false,
  113.     useKeyboard: true,
  114.     suppressScrollX: false,
  115.     suppressScrollY: false,
  116.     scrollXMarginOffset: 0,
  117.     scrollYMarginOffset: 0
  118.   };
  119.  
  120.   $.fn.perfectScrollbar = function (suppliedSettings, option) {
  121.  
  122.     return this.each(function () {
  123.       // Use the default settings
  124.       var settings = $.extend(true, {}, defaultSettings),
  125.           $this = $(this);
  126.  
  127.       if (typeof suppliedSettings === "object") {
  128.         // But over-ride any supplied
  129.         $.extend(true, settings, suppliedSettings);
  130.       } else {
  131.         // If no settings were supplied, then the first param must be the option
  132.         option = suppliedSettings;
  133.       }
  134.  
  135.       // Catch options
  136.  
  137.       if (option === 'update') {
  138.         if ($this.data('perfect-scrollbar-update')) {
  139.           $this.data('perfect-scrollbar-update')();
  140.         }
  141.         return $this;
  142.       }
  143.       else if (option === 'destroy') {
  144.         if ($this.data('perfect-scrollbar-destroy')) {
  145.           $this.data('perfect-scrollbar-destroy')();
  146.         }
  147.         return $this;
  148.       }
  149.  
  150.       if ($this.data('perfect-scrollbar')) {
  151.         // if there's already perfect-scrollbar
  152.         return $this.data('perfect-scrollbar');
  153.       }
  154.  
  155.  
  156.       // Or generate new perfectScrollbar
  157.  
  158.       // Set class to the container
  159.       $this.addClass('ps-container');
  160.  
  161.       var $scrollbarXRail = $("<div class='ps-scrollbar-x-rail'></div>").appendTo($this),
  162.           $scrollbarYRail = $("<div class='ps-scrollbar-y-rail'></div>").appendTo($this),
  163.           $scrollbarX = $("<div class='ps-scrollbar-x'></div>").appendTo($scrollbarXRail),
  164.           $scrollbarY = $("<div class='ps-scrollbar-y'></div>").appendTo($scrollbarYRail),
  165.           scrollbarXActive,
  166.           scrollbarYActive,
  167.           containerWidth,
  168.           containerHeight,
  169.           contentWidth,
  170.           contentHeight,
  171.           scrollbarXWidth,
  172.           scrollbarXLeft,
  173.           scrollbarXBottom = parseInt($scrollbarXRail.css('bottom'), 10),
  174.           scrollbarYHeight,
  175.           scrollbarYTop,
  176.           scrollbarYRight = parseInt($scrollbarYRail.css('right'), 10),      
  177.           isSmoothScrolling;
  178.            
  179.       var getSettingsAdjustedThumbSize = function (thumbSize) {
  180.         if (settings.minScrollbarLength) {
  181.           thumbSize = Math.max(thumbSize, settings.minScrollbarLength);
  182.         }
  183.         return thumbSize;
  184.       };
  185.  
  186.       var updateScrollbarCss = function () {
  187.         $scrollbarXRail.css({position: isSmoothScrolling ? 'fixed' : 'absolute', left: isSmoothScrolling ? 0 : $this.scrollLeft(), bottom: scrollbarXBottom - $this.scrollTop(),
  188.         width: containerWidth, display: settings.suppressScrollX ? "none": "inherit"});
  189.         $scrollbarYRail.css({position: isSmoothScrolling ? 'fixed' : 'absolute', top: isSmoothScrolling ? 0 : $this.scrollTop(), right: scrollbarYRight - $this.scrollLeft(),
  190.         height: containerHeight, display: settings.suppressScrollY ? "none": "inherit"});
  191.         $scrollbarX.css({left: scrollbarXLeft, width: scrollbarXWidth});
  192.         $scrollbarY.css({top: scrollbarYTop, height: scrollbarYHeight});
  193.       };
  194.            
  195.       var updateBarSizeAndPosition = function () {
  196.        
  197.         containerWidth = window.innerWidth;
  198.         containerHeight = window.innerHeight;
  199.         contentWidth = $(document).innerWidth();
  200.         contentHeight = $(document).innerHeight();
  201.                
  202.         if (!settings.suppressScrollX && containerWidth + settings.scrollXMarginOffset < contentWidth) {
  203.           scrollbarXActive = true;
  204.           scrollbarXWidth = getSettingsAdjustedThumbSize(parseInt(containerWidth * containerWidth / contentWidth, 10));
  205.           scrollbarXLeft = parseInt($this.scrollLeft() * (containerWidth - scrollbarXWidth) / (contentWidth - containerWidth), 10);
  206.         }
  207.         else {
  208.           scrollbarXActive = false;
  209.           scrollbarXWidth = 0;
  210.           scrollbarXLeft = 0;
  211.           $this.scrollLeft(0);
  212.         }
  213.  
  214.         if (!settings.suppressScrollY && containerHeight + settings.scrollYMarginOffset < contentHeight) {
  215.           scrollbarYActive = true;
  216.           scrollbarYHeight = getSettingsAdjustedThumbSize(parseInt(containerHeight * containerHeight / contentHeight, 10));
  217.           scrollbarYTop = parseInt($this.scrollTop() * (containerHeight - scrollbarYHeight) / (contentHeight - containerHeight), 10);
  218.         }
  219.         else {
  220.           scrollbarYActive = false;
  221.           scrollbarYHeight = 0;
  222.           scrollbarYTop = 0;
  223.           $this.scrollTop(0);
  224.         }
  225.  
  226.         if (scrollbarYTop >= containerHeight - scrollbarYHeight) {
  227.           scrollbarYTop = containerHeight - scrollbarYHeight;
  228.         }
  229.         if (scrollbarXLeft >= containerWidth - scrollbarXWidth) {
  230.           scrollbarXLeft = containerWidth - scrollbarXWidth;
  231.         }
  232.                
  233.         updateScrollbarCss();
  234.       };
  235.      
  236.       var bindMouseScrollXHandler = function () {
  237.         var currentLeft,
  238.             currentPageX;
  239.  
  240.         $scrollbarX.bind('mousedown.perfect-scrollbar', function (e) {
  241.           currentPageX = e.pageX-$scrollbarXRail.offset().left;
  242.           currentLeft = $scrollbarX.position().left;         
  243.           $scrollbarXRail.addClass('in-scrolling');
  244.           e.stopPropagation();
  245.           e.preventDefault();
  246.         });
  247.        
  248.         $(document).bind('mousemove.perfect-scrollbar', function (e) {
  249.           if ($scrollbarXRail.hasClass('in-scrolling')) {
  250.                    
  251.             var halfOfScrollbarLength = parseInt(scrollbarXWidth / 2, 10),
  252.             positionLeft = e.pageX - $scrollbarXRail.offset().left - currentPageX+currentLeft,
  253.            
  254.             maxPositionLeft = containerWidth - scrollbarXWidth,
  255.             positionRatio = positionLeft / maxPositionLeft;
  256.            
  257.             if (positionRatio < 0) {
  258.                 positionRatio = 0;
  259.             } else if (positionRatio > 1) {
  260.                 positionRatio = 1;
  261.             }
  262.            
  263.            
  264.             $this.scrollLeft((contentWidth - containerWidth) * positionRatio);
  265.             updateBarSizeAndPosition();
  266.            
  267.             e.stopPropagation();
  268.             e.preventDefault();
  269.           }
  270.         });
  271.  
  272.         $(document).bind('mouseup.perfect-scrollbar', function (e) {
  273.           if ($scrollbarXRail.hasClass('in-scrolling')) {
  274.             $scrollbarXRail.removeClass('in-scrolling');
  275.           }
  276.         });
  277.  
  278.         currentLeft =
  279.         currentPageX = null;
  280.       };
  281.  
  282.       var bindMouseScrollYHandler = function () {
  283.         var currentTop,
  284.             currentPageY;
  285.  
  286.         $scrollbarY.bind('mousedown.perfect-scrollbar', function (e) {
  287.           currentPageY = e.pageY-$scrollbarYRail.offset().top;
  288.           currentTop = $scrollbarY.position().top;
  289.           $scrollbarYRail.addClass('in-scrolling');
  290.           e.stopPropagation();
  291.           e.preventDefault();
  292.         });
  293.  
  294.         $(document).bind('mousemove.perfect-scrollbar', function (e) {
  295.           if ($scrollbarYRail.hasClass('in-scrolling')) {
  296.            
  297.             var halfOfScrollbarLength = parseInt(scrollbarYHeight / 2, 10),
  298.             positionTop = e.pageY - $scrollbarYRail.offset().top - currentPageY+currentTop,
  299.            
  300.             maxPositionTop = containerHeight - scrollbarYHeight,
  301.             positionRatio = positionTop / maxPositionTop;
  302.            
  303.             if (positionRatio < 0) {
  304.                 positionRatio = 0;
  305.             } else if (positionRatio > 1) {
  306.                 positionRatio = 1;
  307.             }
  308.            
  309.            
  310.             $this.scrollTop((contentHeight - containerHeight) * positionRatio);
  311.             updateBarSizeAndPosition();
  312.                      
  313.             e.stopPropagation();
  314.             e.preventDefault();
  315.           }
  316.         });
  317.  
  318.         $(document).bind('mouseup.perfect-scrollbar', function (e) {
  319.           if ($scrollbarYRail.hasClass('in-scrolling')) {
  320.             $scrollbarYRail.removeClass('in-scrolling');
  321.           }
  322.         });
  323.  
  324.         currentTop =
  325.         currentPageY = null;
  326.       };
  327.  
  328.       // bind handlers
  329.       var bindMouseWheelHandler = function () {
  330.         var shouldPreventDefault = function (deltaX, deltaY) {
  331.           var scrollTop = $this.scrollTop();
  332.           if (scrollTop === 0 && deltaY > 0 && deltaX === 0) {
  333.             return !settings.wheelPropagation;
  334.           }
  335.           else if (scrollTop >= contentHeight - containerHeight && deltaY < 0 && deltaX === 0) {
  336.             return !settings.wheelPropagation;
  337.           }
  338.  
  339.           var scrollLeft = $this.scrollLeft();
  340.           if (scrollLeft === 0 && deltaX < 0 && deltaY === 0) {
  341.             return !settings.wheelPropagation;
  342.           }
  343.           else if (scrollLeft >= contentWidth - containerWidth && deltaX > 0 && deltaY === 0) {
  344.             return !settings.wheelPropagation;
  345.           }
  346.           return true;
  347.         };
  348.  
  349.         var shouldPrevent = false;
  350.         $this.bind('mousewheel.perfect-scrollbar', function (e, delta, deltaX, deltaY) {
  351.           if (!settings.useBothWheelAxes) {
  352.             // deltaX will only be used for horizontal scrolling and deltaY will
  353.             // only be used for vertical scrolling - this is the default
  354.             $this.scrollTop($this.scrollTop() - (deltaY * settings.wheelSpeed));
  355.             $this.scrollLeft($this.scrollLeft() + (deltaX * settings.wheelSpeed));
  356.           } else if (scrollbarYActive && !scrollbarXActive) {
  357.             // only vertical scrollbar is active and useBothWheelAxes option is
  358.             // active, so let's scroll vertical bar using both mouse wheel axes
  359.             if (deltaY) {
  360.               $this.scrollTop($this.scrollTop() - (deltaY * settings.wheelSpeed));
  361.             } else {
  362.               $this.scrollTop($this.scrollTop() + (deltaX * settings.wheelSpeed));
  363.             }
  364.           } else if (scrollbarXActive && !scrollbarYActive) {
  365.             // useBothWheelAxes and only horizontal bar is active, so use both
  366.             // wheel axes for horizontal bar
  367.             if (deltaX) {
  368.               $this.scrollLeft($this.scrollLeft() + (deltaX * settings.wheelSpeed));
  369.             } else {
  370.               $this.scrollLeft($this.scrollLeft() - (deltaY * settings.wheelSpeed));
  371.             }
  372.           }
  373.  
  374.           // update bar position
  375.           updateBarSizeAndPosition();
  376.  
  377.           shouldPrevent = shouldPreventDefault(deltaX, deltaY);
  378.           if (shouldPrevent) {
  379.             e.preventDefault();
  380.           }
  381.         });
  382.  
  383.         // fix Firefox scroll problem
  384.         $this.bind('MozMousePixelScroll.perfect-scrollbar', function (e) {
  385.           if (shouldPrevent) {
  386.             e.preventDefault();
  387.           }
  388.         });
  389.       };
  390.  
  391.       var bindKeyboardHandler = function () {
  392.         var shouldPreventDefault = function (deltaX, deltaY) {
  393.           var scrollTop = $this.scrollTop();
  394.           if (scrollTop === 0 && deltaY > 0 && deltaX === 0) {
  395.              return false;
  396.           }
  397.           else if (scrollTop >= contentHeight - containerHeight && deltaY < 0 && deltaX === 0) {
  398.              return false;
  399.           }
  400.  
  401.           var scrollLeft = $this.scrollLeft();
  402.           if (scrollLeft === 0 && deltaX < 0 && deltaY === 0) {
  403.              return false;
  404.           }
  405.           else if (scrollLeft >= contentWidth - containerWidth && deltaX > 0 && deltaY === 0) {
  406.             return false;
  407.           }
  408.           return true;
  409.         };
  410.  
  411.         var hovered = false;
  412.         $this.bind('mouseenter.perfect-scrollbar', function (e) {
  413.           hovered = true;
  414.         });
  415.         $this.bind('mouseleave.perfect-scrollbar', function (e) {
  416.           hovered = false;
  417.         });
  418.  
  419.         var shouldPrevent = false;
  420.         $(document).bind('keydown.perfect-scrollbar', function (e) {
  421.           if (!hovered) {
  422.             return;
  423.           }
  424.  
  425.           var deltaX = 0,
  426.               deltaY = 0;
  427.  
  428.           switch (e.which) {
  429.           case 37: // left
  430.             deltaX = -3;
  431.             break;
  432.           case 38: // up
  433.             deltaY = 3;
  434.             break;
  435.           case 39: // right
  436.             deltaX = 3;
  437.             break;
  438.           case 40: // down
  439.             deltaY = -3;
  440.             break;
  441.           default:
  442.             return;
  443.           }
  444.           $this.scrollTop($this.scrollTop() - (deltaY * settings.wheelSpeed));
  445.           $this.scrollLeft($this.scrollLeft() + (deltaX * settings.wheelSpeed));
  446.  
  447.           // update bar position
  448.           updateBarSizeAndPosition();
  449.  
  450.           shouldPrevent = shouldPreventDefault(deltaX, deltaY);
  451.           if (shouldPrevent) {
  452.             e.preventDefault();
  453.           }
  454.         });
  455.       };
  456.  
  457.       var bindRailClickHandler = function () {
  458.         var stopPropagation = function (e) { e.stopPropagation(); };
  459.  
  460.         $scrollbarY.bind('click.perfect-scrollbar', stopPropagation);
  461.         $scrollbarYRail.bind('click.perfect-scrollbar', function (e) {                                  console.log('bindRailClickHandler');
  462.           var halfOfScrollbarLength = parseInt(scrollbarYHeight / 2, 10),
  463.               positionTop = e.pageY - $scrollbarYRail.offset().top - halfOfScrollbarLength,
  464.               maxPositionTop = containerHeight - scrollbarYHeight,
  465.               positionRatio = positionTop / maxPositionTop;
  466.  
  467.           if (positionRatio < 0) {
  468.             positionRatio = 0;
  469.           } else if (positionRatio > 1) {
  470.             positionRatio = 1;
  471.           }
  472.          
  473.             isSmoothScrolling=true;
  474.             $this.stop().animate({scrollTop: (contentHeight - containerHeight) * positionRatio}, {
  475.                 duration: 1000,
  476.                 step: function() {         
  477.                     updateBarSizeAndPosition();
  478.                 },
  479.                 complete: function(){                  
  480.                     isSmoothScrolling=false;
  481.                     updateBarSizeAndPosition();
  482.                 }
  483.             });        
  484.         });
  485.  
  486.         $scrollbarX.bind('click.perfect-scrollbar', stopPropagation);
  487.         $scrollbarXRail.bind('click.perfect-scrollbar', function (e) {
  488.           var halfOfScrollbarLength = parseInt(scrollbarXWidth / 2, 10),
  489.               positionLeft = e.pageX - $scrollbarXRail.offset().left - halfOfScrollbarLength,
  490.               maxPositionLeft = containerWidth - scrollbarXWidth,
  491.               positionRatio = positionLeft / maxPositionLeft;
  492.  
  493.           if (positionRatio < 0) {
  494.             positionRatio = 0;
  495.           } else if (positionRatio > 1) {
  496.             positionRatio = 1;
  497.           }
  498.  
  499.           isSmoothScrolling=true;
  500.           $this.stop().animate({scrollLeft: (contentWidth - containerWidth) * positionRatio}, {
  501.                 duration: 1000,
  502.                 step: function() {         
  503.                     updateBarSizeAndPosition();
  504.                 },
  505.                 complete: function(){                  
  506.                     isSmoothScrolling=false;
  507.                     updateBarSizeAndPosition();
  508.                 }
  509.           });
  510.                    
  511.         });
  512.       };
  513.  
  514.       // bind mobile touch handler
  515.       var bindMobileTouchHandler = function () {
  516.         var applyTouchMove = function (differenceX, differenceY) {
  517.           $this.scrollTop($this.scrollTop() - differenceY);
  518.           $this.scrollLeft($this.scrollLeft() - differenceX);
  519.  
  520.           // update bar position
  521.           updateBarSizeAndPosition();
  522.         };
  523.  
  524.         var startCoords = {},
  525.             startTime = 0,
  526.             speed = {},
  527.             breakingProcess = null,
  528.             inGlobalTouch = false;
  529.  
  530.         $(window).bind("touchstart.perfect-scrollbar", function (e) {
  531.           inGlobalTouch = true;
  532.         });
  533.         $(window).bind("touchend.perfect-scrollbar", function (e) {
  534.           inGlobalTouch = false;
  535.         });
  536.  
  537.         $this.bind("touchstart.perfect-scrollbar", function (e) {
  538.           var touch = e.originalEvent.targetTouches[0];
  539.  
  540.           startCoords.pageX = touch.pageX;
  541.           startCoords.pageY = touch.pageY;
  542.  
  543.           startTime = (new Date()).getTime();
  544.  
  545.           if (breakingProcess !== null) {
  546.             clearInterval(breakingProcess);
  547.           }
  548.  
  549.           e.stopPropagation();
  550.         });
  551.         $this.bind("touchmove.perfect-scrollbar", function (e) {
  552.           if (!inGlobalTouch && e.originalEvent.targetTouches.length === 1) {
  553.             var touch = e.originalEvent.targetTouches[0];
  554.  
  555.             var currentCoords = {};
  556.             currentCoords.pageX = touch.pageX;
  557.             currentCoords.pageY = touch.pageY;
  558.  
  559.             var differenceX = currentCoords.pageX - startCoords.pageX,
  560.               differenceY = currentCoords.pageY - startCoords.pageY;
  561.  
  562.             applyTouchMove(differenceX, differenceY);
  563.             startCoords = currentCoords;
  564.  
  565.             var currentTime = (new Date()).getTime();
  566.             speed.x = differenceX / (currentTime - startTime);
  567.             speed.y = differenceY / (currentTime - startTime);
  568.             startTime = currentTime;
  569.  
  570.             e.preventDefault();
  571.           }
  572.         });
  573.         $this.bind("touchend.perfect-scrollbar", function (e) {
  574.           clearInterval(breakingProcess);
  575.           breakingProcess = setInterval(function () {
  576.             if (Math.abs(speed.x) < 0.01 && Math.abs(speed.y) < 0.01) {
  577.               clearInterval(breakingProcess);
  578.               return;
  579.             }
  580.  
  581.             applyTouchMove(speed.x * 30, speed.y * 30);
  582.  
  583.             speed.x *= 0.8;
  584.             speed.y *= 0.8;
  585.           }, 10);
  586.         });
  587.       };
  588.  
  589.       var destroy = function () {
  590.         $this.unbind('.perfect-scrollbar');
  591.         $(window).unbind('.perfect-scrollbar');
  592.         $(document).unbind('.perfect-scrollbar');
  593.         $this.data('perfect-scrollbar', null);
  594.         $this.data('perfect-scrollbar-update', null);
  595.         $this.data('perfect-scrollbar-destroy', null);
  596.         $scrollbarX.remove();
  597.         $scrollbarY.remove();
  598.         $scrollbarXRail.remove();
  599.         $scrollbarYRail.remove();
  600.  
  601.         // clean all variables
  602.         $scrollbarX =
  603.         $scrollbarY =
  604.         containerWidth =
  605.         containerHeight =
  606.         contentWidth =
  607.         contentHeight =
  608.         scrollbarXWidth =
  609.         scrollbarXLeft =
  610.         scrollbarXBottom =
  611.         scrollbarYHeight =
  612.         scrollbarYTop =
  613.         scrollbarYRight = null;
  614.       };
  615.  
  616.       var ieSupport = function (version) {
  617.         $this.addClass('ie').addClass('ie' + version);
  618.  
  619.         var bindHoverHandlers = function () {
  620.           var mouseenter = function () {
  621.             $(this).addClass('hover');
  622.           };
  623.           var mouseleave = function () {
  624.             $(this).removeClass('hover');
  625.           };
  626.           $this.bind('mouseenter.perfect-scrollbar', mouseenter).bind('mouseleave.perfect-scrollbar', mouseleave);
  627.           $scrollbarXRail.bind('mouseenter.perfect-scrollbar', mouseenter).bind('mouseleave.perfect-scrollbar', mouseleave);
  628.           $scrollbarYRail.bind('mouseenter.perfect-scrollbar', mouseenter).bind('mouseleave.perfect-scrollbar', mouseleave);
  629.           $scrollbarX.bind('mouseenter.perfect-scrollbar', mouseenter).bind('mouseleave.perfect-scrollbar', mouseleave);
  630.           $scrollbarY.bind('mouseenter.perfect-scrollbar', mouseenter).bind('mouseleave.perfect-scrollbar', mouseleave);
  631.         };
  632.  
  633.         var fixIe6ScrollbarPosition = function () {
  634.           updateScrollbarCss = function () {
  635.             $scrollbarX.css({left: scrollbarXLeft + $this.scrollLeft(), bottom: scrollbarXBottom, width: scrollbarXWidth});
  636.             $scrollbarY.css({top: scrollbarYTop + $this.scrollTop(), right: scrollbarYRight, height: scrollbarYHeight});
  637.             $scrollbarX.hide().show();
  638.             $scrollbarY.hide().show();
  639.           };
  640.           updateContentScrollTop = function () {
  641.             var scrollTop = parseInt(scrollbarYTop * contentHeight / containerHeight, 10);
  642.             $this.scrollTop(scrollTop);
  643.             $scrollbarX.css({bottom: scrollbarXBottom});
  644.             $scrollbarX.hide().show();
  645.           };
  646.           updateContentScrollLeft = function () {
  647.             var scrollLeft = parseInt(scrollbarXLeft * contentWidth / containerWidth, 10);
  648.             $this.scrollLeft(scrollLeft);
  649.             $scrollbarY.hide().show();
  650.           };
  651.         };
  652.  
  653.         if (version === 6) {
  654.           bindHoverHandlers();
  655.           fixIe6ScrollbarPosition();
  656.         }
  657.       };
  658.  
  659.       var supportsTouch = (('ontouchstart' in window) || window.DocumentTouch && document instanceof window.DocumentTouch);
  660.  
  661.       var initialize = function () {     
  662.         var ieMatch = navigator.userAgent.toLowerCase().match(/(msie) ([\w.]+)/);
  663.         if (ieMatch && ieMatch[1] === 'msie') {
  664.           // must be executed at first, because 'ieSupport' may addClass to the container
  665.           ieSupport(parseInt(ieMatch[2], 10));
  666.         }
  667.        
  668.         $.browser.chrome = /chrom(e|ium)/.test(navigator.userAgent.toLowerCase());
  669.         if($.browser.chrome){
  670.             $this = $('body');
  671.         }
  672.         else $this = $('body, html');
  673.        
  674.         updateBarSizeAndPosition();
  675.         bindMouseScrollXHandler();
  676.         bindMouseScrollYHandler();
  677.         bindRailClickHandler();
  678.         if (supportsTouch) {
  679.           bindMobileTouchHandler();
  680.         }
  681.         if ($this.mousewheel) {
  682.           bindMouseWheelHandler();
  683.         }
  684.         if (settings.useKeyboard) {
  685.           bindKeyboardHandler();
  686.         }
  687.         $this.data('perfect-scrollbar', $this);
  688.         $this.data('perfect-scrollbar-update', updateBarSizeAndPosition);
  689.         $this.data('perfect-scrollbar-destroy', destroy);
  690.       };
  691.  
  692.       // initialize
  693.       initialize();
  694.  
  695.       return $this;
  696.     });
  697.   };
  698. }));
Advertisement
Add Comment
Please, Sign In to add comment