Permalink
Browse files

Adds a padding option that will add the additional padding to the top…

…, bottom, left or right of the item being scrolled into view.

This makes it look a little nicer when scrolling a box into view and the top of the box isn't sitting at the top of the window.
By default the padding is set to 0.
  • Loading branch information...
1 parent 06834cf commit d369c3a122b2255e005783a25e5b92afd860898a @jaiew committed May 16, 2012
Showing with 18 additions and 6 deletions.
  1. +18 −6 jquery.scrollintoview.js
View
@@ -1,4 +1,4 @@
-/*!
+/*!
* jQuery scrollintoview() plugin and :scrollable selector filter
*
* Version 1.8 (14 Jul 2011)
@@ -20,7 +20,8 @@
var settings = {
duration: "fast",
- direction: "both"
+ direction: "both",
+ viewPadding: 0
};
var rootrx = /^(?:html)$/i;
@@ -83,6 +84,17 @@
options = $.extend({}, settings, options);
options.direction = converter[typeof (options.direction) === "string" && options.direction.toLowerCase()] || converter.both;
+ if (typeof options.viewPadding == "number") {
+ options.viewPadding = { x: options.viewPadding , y: options.viewPadding };
+ } else if (typeof options.viewPadding == "object") {
+ if (options.viewPadding.x == undefined) {
+ options.viewPadding.x = 0;
+ }
+ if (options.viewPadding.y == undefined) {
+ options.viewPadding.y = 0;
+ }
+ }
+
var dirStr = "";
if (options.direction.x === true) dirStr = "horizontal";
if (options.direction.y === true) dirStr = dirStr ? "both" : "vertical";
@@ -101,10 +113,10 @@
};
var rel = {
- top: dim.e.rect.top - (dim.s.rect.top + dim.s.border.top),
- bottom: dim.s.rect.bottom - dim.s.border.bottom - dim.s.scrollbar.bottom - dim.e.rect.bottom,
- left: dim.e.rect.left - (dim.s.rect.left + dim.s.border.left),
- right: dim.s.rect.right - dim.s.border.right - dim.s.scrollbar.right - dim.e.rect.right
+ top: dim.e.rect.top - (dim.s.rect.top + dim.s.border.top) - options.viewPadding.y,
+ bottom: dim.s.rect.bottom - dim.s.border.bottom - dim.s.scrollbar.bottom - dim.e.rect.bottom + options.viewPadding.y,
+ left: dim.e.rect.left - (dim.s.rect.left + dim.s.border.left) - options.viewPadding.x,
+ right: dim.s.rect.right - dim.s.border.right - dim.s.scrollbar.right - dim.e.rect.right + options.viewPadding.x
};
var animOptions = {};

0 comments on commit d369c3a

Please sign in to comment.