diff --git a/README.md b/README.md
index 65afee3..62f2997 100644
--- a/README.md
+++ b/README.md
@@ -10,9 +10,12 @@ Include jquery and jquery pin at the bottom of your html. Then pin any element y
$(".pinned").pin()
-To make a pinned element stay within an outer container, use the containerSelector option:
+To make a pinned element stay within an outer container, use the containerSelector option. If you use a fixed header on your page and want the pin to be triggered as soon as your element hits it, use the fixedHeaderSelector option:
- $(".pinned").pin({containerSelector: ".container"})
+ $(".pinned").pin({
+ containerSelector: ".container",
+ fixedHeaderSelector: "header"
+ })
That's it - go pin all the things!
@@ -29,4 +32,4 @@ Redistribution and use in source and binary forms, with or without modification,
Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
-THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
\ No newline at end of file
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
diff --git a/jquery.pin.js b/jquery.pin.js
index 180452b..93f9d67 100644
--- a/jquery.pin.js
+++ b/jquery.pin.js
@@ -1,7 +1,7 @@
(function ($) {
"use strict";
$.fn.pin = function (options) {
- var scrollY = 0, elements = [], disabled = false, $window = $(window);
+ var scrollY = 0, elements = [], disabledPlugin = false, $window = $(window), $fixedHeaderOffset = 0;
options = options || {};
@@ -15,67 +15,81 @@
disabled = true;
continue;
} else {
- disabled = false;
+ disabledPlugin = false;
}
var $container = options.containerSelector ? $this.closest(options.containerSelector) : $(document.body);
+ $fixedHeaderOffset = options.fixedHeaderSelector ? $(options.fixedHeaderSelector).outerHeight(true) : 0;
var offset = $this.offset();
var containerOffset = $container.offset();
+ containerOffset.top -= $fixedHeaderOffset;
var parentOffset = $this.offsetParent().offset();
+
if (!$this.parent().is(".pin-wrapper")) {
$this.wrap("
");
}
$this.data("pin", {
+ enabled: $container.height() >= $this.height()+$fixedHeaderOffset,
from: options.containerSelector ? containerOffset.top : offset.top,
- to: containerOffset.top + $container.height() - $this.outerHeight(),
- end: containerOffset.top + $container.height(),
- parentTop: parentOffset.top
+ to: containerOffset.top + $container.height() - $this.outerHeight() - $fixedHeaderOffset,
+ end: containerOffset.top + $container.height() + $fixedHeaderOffset,
+ parentTop: parentOffset.top,
+ paddingTop: $fixedHeaderOffset,
});
-
$this.css({width: $this.outerWidth()});
$this.parent().css("height", $this.outerHeight());
}
};
var onScroll = function () {
- if (disabled) { return; }
+ if (disabledPlugin) { return; }
scrollY = $window.scrollTop();
-
+
for (var i=0, len=elements.length; i data.end) {
- $this.css('position', '');
- continue;
+ data = $this.data("pin"),
+ enabled = data.enabled,
+ from = data.from,
+ to = data.to,
+ end = data.end,
+ paddingTop = data.paddingTop;
+
+ if(enabled)
+ {
+ if (from + $this.outerHeight() + $fixedHeaderOffset > end) {
+ $this.css({position: "", top: "", left: "", overflowY: "", height: ""}).removeClass('pin-fixed pin-absolute pin-top');
+ continue;
+ }
+
+ if (from < scrollY && to > scrollY) {
+ !($this.css("position") == "fixed") && $this.css({
+ left: $this.offset().left,
+ top: 0
+ }).css({"position": "fixed", overflowY: "auto", height: "100%", paddingTop: paddingTop}).addClass('pin-fixed').removeClass('pin-absolute pin-top');
+ } else if (scrollY >= to) {
+ $this.css({
+ left: "auto",
+ top: to - data.parentTop
+ }).css({position: "absolute", height: "", overflowY: "", paddingTop: paddingTop}).addClass('pin-absolute').removeClass('pin-fixed pin-top');
+ } else {
+ $this.css({position: "", top: "", left: "", overflowY: "", height: "", paddingTop: 0}).removeClass('pin-fixed pin-absolute').addClass('pin-top');
+ }
}
-
- if (from < scrollY && to > scrollY) {
- !($this.css("position") == "fixed") && $this.css({
- left: $this.offset().left,
- top: 0
- }).css("position", "fixed");
- } else if (scrollY >= to) {
- $this.css({
- left: "auto",
- top: to - data.parentTop
- }).css("position", "absolute");
- } else {
- $this.css({position: "", top: "", left: ""});
+ else
+ {
+ $this.css('paddingTop', 0);
}
- }
+ }
};
var update = function () { recalculateLimits(); onScroll(); };
this.each(function () {
var $this = $(this),
- data = $(this).data('pin') || {};
+ data = $(this).data('pin') || {};
if (data && data.update) { return; }
elements.push($this);
@@ -91,5 +105,5 @@
$window.load(update);
return this;
- };
-})(jQuery);
\ No newline at end of file
+ };
+})(jQuery);
diff --git a/jquery.pin.min.js b/jquery.pin.min.js
index efc9dd1..327a09c 100644
--- a/jquery.pin.min.js
+++ b/jquery.pin.min.js
@@ -1 +1 @@
-(function(e){"use strict";e.fn.pin=function(t){var n=0,r=[],i=!1;t=t||{};var s=function(){for(var n=0,s=r.length;n"),o.data("pin",{from:t.containerSelector?f.top:a.top,to:f.top+u.height()-o.outerHeight(),end:f.top+u.height(),parentTop:l.top}),o.css({width:o.outerWidth()}),o.parent().css("height",o.outerHeight())}},o=function(){if(i)return;n=window.scrollY;for(var t=0,s=r.length;tu.end){o.css("position","");continue}an?o.css("position")!="fixed"&&o.css({left:o.offset().left,top:0}).css("position","fixed"):n>=f?o.css({left:"auto",top:f-u.parentTop}).css("position","absolute"):o.css({position:"",top:"",left:""})}},u=function(){s(),o()};return this.each(function(){var t=e(this),n=e(this).data("pin")||{};if(n&&n.update)return;r.push(t),e("img",this).one("load",s),n.update=u,e(this).data("pin",n)}),e(window).scroll(o),e(window).resize(function(){s()}),s(),e(window).load(u),this}})(jQuery);
\ No newline at end of file
+(function(e){"use strict";e.fn.pin=function(t){var n=0,r=[],i=!1,s=e(window);t=t||{};var o=function(){for(var n=0,o=r.length;n"),u.data("pin",{from:t.containerSelector?l.top:f.top,to:l.top+a.height()-u.outerHeight(),end:l.top+a.height(),parentTop:c.top}),u.css({width:u.outerWidth()}),u.parent().css("height",u.outerHeight())}},u=function(){if(i)return;n=s.scrollTop();for(var t=0,o=r.length;ta.end){u.css("position","");continue}fn?u.css("position")!="fixed"&&u.css({left:u.offset().left,top:0}).css("position","fixed"):n>=l?u.css({left:"auto",top:l-a.parentTop}).css("position","absolute"):u.css({position:"",top:"",left:""})}},a=function(){o(),u()};return this.each(function(){var t=e(this),n=e(this).data("pin")||{};if(n&&n.update)return;r.push(t),e("img",this).one("load",o),n.update=a,e(this).data("pin",n)}),s.scroll(u),s.resize(function(){o()}),o(),s.load(a),this}})(jQuery);
\ No newline at end of file
diff --git a/pin.jquery.json b/pin.jquery.json
new file mode 100644
index 0000000..2590426
--- /dev/null
+++ b/pin.jquery.json
@@ -0,0 +1,35 @@
+{
+ "name": "pin",
+ "title": "jQuery Pin",
+ "description": "jQuery plugin for pinning an element within a container.",
+ "keywords": [
+ "scroll",
+ "stick",
+ "sticky"
+ ],
+ "version": "1.0.1",
+ "author": {
+ "name": "Webpop",
+ "url": "http://www.webpop.com"
+ },
+ "maintainers": [
+ {
+ "name": "Mathias Biilmann Christensen",
+ "url": "http://mathias-biilmann.net",
+ "email": "info@mathias-biilmann.net"
+ }
+ ],
+ "licenses": [
+ {
+ "type": "MIT",
+ "url": "https://github.com/jquery/jquery-color/blob/2.1.2/MIT-LICENSE.txt"
+ }
+ ],
+ "bugs": "https://github.com/webpop/jquery.pin/issues",
+ "homepage": "webpop.github.com/jquery.pin",
+ "docs": "webpop.github.com/jquery.pin",
+ "download": "https://raw.github.com/webpop/jquery.pin/gh-pages/jquery.pin.js",
+ "dependencies": {
+ "jquery": ">=1.5"
+ }
+}
\ No newline at end of file