• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1(function() {
2  var SOURCES = window.TEXT_VARIABLES.sources;
3  window.Lazyload.js(SOURCES.jquery, function() {
4    function affix(options) {
5      var $root = this, $window = $(window), $scrollTarget, $scroll,
6        offsetBottom = 0, scrollTarget = window, scroll = window.document, disabled = false, isOverallScroller = true,
7        rootTop, rootLeft, rootHeight, scrollBottom, rootBottomTop,
8        hasInit = false, curState;
9
10      function setOptions(options) {
11        var _options = options || {};
12        _options.offsetBottom && (offsetBottom = _options.offsetBottom);
13        _options.scrollTarget && (scrollTarget = _options.scrollTarget);
14        _options.scroll && (scroll = _options.scroll);
15        _options.disabled !== undefined && (disabled = _options.disabled);
16        $scrollTarget = $(scrollTarget);
17        isOverallScroller = window.isOverallScroller($scrollTarget[0]);
18        $scroll = $(scroll);
19      }
20      function preCalc() {
21        top();
22        rootHeight = $root.outerHeight();
23        rootTop = $root.offset().top + (isOverallScroller ? 0 :  $scrollTarget.scrollTop());
24        rootLeft = $root.offset().left;
25      }
26      function calc(needPreCalc) {
27        needPreCalc && preCalc();
28        scrollBottom = $scroll.outerHeight() - offsetBottom - rootHeight;
29        rootBottomTop = scrollBottom - rootTop;
30      }
31      function top() {
32        if (curState !== 'top') {
33          $root.removeClass('fixed').css({
34            left: 0,
35            top: 0
36          });
37          curState = 'top';
38        }
39      }
40      function fixed() {
41        if (curState !== 'fixed') {
42          $root.addClass('fixed').css({
43            left: rootLeft + 'px',
44            top: 0
45          });
46          curState = 'fixed';
47        }
48      }
49      function bottom() {
50        if (curState !== 'bottom') {
51          $root.removeClass('fixed').css({
52            left: 0,
53            top: rootBottomTop + 'px'
54          });
55          curState = 'bottom';
56        }
57      }
58      function setState() {
59        var scrollTop = $scrollTarget.scrollTop();
60        if (scrollTop >= rootTop && scrollTop <= scrollBottom) {
61          fixed();
62        } else if (scrollTop < rootTop) {
63          top();
64        } else {
65          bottom();
66        }
67      }
68      function init() {
69        if(!hasInit) {
70          var interval, timeout;
71          calc(true); setState();
72          // run calc every 100 millisecond
73          interval = setInterval(function() {
74            calc();
75          }, 100);
76          timeout = setTimeout(function() {
77            clearInterval(interval);
78          }, 45000);
79          window.pageLoad.then(function() {
80            setTimeout(function() {
81              clearInterval(interval);
82              clearTimeout(timeout);
83            }, 3000);
84          });
85          $scrollTarget.on('scroll', function() {
86            disabled || setState();
87          });
88          $window.on('resize', function() {
89            disabled || (calc(true), setState());
90          });
91          hasInit = true;
92        }
93      }
94
95      setOptions(options);
96      if (!disabled) {
97        init();
98      }
99      $window.on('resize', window.throttle(function() {
100        init();
101      }, 200));
102      return {
103        setOptions: setOptions,
104        refresh: function() {
105          calc(true, { animation: false }); setState();
106        }
107      };
108    }
109    $.fn.affix = affix;
110  });
111})();