// javascript document (function($) { //custom scroll replacement to allow for interval-based 'polling' //rathar than checking on every pixel var uniquecntr = 0; $.fn.scrolled = function(waittime, fn) { if (typeof waittime === 'function') { fn = waittime; waittime = 200; } var tag = 'scrolltimer' + uniquecntr++; this.scroll(function() { var self = $(this); var timer = self.data(tag); if (timer) { cleartimeout(timer); } timer = settimeout(function() { self.removedata(tag); fn.call(self[0]); }, waittime); self.data(tag, timer); }); }; $.fn.aniview = function(options) { //some default settings. animatethreshold controls the trigger point //for animation and is subtracted from the bottom of the viewport. var settings = $.extend({ animatethreshold: 0, scrollpollinterval: 20 }, options); //keep the matched elements in a variable for easy reference var collection = this; //cycle through each matched element and wrap it in a block/div //and then proceed to fade out the inner contents of each matched element $(collection).each(function(index, element) { $(element).wrap('
'); $(element).css('opacity', 0); }); /** * returns boolean representing whether element's top is coming into bottom of viewport * * @param htmldomelement element the current element to check */ function enteringviewport(element) { var elementoffset = $(element).offset(); var elementtop = elementoffset.top + $(element).scrolltop(); var elementbottom = elementoffset.top + $(element).scrolltop() + $(element).height(); var viewportbottom = $(window).scrolltop() + $(window).height(); return (elementtop < (viewportbottom - settings.animatethreshold)) ? true : false; } //cycle through each matched element to make sure any which should be animated into view, //are animated on page load rather than needing to wait for initial 'scrolled' event $(collection).each(function(index, element) { var elementparentcontainer = $(element).parent('.av-container'); if ($(element).is('[av-animation]') && !$(elementparentcontainer).hasclass('av-visible') && enteringviewport(elementparentcontainer)) { $(element).css('opacity', 1); $(elementparentcontainer).addclass('av-visible'); $(element).addclass('animated ' + $(element).attr('av-animation')); } }); //enable the scrolled event timer to watch for elements coming into the viewport //from the bottom. default polling time is 20 ms. this can be changed using //'scrollpollinterval' from the user visible options $(window).scrolled(settings.scrollpollinterval, function() { $(collection).each(function(index, element) { var elementparentcontainer = $(element).parent('.av-container'); if ($(element).is('[av-animation]') && !$(elementparentcontainer).hasclass('av-visible') && enteringviewport(elementparentcontainer)) { $(element).css('opacity', 1); $(elementparentcontainer).addclass('av-visible'); $(element).addclass('animated ' + $(element).attr('av-animation')); } }); }); }; })(jquery);