Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

hidden elements issue #42

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +0,0 @@
.tm_sync.config
72 changes: 8 additions & 64 deletions README.textile
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
h1. Element 'inview' Event Plugin
h1. jQuery inview2

Event that is fired as soon as an element appears in the user's viewport.
Modified version of protonet's jquery.inview to use the more modern (and much faster) getBoundingClientRect() method.
Props to him for his work on the original inview, but I found myself in need of a better-performing solution.

*Author:* "Christopher Blum":http://twitter.com/ChristopherBlum
*Original idea and concept by:* "Remy Sharp":http://remysharp.com/2009/01/26/element-in-view-event-plugin/
*Forked from:* "https://github.com/zuk/jquery.inview/":https://github.com/zuk/jquery.inview/
*Demo* (loading lolcats when they scroll into view): "http://tifftiff.de/jquery.inview/example/live_event.html":http://tifftiff.de/jquery.inview/example/live_event.html
*Author:* Matthew Frey
*Forked from:* "https://github.com/protonet/jquery.inview/":https://github.com/protonet/jquery.inview/
*Requires:* jQuery 1.4+

h2. Usage
Expand All @@ -15,19 +15,10 @@ The script makes use of the new $.contains method - so it will only work with jQ
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.

The variable after the event argument indicates the visible state in the viewport.
The third variable visiblePartX detects which horizontal part of the element is visible to the user (possible values: left, right, both)
The fourth variable visiblePartY detects which vertical part of the element is visible to the user (possible values: top, bottom, both)

bc.. $('div').bind('inview', function(event, isInView, visiblePartX, visiblePartY) {
bc.. $('div').bind('inview', function(event, isInView) {
if (isInView) {
// element is now visible in the viewport
if (visiblePartY == 'top') {
// top part of element is visible
} else if (visiblePartY == 'bottom') {
// bottom part of element is visible
} else {
// whole part of element is visible
}
} else {
// element has gone out of viewport
}
Expand Down Expand Up @@ -55,41 +46,9 @@ bc.. // Assuming that all images have set the 'data-src' attribute instead of t
$this.removeAttr("data-src");
});

h2. More complex example

This way we can attach inview to some DIV in our code to, for example, detect when it FULLY readed by user (user sees it's bottom and top) and only after 1 seconds of view, so after we call some out stats function or whatever

bc.. $(someMyOneDiv).bind('inview', function(e, isInView, visiblePartX, visiblePartY) {
var elem = $(this);

if (elem.data('inviewtimer')) {
clearTimeout(elem.data('inviewtimer'));
elem.removeData('inviewtimer');
}

if (isInView) {
elem.data('inviewtimer', setTimeout(function() {
if (visiblePartY == 'top') {
elem.data('seenTop', true);
} else if (visiblePartY == 'bottom') {
elem.data('seenBottom', true);
} else {
elem.data('seenTop', true);
elem.data('seenBottom', true);
}

if (elem.data('seenTop') && elem.data('seenBottom')) {
elem.unbind('inview');
// here we will do WHAT WHE NEED (for ex. Call Ajax stats collector)
...
}
}, 1000));
}
});

h2. How it works

An interval in the background checks the position of the elements against the viewport dimensions and the scroll position.
An interval in the background checks the position of the bounding rectangle of elements against the viewport dimensions and the scroll position.

However, I wanted to create a utility that would only check the elements that were registered under the 'inview' event, i.e. I didn't want to keep checking the element list if we unbind from the event.

Expand All @@ -102,22 +61,7 @@ h2. Use cases
* Reduce http requests and traffic on server by loading assets (images, javascript, html, ...) only when they are visible to the user
* Endless scrolling (twitter-like)
* Tracking (eg. to see whether a user has read an entire article)
* ...

h2. Browser Compatibility

h4. The Test Suite succeeds in the following browsers that were tested:

* Firefox 3+
* Safari 3+
* Chrome 7+
* Opera 10+
* IE 6+
* Mobile Safari on iPad 4.2.2
* Fennec 4b on Android 2.2
* Opera Mobile 10.1b on Android 2.2

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
Haven't had a chance to test this yet, but the cornerstone of this plugin, getBoundingClientRect, should work in any modern browser as well as IE7+
46 changes: 0 additions & 46 deletions example/advanced.html

This file was deleted.

98 changes: 0 additions & 98 deletions example/live_event.html

This file was deleted.

49 changes: 0 additions & 49 deletions example/simple_example.html

This file was deleted.

Loading