diff --git a/README.textile b/README.textile
index 2c36685..3d60f02 100644
--- a/README.textile
+++ b/README.textile
@@ -87,6 +87,28 @@ bc.. $(someMyOneDiv).bind('inview', function(e, isInView, visiblePartX, visibleP
}
});
+h2. Using offset
+
+If you use this code for lazy loading images, sometimes you are interested to preload the image when it's getting close to the view port. So you can add a offset to the target element.
+
+Add offset from 300px to all sites from the view port. Example: Your view port is 600x800 px, so the new one will be 1200x1400. All images in this area will be preloaded.
+
+```html
+
+```
+
+Add offset from 300px to top and bottom of view port. Example: Your view port is 600x800 px, so the new one will be 600x1400.
+
+```html
+
+```
+
+Add offset from 300px to left and right of the view port. Example: Your view port is 600x800 px, the new will be 1200x1400px.
+
+```html
+
+```
+
h2. How it works
An interval in the background checks the position of the elements against the viewport dimensions and the scroll position.
@@ -120,4 +142,4 @@ h4. The Test Suite succeeds in the following browsers that were tested:
h4. The Test Suite doesn't completely succeed but the demos in this repository work without problems in the following browsers:
* Mobile WebKit on Android 2.2
-* Mobile WebKit on Palm Pre 1
\ No newline at end of file
+* Mobile WebKit on Palm Pre 1
diff --git a/jquery.inview.js b/jquery.inview.js
index 56815f0..d1a4606 100644
--- a/jquery.inview.js
+++ b/jquery.inview.js
@@ -89,10 +89,22 @@
elementSize = { height: $element.height(), width: $element.width() },
elementOffset = $element.offset(),
inView = $element.data('inview'),
+ offset = 0,
+ offsetLeft = 0,
+ offsetTop = 0,
visiblePartX,
visiblePartY,
visiblePartsMerged;
+ if (offset = $element.data('offset')) {
+ offsetLeft = offset;
+ offsetTop = offset;
+ } else if (offset = $element.data('offset-top')) {
+ offsetTop = offset;
+ } else if (offset = $element.data('offset-left')) {
+ offsetLeft = offset;
+ }
+
// Don't ask me why because I haven't figured out yet:
// viewportOffset and viewportSize are sometimes suddenly null in Firefox 5.
// Even though it sounds weird:
@@ -102,10 +114,10 @@
return;
}
- if (elementOffset.top + elementSize.height > viewportOffset.top &&
- elementOffset.top < viewportOffset.top + viewportSize.height &&
- elementOffset.left + elementSize.width > viewportOffset.left &&
- elementOffset.left < viewportOffset.left + viewportSize.width) {
+ if (elementOffset.top + elementSize.height > viewportOffset.top - offsetTop &&
+ elementOffset.top < viewportOffset.top + viewportSize.height + offsetTop &&
+ elementOffset.left + elementSize.width > viewportOffset.left - offsetLeft &&
+ elementOffset.left < viewportOffset.left + viewportSize.width + offsetLeft) {
visiblePartX = (viewportOffset.left > elementOffset.left ?
'right' : (viewportOffset.left + viewportSize.width) < (elementOffset.left + elementSize.width) ?
'left' : 'both');
diff --git a/test/index.html b/test/index.html
index 58f5689..eaf2f61 100644
--- a/test/index.html
+++ b/test/index.html
@@ -5,40 +5,46 @@