Skip to content

Commit

Permalink
Merge pull request davist11#7 from barneyfoxuk/master
Browse files Browse the repository at this point in the history
added onStick and onUnstick callbacks
  • Loading branch information
davist11 committed Oct 3, 2012
2 parents e55f959 + dd016cb commit 08f02dd
Showing 1 changed file with 33 additions and 25 deletions.
58 changes: 33 additions & 25 deletions jquery.stickem.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
*/

;(function($, window, document, undefined) {

var Stickem = function(elem, options) {
this.elem = elem;
this.$elem = $(elem);
Expand All @@ -34,34 +34,34 @@

init: function() {
var _self = this;

//Merge options
_self.config = $.extend({}, _self.defaults, _self.options, _self.metadata);

_self.setWindowHeight();
_self.getItems();
_self.bindEvents();

return _self;
},

bindEvents: function() {
var _self = this;

if(_self.items.length > 0) {
_self.$win.on('scroll.stickem', $.proxy(_self.handleScroll, _self));

_self.$win.on('resize.stickem', $.proxy(_self.handleResize, _self));
}
},

destroy: function() {
var _self = this;

_self.$win.off('scroll.stickem');
_self.$win.off('resize.stickem');
},

getItem: function(index, element) {
var _self = this;
var $this = $(element);
Expand All @@ -71,7 +71,7 @@
$container: $this.parents(_self.config.container),
isStuck: false
};

//If the element is smaller than the window
if(_self.windowHeight > item.elemHeight) {
item.containerHeight = item.$container.outerHeight();
Expand All @@ -83,42 +83,42 @@
padding: {
bottom: parseInt(item.$container.css('padding-bottom'), 10) || 0,
top: parseInt(item.$container.css('padding-top'), 10) || 0
}
}
};

item.containerInnerHeight = item.$container.height();
item.containerStart = item.$container.offset().top - _self.config.offset + _self.config.start + item.containerInner.padding.top + item.containerInner.border.top;
item.scrollFinish = item.containerStart - _self.config.start + (item.containerInnerHeight - item.elemHeight);

//If the element is smaller than the container
if(item.containerInnerHeight > item.elemHeight) {
_self.items.push(item);
}
}
},

getItems: function() {
var _self = this;

_self.items = [];

_self.$elem.find(_self.config.item).each($.proxy(_self.getItem, _self));
},

handleResize: function() {
var _self = this;

_self.getItems();
_self.setWindowHeight();
},

handleScroll: function() {
var _self = this;
var pos = _self.$win.scrollTop();

for(var i = 0, len = _self.items.length; i < len; i++) {
var item = _self.items[i];

//If it's stuck, and we need to unstick it
if(item.isStuck && (pos < item.containerStart || pos > item.scrollFinish)) {
item.$elem.removeClass(_self.config.stickClass);
Expand All @@ -129,18 +129,26 @@
}

item.isStuck = false;


//if supplied fire the onUnstick callback
if(_self.config.onUnstick)
_self.config.onUnstick(item);

//If we need to stick it
} else if(item.isStuck === false && pos > item.containerStart && pos < item.scrollFinish) {
item.$elem.removeClass(_self.config.endStickClass).addClass(_self.config.stickClass);
item.isStuck = true;

//if supplied fire the onStick callback
if(_self.config.onStick)
_self.config.onStick(item);
}
}
},

setWindowHeight: function() {
var _self = this;

_self.windowHeight = _self.$win.height() - _self.config.offset;
}
};
Expand All @@ -154,7 +162,7 @@
new Stickem(this, options).destroy();
});
};

return this.each(function() {
new Stickem(this, options).init();
});
Expand Down

0 comments on commit 08f02dd

Please sign in to comment.