Advertisement
Guest User

Untitled

a guest
Dec 19th, 2014
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.14 KB | None | 0 0
  1. define(['jquery', 'underscore'], function($, _) {
  2.  
  3. var sticky = function(options) {
  4. this.selector = options.selector;
  5. this.offsets = options.offsets;
  6. this.totalOffset = 0;
  7. this.cachedLoc = 0;
  8. this.currScroll = 0;
  9. this.$mainEl = $(this.selector);
  10. this.$offsetEls = [];
  11.  
  12. this.init = function() {
  13. var self = this;
  14.  
  15. self.cacheLocation();
  16.  
  17. $(window).scroll(function() {
  18. self.checkSticky();
  19. });
  20. }
  21.  
  22. this.cacheLocation = function() {
  23. var self = this;
  24.  
  25. _.each(self.offsets, function(selector) {
  26. self.totalOffset += $(selector).outerHeight();
  27. });
  28.  
  29. self.cachedLoc = self.totalOffset + self.$mainEl.offset().top;
  30. }
  31.  
  32. this.checkSticky = function() {
  33. var self = this;
  34.  
  35. self.currScroll = $(window).scrollTop();
  36.  
  37. if (self.currScroll >= self.cachedLoc) {
  38. self.$mainEl.css({
  39. position: 'fixed',
  40. top: self.totalOffset + 'px',
  41. bottom: 0
  42. });
  43. }
  44. else if (self.currScroll < self.cachedLoc) {
  45. self.$mainEl.removeAttr('style');
  46. }
  47. }
  48.  
  49. this.init();
  50. }
  51.  
  52. return sticky;
  53.  
  54. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement