Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for iOS Safari #23

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 44 additions & 25 deletions jquery.stickem.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* @version 1.4
*
* $('.container').stickem({
* item: '.stickem',
* item: '.stickem',
* container: '.stickem-container',
* stickClass: 'stickit',
* endStickClass: 'stickit-end',
Expand All @@ -16,12 +16,14 @@

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

var IS_IOS = (navigator.userAgent.match(/(iPad|iPhone|iPod)/g) ? true : false);

var Stickem = function(elem, options) {
this.elem = elem;
this.$elem = $(elem);
this.options = options;
this.metadata = this.$elem.data("stickem-options");
this.$win = $(window);
this.$win = IS_IOS ? $(document) : $(window);
};

Stickem.prototype = {
Expand All @@ -39,7 +41,7 @@
init: function() {
var _self = this;

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

_self.setWindowHeight();
Expand All @@ -51,16 +53,16 @@

bindEvents: function() {
var _self = this;

_self.$win.on('scroll.stickem', $.proxy(_self.handleScroll, _self));
_self.$win.on('resize.stickem', $.proxy(_self.handleResize, _self));
if (!IS_IOS) _self.$win.on('resize.stickem', $.proxy(_self.handleResize, _self));
},

destroy: function() {
var _self = this;

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

getItem: function(index, element) {
Expand All @@ -70,10 +72,11 @@
$elem: $this,
elemHeight: $this.height(),
$container: $this.parents(_self.config.container),
isStuck: false
isStuck: false,
isStuckAtBottom: false
};

//If the element is smaller than the window
// if the element is smaller than the window
if(_self.windowHeight > item.elemHeight) {
item.containerHeight = item.$container.outerHeight();
item.containerInner = {
Expand All @@ -88,10 +91,18 @@
};

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
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
- _self.config.offset
+ (item.containerInnerHeight
- item.elemHeight);

// if the element is smaller than the container
if(item.containerInnerHeight > item.elemHeight) {
_self.items.push(item);
}
Expand Down Expand Up @@ -124,31 +135,39 @@
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)) {
// if it's stuck, and we need to unstick it
if(pos < item.containerStart || pos > item.scrollFinish) {
if (IS_IOS) {
if (item.isStuckAtBottom && pos < item.containerStart) {
item.$elem.removeClass(_self.config.endStickClass).addClass(_self.config.stickClass);
}
}

item.$elem.removeClass(_self.config.stickClass);

//only at the bottom
// only at the bottom
if(pos > item.scrollFinish) {
item.isStuckAtBottom = true;
item.$elem.addClass(_self.config.endStickClass);
}

item.isStuck = false;

//if supplied fire the onUnstick callback
// 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 we need to stick it
} else if(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);
}
// if supplied fire the onStick callback
if(_self.config.onStick) {
_self.config.onStick(item);
}
}
}
}
Expand All @@ -164,7 +183,7 @@
Stickem.defaults = Stickem.prototype.defaults;

$.fn.stickem = function(options) {
//Create a destroy method so that you can kill it and call it again.
// create a destroy method so that you can kill it and call it again.
this.destroy = function() {
this.each(function() {
new Stickem(this, options).destroy();
Expand Down