From 7ccae8b357027922d23834e57a70d9e3e1456bcc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alejandro=20Garc=C3=ADa=20Iglesias?= Date: Fri, 5 Oct 2012 13:00:32 -0300 Subject: [PATCH 1/4] Defaults options to an empty object Avoids an error if you don't pass any options object on initialization. --- jquery.stickyscroll.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/jquery.stickyscroll.js b/jquery.stickyscroll.js index 0336994..5e57bc7 100644 --- a/jquery.stickyscroll.js +++ b/jquery.stickyscroll.js @@ -19,6 +19,8 @@ var settings; + options = $.extend(options, {}); + if (options.mode !== 'auto' && options.mode !== 'manual') { if (options.container) { options.mode = 'auto'; From ec0c516356e9f0cd292a36a0c14bc329f428264c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alejandro=20Garc=C3=ADa=20Iglesias?= Date: Thu, 29 Nov 2012 15:59:16 -0200 Subject: [PATCH 2/4] Fixes how it gets the height of the elements. Adds offsetTop option. It was getting the height with a wrong method. --- jquery.stickyscroll.js | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/jquery.stickyscroll.js b/jquery.stickyscroll.js index 5e57bc7..0d9d897 100644 --- a/jquery.stickyscroll.js +++ b/jquery.stickyscroll.js @@ -34,20 +34,21 @@ 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'); + - 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 @@ -79,7 +80,7 @@ var top = $(document).scrollTop(), bottom = $(document).height() - top - height; - if(bottom <= settings.bottomBoundary) { + if((bottom - Number(settings.offsetTop)) <= settings.bottomBoundary) { el.offset({ top: $(document).height() - settings.bottomBoundary - height }) @@ -87,9 +88,9 @@ .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') From 9fd3dc4706cb7ebb01bbbae7b22747e7a7161a94 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alejandro=20Garc=C3=ADa=20Iglesias?= Date: Thu, 29 Nov 2012 16:24:00 -0200 Subject: [PATCH 3/4] Now handles elements with dynamic sizes correctly. --- jquery.stickyscroll.js | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/jquery.stickyscroll.js b/jquery.stickyscroll.js index 0d9d897..0cfab24 100644 --- a/jquery.stickyscroll.js +++ b/jquery.stickyscroll.js @@ -71,14 +71,14 @@ 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 - Number(settings.offsetTop)) <= settings.bottomBoundary) { el.offset({ @@ -113,7 +113,6 @@ settings.topBoundary = topBoundary(); settings.bottomBoundary = bottomBoundary(); } - height = elHeight(el); $(this).scroll(); }) From f5bef3055a7a5b4ca0be8ac5e02e5e7cd93eac26 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alejandro=20Garc=C3=ADa=20Iglesias?= Date: Thu, 29 Nov 2012 16:32:22 -0200 Subject: [PATCH 4/4] Fixes code based on JSHint linting. --- jquery.stickyscroll.js | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/jquery.stickyscroll.js b/jquery.stickyscroll.js index 0cfab24..268066e 100644 --- a/jquery.stickyscroll.js +++ b/jquery.stickyscroll.js @@ -10,7 +10,10 @@ * */ +/*global jQuery */ (function($) { + 'use strict'; + $.fn.stickyScroll = function(options) { var methods = { @@ -39,8 +42,8 @@ }, options); function bottomBoundary() { - return $(document).height() - settings.container.offset().top - - settings.container.outerHeight(false); + return $(document).height() - settings.container.offset().top - + settings.container.outerHeight(false); } function topBoundary() { @@ -54,7 +57,8 @@ // 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'); } @@ -114,7 +118,7 @@ settings.bottomBoundary = bottomBoundary(); } $(this).scroll(); - }) + }); el.addClass('sticky-processed'); @@ -156,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'); }