sticky.js 30 KB

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