Skip to content
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
39 changes: 23 additions & 16 deletions jquery.stickyscroll.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@
*
*/

/*global jQuery */
(function($) {
'use strict';

$.fn.stickyScroll = function(options) {

var methods = {
Expand All @@ -19,6 +22,8 @@

var settings;

options = $.extend(options, {});

if (options.mode !== 'auto' && options.mode !== 'manual') {
if (options.container) {
options.mode = 'auto';
Expand All @@ -32,26 +37,28 @@
mode: 'auto', // 'auto' or 'manual'
container: $('body'),
topBoundary: null,
bottomBoundary: null
bottomBoundary: null,
offsetTop: null
}, options);

function bottomBoundary() {
return $(document).height() - settings.container.offset().top
- settings.container.attr('offsetHeight');
return $(document).height() - settings.container.offset().top -
settings.container.outerHeight(false);
}

function topBoundary() {
return settings.container.offset().top
return settings.container.offset().top;
}

function elHeight(el) {
return $(el).attr('offsetHeight');
return $(el).outerHeight(false);
}

// make sure user input is a jQuery object
settings.container = $(settings.container);
if(!settings.container.length) {
if(console) {
if(typeof console === 'object') {
/*jshint devel:true */
console.log('StickyScroll: the element ' + options.container +
' does not exist, we\'re throwing in the towel');
}
Expand All @@ -68,26 +75,26 @@

var el = $(this),
win = $(window),
id = Date.now() + index,
height = elHeight(el);
id = Date.now() + index;

el.data('sticky-id', id);

win.bind('scroll.stickyscroll-' + id, function() {
var top = $(document).scrollTop(),
bottom = $(document).height() - top - height;
var height = elHeight(el);
var top = $(document).scrollTop();
var bottom = $(document).height() - top - height;

if(bottom <= settings.bottomBoundary) {
if((bottom - Number(settings.offsetTop)) <= settings.bottomBoundary) {
el.offset({
top: $(document).height() - settings.bottomBoundary - height
})
.removeClass('sticky-active')
.removeClass('sticky-inactive')
.addClass('sticky-stopped');
}
else if(top > settings.topBoundary) {
else if((top + Number(settings.offsetTop)) > settings.topBoundary) {
el.offset({
top: $(window).scrollTop()
top: $(window).scrollTop() + Number(settings.offsetTop)
})
.removeClass('sticky-stopped')
.removeClass('sticky-inactive')
Expand All @@ -110,9 +117,8 @@
settings.topBoundary = topBoundary();
settings.bottomBoundary = bottomBoundary();
}
height = elHeight(el);
$(this).scroll();
})
});

el.addClass('sticky-processed');

Expand Down Expand Up @@ -154,7 +160,8 @@
return methods.init.apply(this, arguments);
}

else if(console) {
else if(typeof console === 'object') {
/*jshint devel:true */
console.log('Method' + options +
' does not exist on jQuery.stickyScroll');
}
Expand Down