sticky.js 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959
  1. /*!
  2. * # Semantic UI 2.4.2 - Sticky
  3. * http://github.com/semantic-org/semantic-ui/
  4. *
  5. *
  6. * Released under the MIT license
  7. * http://opensource.org/licenses/MIT
  8. *
  9. */
  10. ;(function ($, window, document, undefined) {
  11. 'use strict';
  12. window = (typeof window != 'undefined' && window.Math == Math)
  13. ? window
  14. : (typeof self != 'undefined' && self.Math == Math)
  15. ? self
  16. : Function('return this')()
  17. ;
  18. $.fn.sticky = function(parameters) {
  19. var
  20. $allModules = $(this),
  21. moduleSelector = $allModules.selector || '',
  22. time = new Date().getTime(),
  23. performance = [],
  24. query = arguments[0],
  25. methodInvoked = (typeof query == 'string'),
  26. queryArguments = [].slice.call(arguments, 1),
  27. returnedValue
  28. ;
  29. $allModules
  30. .each(function() {
  31. var
  32. settings = ( $.isPlainObject(parameters) )
  33. ? $.extend(true, {}, $.fn.sticky.settings, parameters)
  34. : $.extend({}, $.fn.sticky.settings),
  35. className = settings.className,
  36. namespace = settings.namespace,
  37. error = settings.error,
  38. eventNamespace = '.' + namespace,
  39. moduleNamespace = 'module-' + namespace,
  40. $module = $(this),
  41. $window = $(window),
  42. $scroll = $(settings.scrollContext),
  43. $container,
  44. $context,
  45. selector = $module.selector || '',
  46. instance = $module.data(moduleNamespace),
  47. requestAnimationFrame = window.requestAnimationFrame
  48. || window.mozRequestAnimationFrame
  49. || window.webkitRequestAnimationFrame
  50. || window.msRequestAnimationFrame
  51. || function(callback) { setTimeout(callback, 0); },
  52. element = this,
  53. documentObserver,
  54. observer,
  55. module
  56. ;
  57. module = {
  58. initialize: function() {
  59. module.determineContainer();
  60. module.determineContext();
  61. module.verbose('Initializing sticky', settings, $container);
  62. module.save.positions();
  63. module.checkErrors();
  64. module.bind.events();
  65. if(settings.observeChanges) {
  66. module.observeChanges();
  67. }
  68. module.instantiate();
  69. },
  70. instantiate: function() {
  71. module.verbose('Storing instance of module', module);
  72. instance = module;
  73. $module
  74. .data(moduleNamespace, module)
  75. ;
  76. },
  77. destroy: function() {
  78. module.verbose('Destroying previous instance');
  79. module.reset();
  80. if(documentObserver) {
  81. documentObserver.disconnect();
  82. }
  83. if(observer) {
  84. observer.disconnect();
  85. }
  86. $window
  87. .off('load' + eventNamespace, module.event.load)
  88. .off('resize' + eventNamespace, module.event.resize)
  89. ;
  90. $scroll
  91. .off('scrollchange' + eventNamespace, module.event.scrollchange)
  92. ;
  93. $module.removeData(moduleNamespace);
  94. },
  95. observeChanges: function() {
  96. if('MutationObserver' in window) {
  97. documentObserver = new MutationObserver(module.event.documentChanged);
  98. observer = new MutationObserver(module.event.changed);
  99. documentObserver.observe(document, {
  100. childList : true,
  101. subtree : true
  102. });
  103. observer.observe(element, {
  104. childList : true,
  105. subtree : true
  106. });
  107. observer.observe($context[0], {
  108. childList : true,
  109. subtree : true
  110. });
  111. module.debug('Setting up mutation observer', observer);
  112. }
  113. },
  114. determineContainer: function() {
  115. if(settings.container) {
  116. $container = $(settings.container);
  117. }
  118. else {
  119. $container = $module.offsetParent();
  120. }
  121. },
  122. determineContext: function() {
  123. if(settings.context) {
  124. $context = $(settings.context);
  125. }
  126. else {
  127. $context = $container;
  128. }
  129. if($context.length === 0) {
  130. module.error(error.invalidContext, settings.context, $module);
  131. return;
  132. }
  133. },
  134. checkErrors: function() {
  135. if( module.is.hidden() ) {
  136. module.error(error.visible, $module);
  137. }
  138. if(module.cache.element.height > module.cache.context.height) {
  139. module.reset();
  140. module.error(error.elementSize, $module);
  141. return;
  142. }
  143. },
  144. bind: {
  145. events: function() {
  146. $window
  147. .on('load' + eventNamespace, module.event.load)
  148. .on('resize' + eventNamespace, module.event.resize)
  149. ;
  150. // pub/sub pattern
  151. $scroll
  152. .off('scroll' + eventNamespace)
  153. .on('scroll' + eventNamespace, module.event.scroll)
  154. .on('scrollchange' + eventNamespace, module.event.scrollchange)
  155. ;
  156. }
  157. },
  158. event: {
  159. changed: function(mutations) {
  160. clearTimeout(module.timer);
  161. module.timer = setTimeout(function() {
  162. module.verbose('DOM tree modified, updating sticky menu', mutations);
  163. module.refresh();
  164. }, 100);
  165. },
  166. documentChanged: function(mutations) {
  167. [].forEach.call(mutations, function(mutation) {
  168. if(mutation.removedNodes) {
  169. [].forEach.call(mutation.removedNodes, function(node) {
  170. if(node == element || $(node).find(element).length > 0) {
  171. module.debug('Element removed from DOM, tearing down events');
  172. module.destroy();
  173. }
  174. });
  175. }
  176. });
  177. },
  178. load: function() {
  179. module.verbose('Page contents finished loading');
  180. requestAnimationFrame(module.refresh);
  181. },
  182. resize: function() {
  183. module.verbose('Window resized');
  184. requestAnimationFrame(module.refresh);
  185. },
  186. scroll: function() {
  187. requestAnimationFrame(function() {
  188. $scroll.triggerHandler('scrollchange' + eventNamespace, $scroll.scrollTop() );
  189. });
  190. },
  191. scrollchange: function(event, scrollPosition) {
  192. module.stick(scrollPosition);
  193. settings.onScroll.call(element);
  194. }
  195. },
  196. refresh: function(hardRefresh) {
  197. module.reset();
  198. if(!settings.context) {
  199. module.determineContext();
  200. }
  201. if(hardRefresh) {
  202. module.determineContainer();
  203. }
  204. module.save.positions();
  205. module.stick();
  206. settings.onReposition.call(element);
  207. },
  208. supports: {
  209. sticky: function() {
  210. var
  211. $element = $('<div/>'),
  212. element = $element[0]
  213. ;
  214. $element.addClass(className.supported);
  215. return($element.css('position').match('sticky'));
  216. }
  217. },
  218. save: {
  219. lastScroll: function(scroll) {
  220. module.lastScroll = scroll;
  221. },
  222. elementScroll: function(scroll) {
  223. module.elementScroll = scroll;
  224. },
  225. positions: function() {
  226. var
  227. scrollContext = {
  228. height : $scroll.height()
  229. },
  230. element = {
  231. margin: {
  232. top : parseInt($module.css('margin-top'), 10),
  233. bottom : parseInt($module.css('margin-bottom'), 10),
  234. },
  235. offset : $module.offset(),
  236. width : $module.outerWidth(),
  237. height : $module.outerHeight()
  238. },
  239. context = {
  240. offset : $context.offset(),
  241. height : $context.outerHeight()
  242. },
  243. container = {
  244. height: $container.outerHeight()
  245. }
  246. ;
  247. if( !module.is.standardScroll() ) {
  248. module.debug('Non-standard scroll. Removing scroll offset from element offset');
  249. scrollContext.top = $scroll.scrollTop();
  250. scrollContext.left = $scroll.scrollLeft();
  251. element.offset.top += scrollContext.top;
  252. context.offset.top += scrollContext.top;
  253. element.offset.left += scrollContext.left;
  254. context.offset.left += scrollContext.left;
  255. }
  256. module.cache = {
  257. fits : ( (element.height + settings.offset) <= scrollContext.height),
  258. sameHeight : (element.height == context.height),
  259. scrollContext : {
  260. height : scrollContext.height
  261. },
  262. element: {
  263. margin : element.margin,
  264. top : element.offset.top - element.margin.top,
  265. left : element.offset.left,
  266. width : element.width,
  267. height : element.height,
  268. bottom : element.offset.top + element.height
  269. },
  270. context: {
  271. top : context.offset.top,
  272. height : context.height,
  273. bottom : context.offset.top + context.height
  274. }
  275. };
  276. module.set.containerSize();
  277. module.stick();
  278. module.debug('Caching element positions', module.cache);
  279. }
  280. },
  281. get: {
  282. direction: function(scroll) {
  283. var
  284. direction = 'down'
  285. ;
  286. scroll = scroll || $scroll.scrollTop();
  287. if(module.lastScroll !== undefined) {
  288. if(module.lastScroll < scroll) {
  289. direction = 'down';
  290. }
  291. else if(module.lastScroll > scroll) {
  292. direction = 'up';
  293. }
  294. }
  295. return direction;
  296. },
  297. scrollChange: function(scroll) {
  298. scroll = scroll || $scroll.scrollTop();
  299. return (module.lastScroll)
  300. ? (scroll - module.lastScroll)
  301. : 0
  302. ;
  303. },
  304. currentElementScroll: function() {
  305. if(module.elementScroll) {
  306. return module.elementScroll;
  307. }
  308. return ( module.is.top() )
  309. ? Math.abs(parseInt($module.css('top'), 10)) || 0
  310. : Math.abs(parseInt($module.css('bottom'), 10)) || 0
  311. ;
  312. },
  313. elementScroll: function(scroll) {
  314. scroll = scroll || $scroll.scrollTop();
  315. var
  316. element = module.cache.element,
  317. scrollContext = module.cache.scrollContext,
  318. delta = module.get.scrollChange(scroll),
  319. maxScroll = (element.height - scrollContext.height + settings.offset),
  320. elementScroll = module.get.currentElementScroll(),
  321. possibleScroll = (elementScroll + delta)
  322. ;
  323. if(module.cache.fits || possibleScroll < 0) {
  324. elementScroll = 0;
  325. }
  326. else if(possibleScroll > maxScroll ) {
  327. elementScroll = maxScroll;
  328. }
  329. else {
  330. elementScroll = possibleScroll;
  331. }
  332. return elementScroll;
  333. }
  334. },
  335. remove: {
  336. lastScroll: function() {
  337. delete module.lastScroll;
  338. },
  339. elementScroll: function(scroll) {
  340. delete module.elementScroll;
  341. },
  342. minimumSize: function() {
  343. $container
  344. .css('min-height', '')
  345. ;
  346. },
  347. offset: function() {
  348. $module.css('margin-top', '');
  349. }
  350. },
  351. set: {
  352. offset: function() {
  353. module.verbose('Setting offset on element', settings.offset);
  354. $module
  355. .css('margin-top', settings.offset)
  356. ;
  357. },
  358. containerSize: function() {
  359. var
  360. tagName = $container.get(0).tagName
  361. ;
  362. if(tagName === 'HTML' || tagName == 'body') {
  363. // this can trigger for too many reasons
  364. //module.error(error.container, tagName, $module);
  365. module.determineContainer();
  366. }
  367. else {
  368. if( Math.abs($container.outerHeight() - module.cache.context.height) > settings.jitter) {
  369. module.debug('Context has padding, specifying exact height for container', module.cache.context.height);
  370. $container.css({
  371. height: module.cache.context.height
  372. });
  373. }
  374. }
  375. },
  376. minimumSize: function() {
  377. var
  378. element = module.cache.element
  379. ;
  380. $container
  381. .css('min-height', element.height)
  382. ;
  383. },
  384. scroll: function(scroll) {
  385. module.debug('Setting scroll on element', scroll);
  386. if(module.elementScroll == scroll) {
  387. return;
  388. }
  389. if( module.is.top() ) {
  390. $module
  391. .css('bottom', '')
  392. .css('top', -scroll)
  393. ;
  394. }
  395. if( module.is.bottom() ) {
  396. $module
  397. .css('top', '')
  398. .css('bottom', scroll)
  399. ;
  400. }
  401. },
  402. size: function() {
  403. if(module.cache.element.height !== 0 && module.cache.element.width !== 0) {
  404. element.style.setProperty('width', module.cache.element.width + 'px', 'important');
  405. element.style.setProperty('height', module.cache.element.height + 'px', 'important');
  406. }
  407. }
  408. },
  409. is: {
  410. standardScroll: function() {
  411. return ($scroll[0] == window);
  412. },
  413. top: function() {
  414. return $module.hasClass(className.top);
  415. },
  416. bottom: function() {
  417. return $module.hasClass(className.bottom);
  418. },
  419. initialPosition: function() {
  420. return (!module.is.fixed() && !module.is.bound());
  421. },
  422. hidden: function() {
  423. return (!$module.is(':visible'));
  424. },
  425. bound: function() {
  426. return $module.hasClass(className.bound);
  427. },
  428. fixed: function() {
  429. return $module.hasClass(className.fixed);
  430. }
  431. },
  432. stick: function(scroll) {
  433. var
  434. cachedPosition = scroll || $scroll.scrollTop(),
  435. cache = module.cache,
  436. fits = cache.fits,
  437. sameHeight = cache.sameHeight,
  438. element = cache.element,
  439. scrollContext = cache.scrollContext,
  440. context = cache.context,
  441. offset = (module.is.bottom() && settings.pushing)
  442. ? settings.bottomOffset
  443. : settings.offset,
  444. scroll = {
  445. top : cachedPosition + offset,
  446. bottom : cachedPosition + offset + scrollContext.height
  447. },
  448. direction = module.get.direction(scroll.top),
  449. elementScroll = (fits)
  450. ? 0
  451. : module.get.elementScroll(scroll.top),
  452. // shorthand
  453. doesntFit = !fits,
  454. elementVisible = (element.height !== 0)
  455. ;
  456. if(elementVisible && !sameHeight) {
  457. if( module.is.initialPosition() ) {
  458. if(scroll.top >= context.bottom) {
  459. module.debug('Initial element position is bottom of container');
  460. module.bindBottom();
  461. }
  462. else if(scroll.top > element.top) {
  463. if( (element.height + scroll.top - elementScroll) >= context.bottom ) {
  464. module.debug('Initial element position is bottom of container');
  465. module.bindBottom();
  466. }
  467. else {
  468. module.debug('Initial element position is fixed');
  469. module.fixTop();
  470. }
  471. }
  472. }
  473. else if( module.is.fixed() ) {
  474. // currently fixed top
  475. if( module.is.top() ) {
  476. if( scroll.top <= element.top ) {
  477. module.debug('Fixed element reached top of container');
  478. module.setInitialPosition();
  479. }
  480. else if( (element.height + scroll.top - elementScroll) >= context.bottom ) {
  481. module.debug('Fixed element reached bottom of container');
  482. module.bindBottom();
  483. }
  484. // scroll element if larger than screen
  485. else if(doesntFit) {
  486. module.set.scroll(elementScroll);
  487. module.save.lastScroll(scroll.top);
  488. module.save.elementScroll(elementScroll);
  489. }
  490. }
  491. // currently fixed bottom
  492. else if(module.is.bottom() ) {
  493. // top edge
  494. if( (scroll.bottom - element.height) <= element.top) {
  495. module.debug('Bottom fixed rail has reached top of container');
  496. module.setInitialPosition();
  497. }
  498. // bottom edge
  499. else if(scroll.bottom >= context.bottom) {
  500. module.debug('Bottom fixed rail has reached bottom of container');
  501. module.bindBottom();
  502. }
  503. // scroll element if larger than screen
  504. else if(doesntFit) {
  505. module.set.scroll(elementScroll);
  506. module.save.lastScroll(scroll.top);
  507. module.save.elementScroll(elementScroll);
  508. }
  509. }
  510. }
  511. else if( module.is.bottom() ) {
  512. if( scroll.top <= element.top ) {
  513. module.debug('Jumped from bottom fixed to top fixed, most likely used home/end button');
  514. module.setInitialPosition();
  515. }
  516. else {
  517. if(settings.pushing) {
  518. if(module.is.bound() && scroll.bottom <= context.bottom ) {
  519. module.debug('Fixing bottom attached element to bottom of browser.');
  520. module.fixBottom();
  521. }
  522. }
  523. else {
  524. if(module.is.bound() && (scroll.top <= context.bottom - element.height) ) {
  525. module.debug('Fixing bottom attached element to top of browser.');
  526. module.fixTop();
  527. }
  528. }
  529. }
  530. }
  531. }
  532. },
  533. bindTop: function() {
  534. module.debug('Binding element to top of parent container');
  535. module.remove.offset();
  536. $module
  537. .css({
  538. left : '',
  539. top : '',
  540. marginBottom : ''
  541. })
  542. .removeClass(className.fixed)
  543. .removeClass(className.bottom)
  544. .addClass(className.bound)
  545. .addClass(className.top)
  546. ;
  547. settings.onTop.call(element);
  548. settings.onUnstick.call(element);
  549. },
  550. bindBottom: function() {
  551. module.debug('Binding element to bottom of parent container');
  552. module.remove.offset();
  553. $module
  554. .css({
  555. left : '',
  556. top : ''
  557. })
  558. .removeClass(className.fixed)
  559. .removeClass(className.top)
  560. .addClass(className.bound)
  561. .addClass(className.bottom)
  562. ;
  563. settings.onBottom.call(element);
  564. settings.onUnstick.call(element);
  565. },
  566. setInitialPosition: function() {
  567. module.debug('Returning to initial position');
  568. module.unfix();
  569. module.unbind();
  570. },
  571. fixTop: function() {
  572. module.debug('Fixing element to top of page');
  573. if(settings.setSize) {
  574. module.set.size();
  575. }
  576. module.set.minimumSize();
  577. module.set.offset();
  578. $module
  579. .css({
  580. left : module.cache.element.left,
  581. bottom : '',
  582. marginBottom : ''
  583. })
  584. .removeClass(className.bound)
  585. .removeClass(className.bottom)
  586. .addClass(className.fixed)
  587. .addClass(className.top)
  588. ;
  589. settings.onStick.call(element);
  590. },
  591. fixBottom: function() {
  592. module.debug('Sticking element to bottom of page');
  593. if(settings.setSize) {
  594. module.set.size();
  595. }
  596. module.set.minimumSize();
  597. module.set.offset();
  598. $module
  599. .css({
  600. left : module.cache.element.left,
  601. bottom : '',
  602. marginBottom : ''
  603. })
  604. .removeClass(className.bound)
  605. .removeClass(className.top)
  606. .addClass(className.fixed)
  607. .addClass(className.bottom)
  608. ;
  609. settings.onStick.call(element);
  610. },
  611. unbind: function() {
  612. if( module.is.bound() ) {
  613. module.debug('Removing container bound position on element');
  614. module.remove.offset();
  615. $module
  616. .removeClass(className.bound)
  617. .removeClass(className.top)
  618. .removeClass(className.bottom)
  619. ;
  620. }
  621. },
  622. unfix: function() {
  623. if( module.is.fixed() ) {
  624. module.debug('Removing fixed position on element');
  625. module.remove.minimumSize();
  626. module.remove.offset();
  627. $module
  628. .removeClass(className.fixed)
  629. .removeClass(className.top)
  630. .removeClass(className.bottom)
  631. ;
  632. settings.onUnstick.call(element);
  633. }
  634. },
  635. reset: function() {
  636. module.debug('Resetting elements position');
  637. module.unbind();
  638. module.unfix();
  639. module.resetCSS();
  640. module.remove.offset();
  641. module.remove.lastScroll();
  642. },
  643. resetCSS: function() {
  644. $module
  645. .css({
  646. width : '',
  647. height : ''
  648. })
  649. ;
  650. $container
  651. .css({
  652. height: ''
  653. })
  654. ;
  655. },
  656. setting: function(name, value) {
  657. if( $.isPlainObject(name) ) {
  658. $.extend(true, settings, name);
  659. }
  660. else if(value !== undefined) {
  661. settings[name] = value;
  662. }
  663. else {
  664. return settings[name];
  665. }
  666. },
  667. internal: function(name, value) {
  668. if( $.isPlainObject(name) ) {
  669. $.extend(true, module, name);
  670. }
  671. else if(value !== undefined) {
  672. module[name] = value;
  673. }
  674. else {
  675. return module[name];
  676. }
  677. },
  678. debug: function() {
  679. if(!settings.silent && settings.debug) {
  680. if(settings.performance) {
  681. module.performance.log(arguments);
  682. }
  683. else {
  684. module.debug = Function.prototype.bind.call(console.info, console, settings.name + ':');
  685. module.debug.apply(console, arguments);
  686. }
  687. }
  688. },
  689. verbose: function() {
  690. if(!settings.silent && settings.verbose && settings.debug) {
  691. if(settings.performance) {
  692. module.performance.log(arguments);
  693. }
  694. else {
  695. module.verbose = Function.prototype.bind.call(console.info, console, settings.name + ':');
  696. module.verbose.apply(console, arguments);
  697. }
  698. }
  699. },
  700. error: function() {
  701. if(!settings.silent) {
  702. module.error = Function.prototype.bind.call(console.error, console, settings.name + ':');
  703. module.error.apply(console, arguments);
  704. }
  705. },
  706. performance: {
  707. log: function(message) {
  708. var
  709. currentTime,
  710. executionTime,
  711. previousTime
  712. ;
  713. if(settings.performance) {
  714. currentTime = new Date().getTime();
  715. previousTime = time || currentTime;
  716. executionTime = currentTime - previousTime;
  717. time = currentTime;
  718. performance.push({
  719. 'Name' : message[0],
  720. 'Arguments' : [].slice.call(message, 1) || '',
  721. 'Element' : element,
  722. 'Execution Time' : executionTime
  723. });
  724. }
  725. clearTimeout(module.performance.timer);
  726. module.performance.timer = setTimeout(module.performance.display, 0);
  727. },
  728. display: function() {
  729. var
  730. title = settings.name + ':',
  731. totalTime = 0
  732. ;
  733. time = false;
  734. clearTimeout(module.performance.timer);
  735. $.each(performance, function(index, data) {
  736. totalTime += data['Execution Time'];
  737. });
  738. title += ' ' + totalTime + 'ms';
  739. if(moduleSelector) {
  740. title += ' \'' + moduleSelector + '\'';
  741. }
  742. if( (console.group !== undefined || console.table !== undefined) && performance.length > 0) {
  743. console.groupCollapsed(title);
  744. if(console.table) {
  745. console.table(performance);
  746. }
  747. else {
  748. $.each(performance, function(index, data) {
  749. console.log(data['Name'] + ': ' + data['Execution Time']+'ms');
  750. });
  751. }
  752. console.groupEnd();
  753. }
  754. performance = [];
  755. }
  756. },
  757. invoke: function(query, passedArguments, context) {
  758. var
  759. object = instance,
  760. maxDepth,
  761. found,
  762. response
  763. ;
  764. passedArguments = passedArguments || queryArguments;
  765. context = element || context;
  766. if(typeof query == 'string' && object !== undefined) {
  767. query = query.split(/[\. ]/);
  768. maxDepth = query.length - 1;
  769. $.each(query, function(depth, value) {
  770. var camelCaseValue = (depth != maxDepth)
  771. ? value + query[depth + 1].charAt(0).toUpperCase() + query[depth + 1].slice(1)
  772. : query
  773. ;
  774. if( $.isPlainObject( object[camelCaseValue] ) && (depth != maxDepth) ) {
  775. object = object[camelCaseValue];
  776. }
  777. else if( object[camelCaseValue] !== undefined ) {
  778. found = object[camelCaseValue];
  779. return false;
  780. }
  781. else if( $.isPlainObject( object[value] ) && (depth != maxDepth) ) {
  782. object = object[value];
  783. }
  784. else if( object[value] !== undefined ) {
  785. found = object[value];
  786. return false;
  787. }
  788. else {
  789. return false;
  790. }
  791. });
  792. }
  793. if ( $.isFunction( found ) ) {
  794. response = found.apply(context, passedArguments);
  795. }
  796. else if(found !== undefined) {
  797. response = found;
  798. }
  799. if($.isArray(returnedValue)) {
  800. returnedValue.push(response);
  801. }
  802. else if(returnedValue !== undefined) {
  803. returnedValue = [returnedValue, response];
  804. }
  805. else if(response !== undefined) {
  806. returnedValue = response;
  807. }
  808. return found;
  809. }
  810. };
  811. if(methodInvoked) {
  812. if(instance === undefined) {
  813. module.initialize();
  814. }
  815. module.invoke(query);
  816. }
  817. else {
  818. if(instance !== undefined) {
  819. instance.invoke('destroy');
  820. }
  821. module.initialize();
  822. }
  823. })
  824. ;
  825. return (returnedValue !== undefined)
  826. ? returnedValue
  827. : this
  828. ;
  829. };
  830. $.fn.sticky.settings = {
  831. name : 'Sticky',
  832. namespace : 'sticky',
  833. silent : false,
  834. debug : false,
  835. verbose : true,
  836. performance : true,
  837. // whether to stick in the opposite direction on scroll up
  838. pushing : false,
  839. context : false,
  840. container : false,
  841. // Context to watch scroll events
  842. scrollContext : window,
  843. // Offset to adjust scroll
  844. offset : 0,
  845. // Offset to adjust scroll when attached to bottom of screen
  846. bottomOffset : 0,
  847. // will only set container height if difference between context and container is larger than this number
  848. jitter : 5,
  849. // set width of sticky element when it is fixed to page (used to make sure 100% width is maintained if no fixed size set)
  850. setSize : true,
  851. // Whether to automatically observe changes with Mutation Observers
  852. observeChanges : false,
  853. // Called when position is recalculated
  854. onReposition : function(){},
  855. // Called on each scroll
  856. onScroll : function(){},
  857. // Called when element is stuck to viewport
  858. onStick : function(){},
  859. // Called when element is unstuck from viewport
  860. onUnstick : function(){},
  861. // Called when element reaches top of context
  862. onTop : function(){},
  863. // Called when element reaches bottom of context
  864. onBottom : function(){},
  865. error : {
  866. container : 'Sticky element must be inside a relative container',
  867. visible : 'Element is hidden, you must call refresh after element becomes visible. Use silent setting to surpress this warning in production.',
  868. method : 'The method you called is not defined.',
  869. invalidContext : 'Context specified does not exist',
  870. elementSize : 'Sticky element is larger than its container, cannot create sticky.'
  871. },
  872. className : {
  873. bound : 'bound',
  874. fixed : 'fixed',
  875. supported : 'native',
  876. top : 'top',
  877. bottom : 'bottom'
  878. }
  879. };
  880. })( jQuery, window, document );