PageSourceSearch

https://apiserv2.childrenhospital.go.th/assets/f48bc8ba/js/sticky.js?9

js childrenhospital.go.th collected 2026-10-04 21:51:43 UTC 11,064 bytes, 306 lines download raw bytes

vendor: 6,919 bytes, lines 1-179
1// Sticky Plugin v1.0.4 for jQuery
2// =============
3// Author: Anthony Garand
4// Improvements by German M. Bravo (Kronuz) and Ruud Kamphuis (ruudk)
5// Improvements by Leonardo C. Daronco (daronco)
6// Created: 02/14/2011
7// Date: 07/20/2015
8// Website: http://stickyjs.com/
9// Description: Makes an element on the page stick on the screen as you scroll
10//              It will only set the 'top' and 'position' of your element, you
11//              might need to adjust the width in some cases.
12
13(function (factory) {
14    if (typeof define === 'function' && define.amd) {
15        // AMD. Register as an anonymous module.
16        define(['jquery'], factory);
17    } else if (typeof module === 'object' && module.exports) {
18        // Node/CommonJS
19        module.exports = factory(require('jquery'));
20    } else {
21        // Browser globals
22        factory(jQuery);
23    }
24}(function ($) {
25    var slice = Array.prototype.slice; // save ref to original slice()
26    var splice = Array.prototype.splice; // save ref to original slice()
27
28  var defaults = {
29      topSpacing: 0,
30      bottomSpacing: 0,
31      className: 'is-sticky',
32      wrapperClassName: 'sticky-wrapper',
33      center: false,
34      getWidthFrom: '',
35      widthFromWrapper: true, // works only when .getWidthFrom is empty
36      responsiveWidth: false,
37      zIndex: 'inherit',
38      mobileDisable: false,
39      mobileLimitHeight: 800,
40    },
41    $window = $(window),
42    $document = $(document),
43    sticked = [],
44    windowHeight = $window.height(),
45    scroller = function() {
46      var scrollTop = $window.scrollTop(),
47        documentHeight = $document.height(),
48        dwh = documentHeight - windowHeight,
49        extra = (scrollTop > dwh) ? dwh - scrollTop : 0;
50
51      for (var i = 0, l = sticked.length; i < l; i++) {
52        var s = sticked[i],
53          elementTop = s.stickyWrapper.offset().top,
54          etse = elementTop - s.topSpacing - extra;
55
56        //update height in case of dynamic content
57        s.stickyWrapper.css('height', s.stickyElement.outerHeight());
58
59        if (scrollTop <= etse) {
60          if (s.currentTop !== null) {
61            s.stickyElement
62              .css({
63                'width': '',
64                'position': '',
65                'top': '',
66                'z-index': ''
67              });
68            s.stickyElement.parent().removeClass(s.className);
69            s.stickyElement.trigger('sticky-end', [s]);
70            s.currentTop = null;
71          }
72        }
73        else {
74          var newTop = documentHeight - s.stickyElement.outerHeight()
75            - s.topSpacing - s.bottomSpacing - scrollTop - extra;
76          if (newTop < 0) {
77            newTop = newTop + s.topSpacing;
78          } else {
79            newTop = s.topSpacing;
80          }
81          
82          if(($window.width() >= 768 && $window.height() >= s.mobileLimitHeight) || !s.mobileDisable){//$window.width() >= 768 &&
83              if (s.currentTop !== newTop) {
84                var newWidth;
85                if (s.getWidthFrom) {
86                    padding =  s.stickyElement.innerWidth() - s.stickyElement.width();
87                    newWidth = $(s.getWidthFrom).width() - padding || null;
88                } else if (s.widthFromWrapper) {
89                    newWidth = s.stickyWrapper.width();
90                }
91                if (newWidth == null) {
92                    newWidth = s.stickyElement.width();
93                }
94                s.stickyElement
95                  .css('width', newWidth)
96                  .css('position', 'fixed')
97                  .css('top', newTop);
98                  
99                  s.stickyElement.css('cssText', s.stickyElement.attr('style')+'z-index: ' + s.zIndex + '!important;');
100                  
101                s.stickyElement.parent().addClass(s.className);
102
103                if (s.currentTop === null) {
104                  s.stickyElement.trigger('sticky-start', [s]);
105                } else {
106                  // sticky is started but it have to be repositioned
107                  s.stickyElement.trigger('sticky-update', [s]);
108                }
109
110                if (s.currentTop === s.topSpacing && s.currentTop > newTop || s.currentTop === null && newTop < s.topSpacing) {
111                  // just reached bottom || just started to stick but bottom is already reached
112                  s.stickyElement.trigger('sticky-bottom-reached', [s]);
113                } else if(s.currentTop !== null && newTop === s.topSpacing && s.currentTop < newTop) {
114                  // sticky is started && sticked at topSpacing && overflowing from top just finished
115                  s.stickyElement.trigger('sticky-bottom-unreached', [s]);
116                }
117
118                s.currentTop = newTop;
119              }
120
121              // Check if sticky has reached end of container and stop sticking
122              var stickyWrapperContainer = s.stickyWrapper.parent();
123              var unstick = (s.stickyElement.offset().top + s.stickyElement.outerHeight() >= stickyWrapperContainer.offset().top + stickyWrapperContainer.outerHeight()) && (s.stickyElement.offset().top <= s.topSpacing);
124
125              if( unstick ) {
126                s.stickyElement
127                  .css('position', 'absolute')
128                  .css('top', '')
129                  .css('bottom', 0)
130                  .css('z-index', '');
131              } else {
132                s.stickyElement
133                  .css('position', 'fixed')
134                  .css('top', newTop)
135                  .css('bottom', '');
136                  s.stickyElement.css('cssText', s.stickyElement.attr('style')+'z-index: ' + s.zIndex + '!important;');
137              }
138          } else {
139              s.stickyElement
140                .css({
141                  'width': '',
142                  'position': '',
143                  'top': '',
144                  'z-index': ''
145                });
146              s.stickyElement.parent().removeClass(s.className);
147              s.stickyElement.trigger('sticky-end', [s]);
148              s.currentTop = null;
149          }
150          
151        }
152      }
153    },
154    resizer = function() {
155      windowHeight = $window.height();
156
157      for (var i = 0, l = sticked.length; i < l; i++) {
158        var s = sticked[i];
159        var newWidth = null;
160        if (s.getWidthFrom) {
161            if (s.responsiveWidth) {
162                newWidth = $(s.getWidthFrom).width();
163            }
164        } else if(s.widthFromWrapper) {
165            newWidth = s.stickyWrapper.width();
166        }
167        if (newWidth != null) {
168            s.stickyElement.css('width', newWidth);
169        }
170      }
171    },
172    methods = {
173      init: function(options) {
174        return this.each(function() {
175          var o = $.extend({}, defaults, options);
176          var stickyElement = $(this);
177
178          var stickyId = stickyElement.attr('id');
179          var wrapperId = stickyId ? stickyId + '-' + defaults.wrapperClassName : defaults.wrapperClassName;
180          var wrapper = $('<div></div>')
181            .attr('id', wrapperId)
182            .addClass(o.wrapperClassName);
183
184          stickyElement.wrapAll(function() {
185            if ($(this).parent("#" + wrapperId).length == 0) {
186                    return wrapper;
187            }
188});
189
190          var stickyWrapper = stickyElement.parent();
191
192          if (o.center) {
193            stickyWrapper.css({width:stickyElement.outerWidth(),marginLeft:"auto",marginRight:"auto"});
194          }
195
196          if (stickyElement.css("float") === "right") {
197            stickyElement.css({"float":"none"}).parent().css({"float":"right"});
198          }
199
200          o.stickyElement = stickyElement;
201          o.stickyWrapper = stickyWrapper;
202          o.currentTop    = null;
203
204          sticked.push(o);
205
206          methods.setWrapperHeight(this);
207          methods.setupChangeListeners(this);
208        });
209      },
210
211      setWrapperHeight: function(stickyElement) {
212        var element = $(stickyElement);
213        var stickyWrapper = element.parent();
214        if (stickyWrapper) {
215          stickyWrapper.css('height', element.outerHeight());
216        }
217      },
218
219      setupChangeListeners: function(stickyElement) {
220        if (window.MutationObserver) {
221          var mutationObserver = new window.MutationObserver(function(mutations) {
222            if (mutations[0].addedNodes.length || mutations[0].removedNodes.length) {
223              methods.setWrapperHeight(stickyElement);
224            }
225          });
226          mutationObserver.observe(stickyElement, {subtree: true, childList: true});
227        } else {
228          if (window.addEventListener) {
229            stickyElement.addEventListener('DOMNodeInserted', function() {
230              methods.setWrapperHeight(stickyElement);
231            }, false);
232            stickyElement.addEventListener('DOMNodeRemoved', function() {
233              methods.setWrapperHeight(stickyElement);
234            }, false);
235          } else if (window.attachEvent) {
236            stickyElement.attachEvent('onDOMNodeInserted', function() {
237              methods.setWrapperHeight(stickyElement);
238            });
239            stickyElement.attachEvent('onDOMNodeRemoved', function() {
240              methods.setWrapperHeight(stickyElement);
241            });
242          }
243        }
244      },
245      update: scroller,
246      unstick: function(options) {
247        return this.each(function() {
248          var that = this;
249          var unstickyElement = $(that);
250
251          var removeIdx = -1;
252          var i = sticked.length;
253          while (i-- > 0) {
254            if (sticked[i].stickyElement.get(0) === that) {
255                splice.call(sticked,i,1);
256                removeIdx = i;
257            }
258          }
259          if(removeIdx !== -1) {
260            unstickyElement.unwrap();
261            unstickyElement
262              .css({
263                'width': '',
264                'position': '',
265                'top': '',
266                'float': '',
267                'z-index': ''
268              })
269            ;
270          }
271        });
272      }
273    };
274
275  // should be more efficient than using $window.scroll(scroller) and $window.resize(resizer):
276  if (window.addEventListener) {
277    window.addEventListener('scroll', scroller, false);
278    window.addEventListener('resize', resizer, false);
279  } else if (window.attachEvent) {
280    window.attachEvent('onscroll', scroller);
281    window.attachEvent('onresize', resizer);
282  }
283
284  $.fn.sticky = function(method) {
285    if (methods[method]) {
286      return methods[method].apply(this, slice.call(arguments, 1));
287    } else if (typeof method === 'object' || !method ) {
288      return methods.init.apply( this, arguments );
289    } else {
290      $.error('Method ' + method + ' does not exist on jQuery.sticky');
291    }
292  };
293
294  $.fn.unstick = function(method) {
295    if (methods[method]) {
296      return methods[method].apply(this, slice.call(arguments, 1));
297    } else if (typeof method === 'object' || !method ) {
298      return methods.unstick.apply( this, arguments );
299    } else {
300      $.error('Method ' + method + ' does not exist on jQuery.sticky');
301    }
302  };
303  $(function() {
304    setTimeout(scroller, 0);
305  });
306}));

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.