You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

1 lines
11 KiB

!function(a,b,c,d){a.fn.visibility=function(e){var f,g=a(this),h=g.selector||"",i=(new Date).getTime(),j=[],k=arguments[0],l="string"==typeof k,m=[].slice.call(arguments,1);return g.each(function(){var g,n=a.extend(!0,{},a.fn.visibility.settings,e),o=(n.className,n.namespace),p=(n.error,"."+o),q="module-"+o,r=a(b),s=a(this),t=a(n.context),u=(s.offsetParent(),s.selector||"",s.data(q)),v=b.requestAnimationFrame||b.mozRequestAnimationFrame||b.webkitRequestAnimationFrame||b.msRequestAnimationFrame||function(a){setTimeout(a,0)},w=this;g={initialize:function(){g.verbose("Initializing visibility",n),g.reset(),g.save.position(),g.bindEvents(),g.instantiate(),"image"==n.type&&g.setup.image(),v(g.checkVisibility)},instantiate:function(){g.verbose("Storing instance of module",g),u=g,s.data(q,g)},destroy:function(){g.verbose("Destroying previous module"),s.off(p).removeData(q)},bindEvents:function(){g.verbose("Binding visibility events to scroll and resize"),r.on("resize"+p,g.event.refresh),t.on("scroll"+p,g.event.scroll)},event:{refresh:function(){v(g.refresh)},scroll:function(){g.verbose("Scroll position changed"),n.throttle?(clearTimeout(g.timer),g.timer=setTimeout(g.checkVisibility,n.throttle)):v(g.checkVisibility)}},precache:function(b,d){b instanceof Array||(b=[b]);for(var e=b.length,f=0,g=[],h=c.createElement("img"),i=function(){f++,f>=b.length&&a.isFunction(d)&&d()};e--;)h=c.createElement("img"),h.onload=i,h.onerror=i,h.src=b[e],g.push(h)},setup:{image:function(){var a=s.data("src");a&&(g.verbose("Lazy loading image",a),g.topVisible(function(){g.precache(a,function(){g.set.image(a),n.onTopVisible=!1})}))}},set:{image:function(b){var c=g.cache.screen.bottom<g.cache.element.top;s.attr("src",b),c?s.show():a.fn.transition!==d?s.transition(n.transition,n.duration):s.fadeIn(n.duration)}},refresh:function(){g.debug("Refreshing constants (element width/height)"),g.reset(),g.save.position(),g.checkVisibility(),a.proxy(n.onRefresh,w)()},reset:function(){g.verbose("Reseting all cached values"),g.cache={occurred:{},screen:{},element:{}}},checkVisibility:function(){g.verbose("Checking visibility of element",g.cache.element),g.save.scroll(),g.save.direction(),g.save.screenCalculations(),g.save.elementCalculations(),g.passed(),g.passingReverse(),g.topVisibleReverse(),g.bottomVisibleReverse(),g.topPassedReverse(),g.bottomPassedReverse(),g.passing(),g.topVisible(),g.bottomVisible(),g.topPassed(),g.bottomPassed()},passed:function(b,c){var e=g.get.elementCalculations();if(b!==d&&c!==d)n.onPassed[b]=c;else{if(b!==d)return g.get.pixelsPassed(b)>e.pixelsPassed;e.passing&&a.each(n.onPassed,function(a,b){e.bottomVisible||e.pixelsPassed>g.get.pixelsPassed(a)?g.execute(b,a):n.once||g.remove.occurred(b)})}},passing:function(a){var b=g.get.elementCalculations(),c=a||n.onPassing,e="passing";return a&&(g.debug("Adding callback for passing",a),n.onPassing=a),b.passing?g.execute(c,e):n.once||g.remove.occurred(e),a!==d?b.passing:void 0},topVisible:function(a){var b=g.get.elementCalculations(),c=a||n.onTopVisible,e="topVisible";return a&&(g.debug("Adding callback for top visible",a),n.onTopVisible=a),b.topVisible?g.execute(c,e):n.once||g.remove.occurred(e),a===d?b.topVisible:void 0},bottomVisible:function(a){var b=g.get.elementCalculations(),c=a||n.onBottomVisible,e="bottomVisible";return a&&(g.debug("Adding callback for bottom visible",a),n.onBottomVisible=a),b.bottomVisible?g.execute(c,e):n.once||g.remove.occurred(e),a===d?b.bottomVisible:void 0},topPassed:function(a){var b=g.get.elementCalculations(),c=a||n.onTopPassed,e="topPassed";return a&&(g.debug("Adding callback for top passed",a),n.onTopPassed=a),b.topPassed?g.execute(c,e):n.once||g.remove.occurred(e),a===d?b.onTopPassed:void 0},bottomPassed:function(a){var b=g.get.elementCalculations(),c=a||n.onBottomPassed,e="bottomPassed";return a&&(g.debug("Adding callback for bottom passed",a),n.onBottomPassed=a),b.bottomPassed?g.execute(c,e):n.once||g.remove.occurred(e),a===d?b.bottomPassed:void 0},passingReverse:function(a){var b=g.get.elementCalculations(),c=a||n.onPassingReverse,e="passingReverse";return a&&(g.debug("Adding callback for passing reverse",a),n.onPassingReverse=a),b.passing?n.once||g.remove.occurred(e):g.get.occurred("passing")&&g.execute(c,e),a!==d?!b.passing:void 0},topVisibleReverse:function(a){var b=g.get.elementCalculations(),c=a||n.onTopVisibleReverse,e="topVisibleReverse";return a&&(g.debug("Adding callback for top visible reverse",a),n.onTopVisibleReverse=a),b.topVisible?n.once||g.remove.occurred(e):g.get.occurred("topVisible")&&g.execute(c,e),a===d?!b.topVisible:void 0},bottomVisibleReverse:function(a){var b=g.get.elementCalculations(),c=a||n.onBottomVisibleReverse,e="bottomVisibleReverse";return a&&(g.debug("Adding callback for bottom visible reverse",a),n.onBottomVisibleReverse=a),b.bottomVisible?n.once||g.remove.occurred(e):g.get.occurred("bottomVisible")&&g.execute(c,e),a===d?!b.bottomVisible:void 0},topPassedReverse:function(a){var b=g.get.elementCalculations(),c=a||n.onTopPassedReverse,e="topPassedReverse";return a&&(g.debug("Adding callback for top passed reverse",a),n.onTopPassedReverse=a),b.topPassed?n.once||g.remove.occurred(e):g.get.occurred("topPassed")&&g.execute(c,e),a===d?!b.onTopPassed:void 0},bottomPassedReverse:function(a){var b=g.get.elementCalculations(),c=a||n.onBottomPassedReverse,e="bottomPassedReverse";return a&&(g.debug("Adding callback for bottom passed reverse",a),n.onBottomPassedReverse=a),b.bottomPassed?n.once||g.remove.occurred(e):g.get.occurred("bottomPassed")&&g.execute(c,e),a===d?!b.bottomPassed:void 0},execute:function(b,c){var d=g.get.elementCalculations(),e=g.get.screenCalculations();b=b||!1,b&&(n.continuous?(g.debug("Callback being called continuously",c,d),a.proxy(b,w)(d,e)):g.get.occurred(c)||(g.debug("Conditions met",c,d),a.proxy(b,w)(d,e))),g.save.occurred(c)},remove:{occurred:function(a){a?g.cache.occurred[a]!==d&&g.cache.occurred[a]===!0&&(g.debug("Callback can now be called again",a),g.cache.occurred[a]=!1):g.cache.occurred={}}},save:{occurred:function(a){a&&(g.cache.occurred[a]===d||g.cache.occurred[a]!==!0)&&(g.verbose("Saving callback occurred",a),g.cache.occurred[a]=!0)},scroll:function(){g.cache.scroll=t.scrollTop()+n.offset},direction:function(){var a,b=g.get.scroll(),c=g.get.lastScroll();return a=b>c&&c?"down":c>b&&c?"up":"static",g.cache.direction=a,g.cache.direction},elementPosition:function(){var b=g.get.screenSize();return g.verbose("Saving element position"),a.extend(g.cache.element,{margin:{top:parseInt(s.css("margin-top"),10),bottom:parseInt(s.css("margin-bottom"),10)},fits:w.height<b.height,offset:s.offset(),width:s.outerWidth(),height:s.outerHeight()}),g.cache.element},elementCalculations:function(){var b=g.get.screenCalculations(),c=g.get.elementPosition();n.includeMargin?a.extend(g.cache.element,{top:c.offset.top-c.margin.top,bottom:c.offset.top+c.height+c.margin.bottom}):a.extend(g.cache.element,{top:c.offset.top,bottom:c.offset.top+c.height}),a.extend(g.cache.element,{topVisible:b.bottom>=c.top,topPassed:b.top>=c.top,bottomVisible:b.bottom>=c.bottom,bottomPassed:b.top>=c.bottom,pixelsPassed:0,percentagePassed:0}),a.extend(g.cache.element,{visible:g.cache.element.topVisible||g.cache.element.bottomVisible,passing:g.cache.element.topPassed&&!g.cache.element.bottomPassed,hidden:!g.cache.element.topVisible&&!g.cache.element.bottomVisible}),g.cache.element.passing&&(g.cache.element.pixelsPassed=b.top-c.top,g.cache.element.percentagePassed=(b.top-c.top)/c.height),g.verbose("Updated element calculations",g.cache.element)},screenCalculations:function(){var b=t.scrollTop()+n.offset;return g.cache.scroll===d&&(g.cache.scroll=t.scrollTop()+n.offset),g.save.direction(),a.extend(g.cache.screen,{top:b,bottom:b+g.cache.screen.height}),g.cache.screen},screenSize:function(){g.verbose("Saving window position"),g.cache.screen={height:t.height()}},position:function(){g.save.screenSize(),g.save.elementPosition()}},get:{pixelsPassed:function(a){var b=g.get.elementCalculations();return a.search("%")>-1?b.height*(parseInt(a,10)/100):parseInt(a,10)},occurred:function(a){return g.cache.occurred!==d?g.cache.occurred[a]||!1:!1},direction:function(){return g.cache.direction===d&&g.save.direction(),g.cache.direction},elementPosition:function(){return g.cache.element===d&&g.save.elementPosition(),g.cache.element},elementCalculations:function(){return g.cache.element===d&&g.save.elementCalculations(),g.cache.element},screenCalculations:function(){return g.cache.screen===d&&g.save.screenCalculations(),g.cache.screen},screenSize:function(){return g.cache.screen===d&&g.save.screenSize(),g.cache.screen},scroll:function(){return g.cache.scroll===d&&g.save.scroll(),g.cache.scroll},lastScroll:function(){return g.cache.screen===d?(g.debug("First scroll event, no last scroll could be found"),!1):g.cache.screen.top}},setting:function(b,c){if(a.isPlainObject(b))a.extend(!0,n,b);else{if(c===d)return n[b];n[b]=c}},internal:function(b,c){if(a.isPlainObject(b))a.extend(!0,g,b);else{if(c===d)return g[b];g[b]=c}},debug:function(){n.debug&&(n.performance?g.performance.log(arguments):(g.debug=Function.prototype.bind.call(console.info,console,n.name+":"),g.debug.apply(console,arguments)))},verbose:function(){n.verbose&&n.debug&&(n.performance?g.performance.log(arguments):(g.verbose=Function.prototype.bind.call(console.info,console,n.name+":"),g.verbose.apply(console,arguments)))},error:function(){g.error=Function.prototype.bind.call(console.error,console,n.name+":"),g.error.apply(console,arguments)},performance:{log:function(a){var b,c,d;n.performance&&(b=(new Date).getTime(),d=i||b,c=b-d,i=b,j.push({Element:w,Name:a[0],Arguments:[].slice.call(a,1)||"","Execution Time":c})),clearTimeout(g.performance.timer),g.performance.timer=setTimeout(g.performance.display,100)},display:function(){var b=n.name+":",c=0;i=!1,clearTimeout(g.performance.timer),a.each(j,function(a,b){c+=b["Execution Time"]}),b+=" "+c+"ms",h&&(b+=" '"+h+"'"),(console.group!==d||console.table!==d)&&j.length>0&&(console.groupCollapsed(b),console.table?console.table(j):a.each(j,function(a,b){console.log(b.Name+": "+b["Execution Time"]+"ms")}),console.groupEnd()),j=[]}},invoke:function(b,c,e){var g,h,i,j=u;return c=c||m,e=w||e,"string"==typeof b&&j!==d&&(b=b.split(/[\. ]/),g=b.length-1,a.each(b,function(c,e){var f=c!=g?e+b[c+1].charAt(0).toUpperCase()+b[c+1].slice(1):b;if(a.isPlainObject(j[f])&&c!=g)j=j[f];else{if(j[f]!==d)return h=j[f],!1;if(!a.isPlainObject(j[e])||c==g)return j[e]!==d?(h=j[e],!1):!1;j=j[e]}})),a.isFunction(h)?i=h.apply(e,c):h!==d&&(i=h),a.isArray(f)?f.push(i):f!==d?f=[f,i]:i!==d&&(f=i),h}},l?(u===d&&g.initialize(),g.invoke(k)):(u!==d&&g.destroy(),g.initialize())}),f!==d?f:this},a.fn.visibility.settings={name:"Visibility",namespace:"visibility",debug:!1,verbose:!1,performance:!0,offset:0,includeMargin:!1,context:b,throttle:!1,type:!1,transition:"fade in",duration:500,onPassed:{},onPassing:!1,onTopVisible:!1,onBottomVisible:!1,onTopPassed:!1,onBottomPassed:!1,onPassingReverse:!1,onTopVisibleReverse:!1,onBottomVisibleReverse:!1,onTopPassedReverse:!1,onBottomPassedReverse:!1,once:!0,continuous:!1,onRefresh:function(){},onScroll:function(){},error:{method:"The method you called is not defined."}}}(jQuery,window,document);