sticky.js 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977
  1. /*!
  2. * # Semantic UI 2.5.0 - 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. var tallestHeight = Math.max(module.cache.context.height, module.cache.element.height);
  369. if(tallestHeight - $container.outerHeight() > settings.jitter) {
  370. module.debug('Context is taller than container. Specifying exact height for container', module.cache.context.height);
  371. $container.css({
  372. height: tallestHeight,
  373. });
  374. }
  375. else {
  376. $container.css({
  377. height: '',
  378. });
  379. }
  380. if( Math.abs($container.outerHeight() - module.cache.context.height) > settings.jitter) {
  381. module.debug('Context has padding, specifying exact height for container', module.cache.context.height);
  382. $container.css({
  383. height: module.cache.context.height
  384. });
  385. }
  386. }
  387. },
  388. minimumSize: function() {
  389. var
  390. element = module.cache.element
  391. ;
  392. $container
  393. .css('min-height', element.height)
  394. ;
  395. },
  396. scroll: function(scroll) {
  397. module.debug('Setting scroll on element', scroll);
  398. if(module.elementScroll == scroll) {
  399. return;
  400. }
  401. if( module.is.top() ) {
  402. $module
  403. .css('bottom', '')
  404. .css('top', -scroll)
  405. ;
  406. }
  407. if( module.is.bottom() ) {
  408. $module
  409. .css('top', '')
  410. .css('bottom', scroll)
  411. ;
  412. }
  413. },
  414. size: function() {
  415. if(module.cache.element.height !== 0 && module.cache.element.width !== 0) {
  416. element.style.setProperty('width', module.cache.element.width + 'px', 'important');
  417. element.style.setProperty('height', module.cache.element.height + 'px', 'important');
  418. }
  419. }
  420. },
  421. is: {
  422. standardScroll: function() {
  423. return ($scroll[0] == window);
  424. },
  425. top: function() {
  426. return $module.hasClass(className.top);
  427. },
  428. bottom: function() {
  429. return $module.hasClass(className.bottom);
  430. },
  431. initialPosition: function() {
  432. return (!module.is.fixed() && !module.is.bound());
  433. },
  434. hidden: function() {
  435. return (!$module.is(':visible'));
  436. },
  437. bound: function() {
  438. return $module.hasClass(className.bound);
  439. },
  440. fixed: function() {
  441. return $module.hasClass(className.fixed);
  442. }
  443. },
  444. stick: function(scroll) {
  445. var
  446. cachedPosition = scroll || $scroll.scrollTop(),
  447. cache = module.cache,
  448. fits = cache.fits,
  449. sameHeight = cache.sameHeight,
  450. element = cache.element,
  451. scrollContext = cache.scrollContext,
  452. context = cache.context,
  453. offset = (module.is.bottom() && settings.pushing)
  454. ? settings.bottomOffset
  455. : settings.offset,
  456. scroll = {
  457. top : cachedPosition + offset,
  458. bottom : cachedPosition + offset + scrollContext.height
  459. },
  460. direction = module.get.direction(scroll.top),
  461. elementScroll = (fits)
  462. ? 0
  463. : module.get.elementScroll(scroll.top),
  464. // shorthand
  465. doesntFit = !fits,
  466. elementVisible = (element.height !== 0)
  467. ;
  468. if(elementVisible && !sameHeight) {
  469. if( module.is.initialPosition() ) {
  470. if(scroll.top >= context.bottom) {
  471. module.debug('Initial element position is bottom of container');
  472. module.bindBottom();
  473. }
  474. else if(scroll.top > element.top) {
  475. if((element.height + scroll.top - elementScroll) >= context.bottom && element.height < context.height) {
  476. module.debug('Initial element position is bottom of container');
  477. module.bindBottom();
  478. }
  479. else {
  480. module.debug('Initial element position is fixed');
  481. module.fixTop();
  482. }
  483. }
  484. }
  485. else if( module.is.fixed() ) {
  486. // currently fixed top
  487. if( module.is.top() ) {
  488. if( scroll.top <= element.top ) {
  489. module.debug('Fixed element reached top of container');
  490. module.setInitialPosition();
  491. }
  492. else if( (element.height + scroll.top - elementScroll) >= context.bottom ) {
  493. module.debug('Fixed element reached bottom of container');
  494. module.bindBottom();
  495. }
  496. // scroll element if larger than screen
  497. else if(doesntFit) {
  498. module.set.scroll(elementScroll);
  499. module.save.lastScroll(scroll.top);
  500. module.save.elementScroll(elementScroll);
  501. }
  502. }
  503. // currently fixed bottom
  504. else if(module.is.bottom() ) {
  505. // top edge
  506. if( (scroll.bottom - element.height) <= element.top) {
  507. module.debug('Bottom fixed rail has reached top of container');
  508. module.setInitialPosition();
  509. }
  510. // bottom edge
  511. else if(scroll.bottom >= context.bottom) {
  512. module.debug('Bottom fixed rail has reached bottom of container');
  513. module.bindBottom();
  514. }
  515. // scroll element if larger than screen
  516. else if(doesntFit) {
  517. module.set.scroll(elementScroll);
  518. module.save.lastScroll(scroll.top);
  519. module.save.elementScroll(elementScroll);
  520. }
  521. }
  522. }
  523. else if( module.is.bottom() ) {
  524. if( scroll.top <= element.top ) {
  525. module.debug('Jumped from bottom fixed to top fixed, most likely used home/end button');
  526. module.setInitialPosition();
  527. }
  528. else {
  529. if(settings.pushing) {
  530. if(module.is.bound() && scroll.bottom <= context.bottom ) {
  531. module.debug('Fixing bottom attached element to bottom of browser.');
  532. module.fixBottom();
  533. }
  534. }
  535. else {
  536. if(module.is.bound() && (scroll.top <= context.bottom - element.height) ) {
  537. module.debug('Fixing bottom attached element to top of browser.');
  538. module.fixTop();
  539. }
  540. }
  541. }
  542. }
  543. }
  544. },
  545. bindTop: function() {
  546. module.debug('Binding element to top of parent container');
  547. module.remove.offset();
  548. if(settings.setSize) {
  549. module.set.size();
  550. }
  551. $module
  552. .css({
  553. left : '',
  554. top : '',
  555. marginBottom : ''
  556. })
  557. .removeClass(className.fixed)
  558. .removeClass(className.bottom)
  559. .addClass(className.bound)
  560. .addClass(className.top)
  561. ;
  562. settings.onTop.call(element);
  563. settings.onUnstick.call(element);
  564. },
  565. bindBottom: function() {
  566. module.debug('Binding element to bottom of parent container');
  567. module.remove.offset();
  568. if(settings.setSize) {
  569. module.set.size();
  570. }
  571. $module
  572. .css({
  573. left : '',
  574. top : ''
  575. })
  576. .removeClass(className.fixed)
  577. .removeClass(className.top)
  578. .addClass(className.bound)
  579. .addClass(className.bottom)
  580. ;
  581. settings.onBottom.call(element);
  582. settings.onUnstick.call(element);
  583. },
  584. setInitialPosition: function() {
  585. module.debug('Returning to initial position');
  586. module.unfix();
  587. module.unbind();
  588. },
  589. fixTop: function() {
  590. module.debug('Fixing element to top of page');
  591. if(settings.setSize) {
  592. module.set.size();
  593. }
  594. module.set.minimumSize();
  595. module.set.offset();
  596. $module
  597. .css({
  598. left : module.cache.element.left,
  599. bottom : '',
  600. marginBottom : ''
  601. })
  602. .removeClass(className.bound)
  603. .removeClass(className.bottom)
  604. .addClass(className.fixed)
  605. .addClass(className.top)
  606. ;
  607. settings.onStick.call(element);
  608. },
  609. fixBottom: function() {
  610. module.debug('Sticking element to bottom of page');
  611. if(settings.setSize) {
  612. module.set.size();
  613. }
  614. module.set.minimumSize();
  615. module.set.offset();
  616. $module
  617. .css({
  618. left : module.cache.element.left,
  619. bottom : '',
  620. marginBottom : ''
  621. })
  622. .removeClass(className.bound)
  623. .removeClass(className.top)
  624. .addClass(className.fixed)
  625. .addClass(className.bottom)
  626. ;
  627. settings.onStick.call(element);
  628. },
  629. unbind: function() {
  630. if( module.is.bound() ) {
  631. module.debug('Removing container bound position on element');
  632. module.remove.offset();
  633. $module
  634. .removeClass(className.bound)
  635. .removeClass(className.top)
  636. .removeClass(className.bottom)
  637. ;
  638. }
  639. },
  640. unfix: function() {
  641. if( module.is.fixed() ) {
  642. module.debug('Removing fixed position on element');
  643. module.remove.minimumSize();
  644. module.remove.offset();
  645. $module
  646. .removeClass(className.fixed)
  647. .removeClass(className.top)
  648. .removeClass(className.bottom)
  649. ;
  650. settings.onUnstick.call(element);
  651. }
  652. },
  653. reset: function() {
  654. module.debug('Resetting elements position');
  655. module.unbind();
  656. module.unfix();
  657. module.resetCSS();
  658. module.remove.offset();
  659. module.remove.lastScroll();
  660. },
  661. resetCSS: function() {
  662. $module
  663. .css({
  664. width : '',
  665. height : ''
  666. })
  667. ;
  668. $container
  669. .css({
  670. height: ''
  671. })
  672. ;
  673. },
  674. setting: function(name, value) {
  675. if( $.isPlainObject(name) ) {
  676. $.extend(true, settings, name);
  677. }
  678. else if(value !== undefined) {
  679. settings[name] = value;
  680. }
  681. else {
  682. return settings[name];
  683. }
  684. },
  685. internal: function(name, value) {
  686. if( $.isPlainObject(name) ) {
  687. $.extend(true, module, name);
  688. }
  689. else if(value !== undefined) {
  690. module[name] = value;
  691. }
  692. else {
  693. return module[name];
  694. }
  695. },
  696. debug: function() {
  697. if(!settings.silent && settings.debug) {
  698. if(settings.performance) {
  699. module.performance.log(arguments);
  700. }
  701. else {
  702. module.debug = Function.prototype.bind.call(console.info, console, settings.name + ':');
  703. module.debug.apply(console, arguments);
  704. }
  705. }
  706. },
  707. verbose: function() {
  708. if(!settings.silent && settings.verbose && settings.debug) {
  709. if(settings.performance) {
  710. module.performance.log(arguments);
  711. }
  712. else {
  713. module.verbose = Function.prototype.bind.call(console.info, console, settings.name + ':');
  714. module.verbose.apply(console, arguments);
  715. }
  716. }
  717. },
  718. error: function() {
  719. if(!settings.silent) {
  720. module.error = Function.prototype.bind.call(console.error, console, settings.name + ':');
  721. module.error.apply(console, arguments);
  722. }
  723. },
  724. performance: {
  725. log: function(message) {
  726. var
  727. currentTime,
  728. executionTime,
  729. previousTime
  730. ;
  731. if(settings.performance) {
  732. currentTime = new Date().getTime();
  733. previousTime = time || currentTime;
  734. executionTime = currentTime - previousTime;
  735. time = currentTime;
  736. performance.push({
  737. 'Name' : message[0],
  738. 'Arguments' : [].slice.call(message, 1) || '',
  739. 'Element' : element,
  740. 'Execution Time' : executionTime
  741. });
  742. }
  743. clearTimeout(module.performance.timer);
  744. module.performance.timer = setTimeout(module.performance.display, 0);
  745. },
  746. display: function() {
  747. var
  748. title = settings.name + ':',
  749. totalTime = 0
  750. ;
  751. time = false;
  752. clearTimeout(module.performance.timer);
  753. $.each(performance, function(index, data) {
  754. totalTime += data['Execution Time'];
  755. });
  756. title += ' ' + totalTime + 'ms';
  757. if(moduleSelector) {
  758. title += ' \'' + moduleSelector + '\'';
  759. }
  760. if( (console.group !== undefined || console.table !== undefined) && performance.length > 0) {
  761. console.groupCollapsed(title);
  762. if(console.table) {
  763. console.table(performance);
  764. }
  765. else {
  766. $.each(performance, function(index, data) {
  767. console.log(data['Name'] + ': ' + data['Execution Time']+'ms');
  768. });
  769. }
  770. console.groupEnd();
  771. }
  772. performance = [];
  773. }
  774. },
  775. invoke: function(query, passedArguments, context) {
  776. var
  777. object = instance,
  778. maxDepth,
  779. found,
  780. response
  781. ;
  782. passedArguments = passedArguments || queryArguments;
  783. context = element || context;
  784. if(typeof query == 'string' && object !== undefined) {
  785. query = query.split(/[\. ]/);
  786. maxDepth = query.length - 1;
  787. $.each(query, function(depth, value) {
  788. var camelCaseValue = (depth != maxDepth)
  789. ? value + query[depth + 1].charAt(0).toUpperCase() + query[depth + 1].slice(1)
  790. : query
  791. ;
  792. if( $.isPlainObject( object[camelCaseValue] ) && (depth != maxDepth) ) {
  793. object = object[camelCaseValue];
  794. }
  795. else if( object[camelCaseValue] !== undefined ) {
  796. found = object[camelCaseValue];
  797. return false;
  798. }
  799. else if( $.isPlainObject( object[value] ) && (depth != maxDepth) ) {
  800. object = object[value];
  801. }
  802. else if( object[value] !== undefined ) {
  803. found = object[value];
  804. return false;
  805. }
  806. else {
  807. return false;
  808. }
  809. });
  810. }
  811. if ( $.isFunction( found ) ) {
  812. response = found.apply(context, passedArguments);
  813. }
  814. else if(found !== undefined) {
  815. response = found;
  816. }
  817. if($.isArray(returnedValue)) {
  818. returnedValue.push(response);
  819. }
  820. else if(returnedValue !== undefined) {
  821. returnedValue = [returnedValue, response];
  822. }
  823. else if(response !== undefined) {
  824. returnedValue = response;
  825. }
  826. return found;
  827. }
  828. };
  829. if(methodInvoked) {
  830. if(instance === undefined) {
  831. module.initialize();
  832. }
  833. module.invoke(query);
  834. }
  835. else {
  836. if(instance !== undefined) {
  837. instance.invoke('destroy');
  838. }
  839. module.initialize();
  840. }
  841. })
  842. ;
  843. return (returnedValue !== undefined)
  844. ? returnedValue
  845. : this
  846. ;
  847. };
  848. $.fn.sticky.settings = {
  849. name : 'Sticky',
  850. namespace : 'sticky',
  851. silent : false,
  852. debug : false,
  853. verbose : true,
  854. performance : true,
  855. // whether to stick in the opposite direction on scroll up
  856. pushing : false,
  857. context : false,
  858. container : false,
  859. // Context to watch scroll events
  860. scrollContext : window,
  861. // Offset to adjust scroll
  862. offset : 0,
  863. // Offset to adjust scroll when attached to bottom of screen
  864. bottomOffset : 0,
  865. // will only set container height if difference between context and container is larger than this number
  866. jitter : 5,
  867. // set width of sticky element when it is fixed to page (used to make sure 100% width is maintained if no fixed size set)
  868. setSize : true,
  869. // Whether to automatically observe changes with Mutation Observers
  870. observeChanges : false,
  871. // Called when position is recalculated
  872. onReposition : function(){},
  873. // Called on each scroll
  874. onScroll : function(){},
  875. // Called when element is stuck to viewport
  876. onStick : function(){},
  877. // Called when element is unstuck from viewport
  878. onUnstick : function(){},
  879. // Called when element reaches top of context
  880. onTop : function(){},
  881. // Called when element reaches bottom of context
  882. onBottom : function(){},
  883. error : {
  884. container : 'Sticky element must be inside a relative container',
  885. visible : 'Element is hidden, you must call refresh after element becomes visible. Use silent setting to surpress this warning in production.',
  886. method : 'The method you called is not defined.',
  887. invalidContext : 'Context specified does not exist',
  888. elementSize : 'Sticky element is larger than its container, cannot create sticky.'
  889. },
  890. className : {
  891. bound : 'bound',
  892. fixed : 'fixed',
  893. supported : 'native',
  894. top : 'top',
  895. bottom : 'bottom'
  896. }
  897. };
  898. })( jQuery, window, document );