Skip to content

Commit

Permalink
added jquery.pin
Browse files Browse the repository at this point in the history
  • Loading branch information
sparkyfen committed Mar 27, 2013
1 parent 92f486c commit 89a4fe5
Show file tree
Hide file tree
Showing 3 changed files with 131 additions and 0 deletions.
95 changes: 95 additions & 0 deletions ajax/libs/jquery.pin/0.1/jquery.pin.js
@@ -0,0 +1,95 @@
(function ($) {
"use strict";
$.fn.pin = function (options) {
var scrollY = 0, elements = [], disabled = false, $window = $(window);

options = options || {};

var recalculateLimits = function () {
for (var i=0, len=elements.length; i<len; i++) {
var $this = elements[i];

if (options.minWidth && $window.width() <= options.minWidth) {
if ($this.parent().is(".pin-wrapper")) { $this.unwrap(); }
$this.css({width: "", left: "", top: "", position: ""});
disabled = true;
continue;
} else {
disabled = false;
}

var $container = options.containerSelector ? $this.closest(options.containerSelector) : $(document.body);
var offset = $this.offset();
var containerOffset = $container.offset();
var parentOffset = $this.offsetParent().offset();

if (!$this.parent().is(".pin-wrapper")) {
$this.wrap("<div class='pin-wrapper'>");
}

$this.data("pin", {
from: options.containerSelector ? containerOffset.top : offset.top,
to: containerOffset.top + $container.height() - $this.outerHeight(),
end: containerOffset.top + $container.height(),
parentTop: parentOffset.top
});

$this.css({width: $this.outerWidth()});
$this.parent().css("height", $this.outerHeight());
}
};

var onScroll = function () {
if (disabled) { return; }

scrollY = $window.scrollTop();

for (var i=0, len=elements.length; i<len; i++) {
var $this = $(elements[i]),
data = $this.data("pin"),
from = data.from,
to = data.to;

if (from + $this.outerHeight() > data.end) {
$this.css('position', '');
continue;
}

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: ""});
}
}
};

var update = function () { recalculateLimits(); onScroll(); };

this.each(function () {
var $this = $(this),
data = $(this).data('pin') || {};

if (data && data.update) { return; }
elements.push($this);
$("img", this).one("load", recalculateLimits);
data.update = update;
$(this).data('pin', data);
});

$window.scroll(onScroll);
$window.resize(function () { recalculateLimits(); });
recalculateLimits();

$window.load(update);

return this;
};
})(jQuery);
1 change: 1 addition & 0 deletions ajax/libs/jquery.pin/0.1/jquery.pin.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

35 changes: 35 additions & 0 deletions ajax/libs/jquery.pin/package.json
@@ -0,0 +1,35 @@
{
"name": "jquery.pin",
"filename": "jquery.pin.min.js",
"description": "Pin any element within a container",
"version": "0.1",
"keywords": [
"jquery-pin",
"jquery.pin",
"pin"
],
"maintainers": [
{
"name": "Mathias Biilmann",
"email": "us@webpop.com",
"web": "https://github.com/webpop"
}
],
"demos": [
"http://webpop.github.com/jquery.pin/"
],
"bugs": "https://github.com/webpop/jquery.pin/issues",
"repositories": [
{
"type": "git",
"url": "https://github.com/webpop/jquery.pin.git"
}
],
"licenses": [
{
"name": "Personalized License",
"url": "https://github.com/webpop/jquery.pin#license"
}
],
"homepage": "http://webpop.github.com/jquery.pin/"
}

0 comments on commit 89a4fe5

Please sign in to comment.