Skip to content

Commit dd4be9e

Browse files
committed
Update README
1 parent e01a41f commit dd4be9e

File tree

2 files changed

+64
-64
lines changed

2 files changed

+64
-64
lines changed

README.md

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
# Element 'inview' Event Plugin
2+
3+
Event that is fired as soon as an element appears in the user's viewport.
4+
5+
**Author:** "Christopher Blum":http://twitter.com/ChristopherBlum
6+
**Original idea and concept by:** "Remy Sharp":http://remysharp.com/2009/01/26/element-in-view-event-plugin/
7+
**Forked from:** "https://github.com/zuk/jquery.inview/":https://github.com/zuk/jquery.inview/
8+
**Requires:** jQuery 1.8+
9+
10+
## Usage
11+
12+
The script makes use of the new $.contains method - so it will only work with jQuery 1.8 upwards. If you need to use it with older versions of jQuery, drop a comment, and I'll post an alternative.
13+
14+
The event will only fire when the element comes in to view of the viewport, and out of view. It won't keep firing if the user scrolls and the element remains in view.
15+
16+
The variable after the event argument indicates the visible state in the viewport.
17+
18+
$('div').on('inview', function(event, isInView) {
19+
if (isInView) {
20+
// element is now visible in the viewport
21+
} else {
22+
// element has gone out of viewport
23+
}
24+
});
25+
26+
To stop listening for the event - simply unbind:
27+
28+
$('div').off('inview');
29+
30+
Remember you can also bind once:
31+
32+
$('div').one('inview', fn);
33+
34+
Live events
35+
36+
Yep, inview events can also be used with .on/.delegate methods.
37+
*Please note that this could slow down your app when the selector is too complex and/or matches a huge set of elements.*
38+
The following code snippet only loads images when they appear in the browser's viewport.
39+
40+
// Assuming that all images have set the 'data-src' attribute instead of the 'src'attribute
41+
$("body").on("inview", "img[data-src]", function() {
42+
var $this = $(this);
43+
$this.attr("src", $this.attr("data-src"));
44+
// Remove it from the set of matching elements in order to avoid that the handler gets re-executed
45+
$this.removeAttr("data-src");
46+
});
47+
48+
## Use cases
49+
50+
* Reduce http requests and traffic on server by loading assets (images, javascript, html, ...) only when they are visible to the user
51+
* Endless scrolling (twitter-like)
52+
* Tracking (eg. to see whether a user has read an entire article)
53+
* ...
54+
55+
## Browser Compatibility
56+
57+
The Test Suite succeeds in the following browsers that were tested:
58+
59+
* Firefox 3+
60+
* Safari 3+
61+
* Chrome 7+
62+
* Opera 10+
63+
* IE 6+
64+
* Mobile Safari on iPad 4.2.2

README.textile

Lines changed: 0 additions & 64 deletions
This file was deleted.

0 commit comments

Comments
 (0)