From bddcc75c79bbbebae6cae383ea0547698a6925e2 Mon Sep 17 00:00:00 2001 From: piumosso Date: Tue, 7 Aug 2012 14:05:48 +0400 Subject: [PATCH 1/4] Settings renamed to options. --- js/jquery.fixed.js | 96 ++++++++++++++++++++++------------------------ 1 file changed, 45 insertions(+), 51 deletions(-) diff --git a/js/jquery.fixed.js b/js/jquery.fixed.js index aabf656..8439df0 100644 --- a/js/jquery.fixed.js +++ b/js/jquery.fixed.js @@ -1,56 +1,50 @@ /* -* jQuery Fixed Div plugin v1.0.0 -* @requires jQuery v1.2.6 or later -* is released under the MIT License -*/ + * jQuery Fixed Div plugin v1.0.0 + * @requires jQuery v1.2.6 or later + * is released under the MIT License + */ (function( $ ){ - $.fn.fixed = function( options ) { - - var settings = { - 'top' : 0 - }; - - return this.each(function() { - // If options exist, lets merge them with our default settings - if ( options ) { - $.extend( settings, options ); - } - - // Set the basics - var $this = $(this); - var offset = $this.offset(); - var offset = (parseInt(offset.top) - parseInt(settings.top) ); - - // Init - $this.css('position','absolute'); - - // Check if element is already passed offset; usually on page refresh - if ( $(document).scrollTop() > offset ) { - setFixed(); - }; - - window.onscroll = function() { - //documentElement.scrollTo works for IE/Firefox (Gecko); self.pageYOffset for Chrome/Safari(Webkit)) - if (document.documentElement.scrollTop > offset || self.pageYOffset > offset) { - setFixed(); - - } else if (document.documentElement.scrollTop < offset || self.pageYOffset < offset) { - setAb(); - - }; - }; - - function setFixed() { - $this.css('position','fixed').css('top', settings.top+'px'); - }; - - function setAb() { - $this.css('position','absolute').css('top', '0px'); - }; - - }); - - }; + var default_options = { + 'top': 0 + }; + + $.fn.fixed = function( options ) { + var o = $.extend({}, default_options, options); + + return this.each(function() { + // Set the basics + var $this = $(this); + var offset = $this.offset(); + + offset = (parseInt(offset.top) - parseInt(o.top) ); + + // Init + $this.css('position','absolute'); + + // Check if element is already passed offset; usually on page refresh + if ( $(document).scrollTop() > offset ) { + setFixed(); + } + + window.onscroll = function() { + //documentElement.scrollTo works for IE/Firefox (Gecko); self.pageYOffset for Chrome/Safari(Webkit)) + if (document.documentElement.scrollTop > offset || self.pageYOffset > offset) { + setFixed(); + + } else if (document.documentElement.scrollTop < offset || self.pageYOffset < offset) { + setAb(); + } + }; + + function setFixed() { + $this.css('position','fixed').css('top', o.top+'px'); + } + function setAb() { + $this.css('position','absolute').css('top', '0px'); + } + }); + + }; })( jQuery ); From 33227f8d4a790feb45c86b5820252148738d93d2 Mon Sep 17 00:00:00 2001 From: piumosso Date: Tue, 7 Aug 2012 14:23:10 +0400 Subject: [PATCH 2/4] Absolute-position offset moved in options --- js/jquery.fixed.js | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/js/jquery.fixed.js b/js/jquery.fixed.js index 8439df0..8335bf8 100644 --- a/js/jquery.fixed.js +++ b/js/jquery.fixed.js @@ -7,7 +7,8 @@ (function( $ ){ var default_options = { - 'top': 0 + top: 0, + absolutePosition: {top: 0} }; $.fn.fixed = function( options ) { @@ -15,34 +16,36 @@ return this.each(function() { // Set the basics - var $this = $(this); - var offset = $this.offset(); - - offset = (parseInt(offset.top) - parseInt(o.top) ); + var $this = $(this), + offset = $this.offset(), + topOffset = (parseInt(offset.top) - parseInt(o.top) ); // Init - $this.css('position','absolute'); + $this.css('position', 'absolute'); // Check if element is already passed offset; usually on page refresh - if ( $(document).scrollTop() > offset ) { + if ( $(document).scrollTop() > topOffset ) { setFixed(); } window.onscroll = function() { //documentElement.scrollTo works for IE/Firefox (Gecko); self.pageYOffset for Chrome/Safari(Webkit)) - if (document.documentElement.scrollTop > offset || self.pageYOffset > offset) { + if (document.documentElement.scrollTop > topOffset || self.pageYOffset > topOffset) { setFixed(); - - } else if (document.documentElement.scrollTop < offset || self.pageYOffset < offset) { + } else if (document.documentElement.scrollTop < topOffset || self.pageYOffset < topOffset) { setAb(); } }; function setFixed() { - $this.css('position','fixed').css('top', o.top+'px'); + $this + .css('position', 'fixed') + .css({top: o.top, left: offset.left}); } function setAb() { - $this.css('position','absolute').css('top', '0px'); + $this + .css('position', 'absolute') + .css(o.absolutePosition); } }); From f3850fceca87b7c2678bcce5ae0865a55a374d43 Mon Sep 17 00:00:00 2001 From: piumosso Date: Tue, 7 Aug 2012 14:25:13 +0400 Subject: [PATCH 3/4] Refactoring --- js/jquery.fixed.js | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/js/jquery.fixed.js b/js/jquery.fixed.js index 8335bf8..a1bb328 100644 --- a/js/jquery.fixed.js +++ b/js/jquery.fixed.js @@ -4,14 +4,13 @@ * is released under the MIT License */ -(function( $ ){ - +(function($){ var default_options = { top: 0, absolutePosition: {top: 0} }; - $.fn.fixed = function( options ) { + $.fn.fixed = function(options) { var o = $.extend({}, default_options, options); return this.each(function() { @@ -24,7 +23,7 @@ $this.css('position', 'absolute'); // Check if element is already passed offset; usually on page refresh - if ( $(document).scrollTop() > topOffset ) { + if ($(document).scrollTop() > topOffset) { setFixed(); } @@ -48,6 +47,5 @@ .css(o.absolutePosition); } }); - }; })( jQuery ); From be20bb754d016a7492563495a51a61572d852f44 Mon Sep 17 00:00:00 2001 From: piumosso Date: Tue, 7 Aug 2012 14:28:44 +0400 Subject: [PATCH 4/4] Demo and plugin fixes --- index.html | 192 ++++++++++++++++++++++----------------------- js/jquery.fixed.js | 2 +- 2 files changed, 96 insertions(+), 98 deletions(-) diff --git a/index.html b/index.html index fb2ace3..2c240bf 100755 --- a/index.html +++ b/index.html @@ -1,97 +1,95 @@ - - - - - Fixed Div on Scroll - - - - - - - - -
-
- -
- -
-
-
- .content -
- -
- -
- .content -
- -
- -
- .content -
- -
- -
- .content -
- -
- -
- .content -
- -
- -
- .content -
- -
-
- - - -
- - - + + + + + Fixed Div on Scroll + + + + + + + + +
+
+ +
+ +
+
+
+ .content +
+ +
+ +
+ .content +
+ +
+ +
+ .content +
+ +
+ +
+ .content +
+ +
+ +
+ .content +
+ +
+ +
+ .content +
+ +
+
+ + + +
+ + + diff --git a/js/jquery.fixed.js b/js/jquery.fixed.js index a1bb328..77faeeb 100644 --- a/js/jquery.fixed.js +++ b/js/jquery.fixed.js @@ -7,7 +7,7 @@ (function($){ var default_options = { top: 0, - absolutePosition: {top: 0} + absolutePosition: {top: 0, left: 0} }; $.fn.fixed = function(options) {