Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- define(['jquery', 'underscore'], function($, _) {
- var sticky = function(options) {
- this.selector = options.selector;
- this.offsets = options.offsets;
- this.totalOffset = 0;
- this.cachedLoc = 0;
- this.currScroll = 0;
- this.$mainEl = $(this.selector);
- this.$offsetEls = [];
- this.init = function() {
- var self = this;
- self.cacheLocation();
- $(window).scroll(function() {
- self.checkSticky();
- });
- }
- this.cacheLocation = function() {
- var self = this;
- _.each(self.offsets, function(selector) {
- self.totalOffset += $(selector).outerHeight();
- });
- self.cachedLoc = self.totalOffset + self.$mainEl.offset().top;
- }
- this.checkSticky = function() {
- var self = this;
- self.currScroll = $(window).scrollTop();
- if (self.currScroll >= self.cachedLoc) {
- self.$mainEl.css({
- position: 'fixed',
- top: self.totalOffset + 'px',
- bottom: 0
- });
- }
- else if (self.currScroll < self.cachedLoc) {
- self.$mainEl.removeAttr('style');
- }
- }
- this.init();
- }
- return sticky;
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement