diff --git a/.gitignore b/.gitignore index 2117f73..e69de29 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +0,0 @@ -.tm_sync.config \ No newline at end of file diff --git a/README.textile b/README.textile index 2c36685..74c5094 100644 --- a/README.textile +++ b/README.textile @@ -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 @@ -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 } @@ -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. @@ -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 \ No newline at end of file +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+ \ No newline at end of file diff --git a/example/advanced.html b/example/advanced.html deleted file mode 100644 index 7fb0618..0000000 --- a/example/advanced.html +++ /dev/null @@ -1,46 +0,0 @@ - - -
-Scroll to the right and to the bottom
- - visible! - visible! - - - - \ No newline at end of file diff --git a/jquery.inview.js b/jquery.inview.js deleted file mode 100644 index b2c29c1..0000000 --- a/jquery.inview.js +++ /dev/null @@ -1,136 +0,0 @@ -/** - * author Christopher Blum - * - based on the idea of Remy Sharp, http://remysharp.com/2009/01/26/element-in-view-event-plugin/ - * - forked from http://github.com/zuk/jquery.inview/ - */ -(function ($) { - var inviewObjects = {}, viewportSize, viewportOffset, - d = document, w = window, documentElement = d.documentElement, expando = $.expando, timer; - - $.event.special.inview = { - add: function(data) { - inviewObjects[data.guid + "-" + this[expando]] = { data: data, $element: $(this) }; - - // Use setInterval in order to also make sure this captures elements within - // "overflow:scroll" elements or elements that appeared in the dom tree due to - // dom manipulation and reflow - // old: $(window).scroll(checkInView); - // - // By the way, iOS (iPad, iPhone, ...) seems to not execute, or at least delays - // intervals while the user scrolls. Therefore the inview event might fire a bit late there - // - // Don't waste cycles with an interval until we get at least one element that - // has bound to the inview event. - if (!timer && !$.isEmptyObject(inviewObjects)) { - timer = setInterval(checkInView, 250); - } - }, - - remove: function(data) { - try { delete inviewObjects[data.guid + "-" + this[expando]]; } catch(e) {} - - // Clear interval when we no longer have any elements listening - if ($.isEmptyObject(inviewObjects)) { - clearInterval(timer); - timer = null; - } - } - }; - - function getViewportSize() { - var mode, domObject, size = { height: w.innerHeight, width: w.innerWidth }; - - // if this is correct then return it. iPad has compat Mode, so will - // go into check clientHeight/clientWidth (which has the wrong value). - if (!size.height) { - mode = d.compatMode; - if (mode || !$.support.boxModel) { // IE, Gecko - domObject = mode === 'CSS1Compat' ? - documentElement : // Standards - d.body; // Quirks - size = { - height: domObject.clientHeight, - width: domObject.clientWidth - }; - } - } - - return size; - } - - function getViewportOffset() { - return { - top: w.pageYOffset || documentElement.scrollTop || d.body.scrollTop, - left: w.pageXOffset || documentElement.scrollLeft || d.body.scrollLeft - }; - } - - function checkInView() { - var $elements = $(), elementsLength, i = 0; - - $.each(inviewObjects, function(i, inviewObject) { - var selector = inviewObject.data.selector, - $element = inviewObject.$element; - $elements = $elements.add(selector ? $element.find(selector) : $element); - }); - - elementsLength = $elements.length; - if (elementsLength) { - viewportSize = viewportSize || getViewportSize(); - viewportOffset = viewportOffset || getViewportOffset(); - - for (; i