forked from Prinzhorn/skrollr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathskrollr-0.3.3.min.js
1 lines (1 loc) · 5.23 KB
/
skrollr-0.3.3.min.js
1
/*! skrollr v0.3.3 https://github.com/Prinzhorn/skrollr | free to use under terms of MIT license */(function(a,b,c){function z(e){var h=this;e=e||{};if(e.easing)for(var j in e.easing)x[j]=e.easing[j];h.scale=e.scale||1,h.listeners={beforerender:e.beforerender||d,render:e.render||d},h.skrollables=[],h.maxKeyFrame=e.maxKeyFrame||0,h.dir="down",h.lastTop=-1,h.curTop=0;var k=b.getElementsByTagName("*"),l=[];for(var n=0;n<k.length;n++){var o=k[n],p=[];if(!o.attributes)continue;for(var q=0;q<o.attributes.length;q++){var r=o.attributes[q],s=r.name.match(m);if(s!==null){var t,u;t=(s[2]|0)*h.scale,u={frame:t,props:r.value},p.push(u),s[1]==="-end"&&l.push(u),t>h.maxKeyFrame&&(h.maxKeyFrame=t)}}p.length&&(h.skrollables.push({element:o,keyFrames:p}),A(o,i))}for(var n=0;n<l.length;n++){var u=l[n];u.frame=h.maxKeyFrame-u.frame}for(var n=0;n<h.skrollables.length;n++){var v=h.skrollables[n];v.keyFrames.sort(function(a,b){return a.frame-b.frame}),h._parseProps(v),h._fillProps(v)}var w=b.createElement("div"),y=w.style;y.width="1px",y.position="absolute",y.right=y.top=y.zIndex="0",g.appendChild(w);var z=function(){y.height=h.maxKeyFrame+f.clientHeight+"px"};return a.addEventListener?a.addEventListener("resize",z,!1):a.attachEvent("onresize",z),z(),h._render(),w=l=e=c,h}var d=function(){},e=Object.prototype.hasOwnProperty,f=b.documentElement,g=b.body,h="hidden",i="skrollable",j="linear",k=a.requestAnimationFrame||a.webkitRequestAnimationFrame||a.mozRequestAnimationFrame||a.oRequestAnimationFrame||a.msRequestAnimationFrame||function(a){setTimeout(a,1e3/30)},l=/^\s*(.*)\s$/,m=/^data(-end)?-?(\d+)?$/,n=/:|;/g,o=/^([a-z-]+)\[(\w+)\]$/,p=/-([a-z])/g,q=/(:?\+|-)?[\d.]+/g,r=/rgba?\(\s*-?\d+\s*,\s*-?\d+\s*,\s*-?\d+/g,s=/[a-z-]+-gradient/g,t=/^O|Moz|webkit|ms/,u,v;for(var w in g.style)if(u=w.match(t))break;u=(u||[""])[0],v="-"+u.toLowerCase()+"-",t=c;var x={begin:function(){return 0},end:function(){return 1},linear:function(a){return a},quadratic:function(a){return a*a},cubic:function(a){return a*a*a*a},swing:function(a){return-Math.cos(a*Math.PI)/2+.5},bounce:function(a){var b;switch(!0){case a<=.5083:b=3;break;case a<=.8489:b=9;break;case a<=.96208:b=27;break;case a<=.99981:b=91;break;default:return 1}return 1-Math.abs(3*Math.cos(a*b*1.028)/b)}},y={};z.prototype.setScrollTop=function(a){pageYOffset=g.scrollTop=f.scrollTop=a},z.prototype.on=function(a,b){this.listeners[a]=b||d},z.prototype.off=function(a){this.listeners[a]=d},z.prototype._calcSteps=function(a,b){var c=this,d=a.keyFrames;if(b<d[0].frame)A(a.element,h);else if(b>d[d.length-1].frame){B(a.element,h);var f=d[d.length-1],g;for(var i in f.props)e.call(f.props,i)&&(g=c._interpolateString(f.props[i].value),c._setStyle(a.element,i,g))}else{B(a.element,h);for(var j=0;j<d.length-1;j++)if(b>=d[j].frame&&b<=d[j+1].frame){var k=d[j],l=d[j+1];for(var i in k.props)if(e.call(k.props,i)){var m=(b-k.frame)/(l.frame-k.frame);m=k.props[i].easing(m);var g=c._calcInterpolation(k.props[i].value,l.props[i].value,m);g=c._interpolateString(g),c._setStyle(a.element,i,g)}break}}},z.prototype._render=function(){var b=this;b.curTop=a.pageYOffset||f.scrollTop||g.scrollTop||0,b.curTop<0&&(b.curTop=0);if(b.lastTop!==b.curTop){b.dir=b.curTop>=b.lastTop?"down":"up";var c={curTop:b.curTop,lastTop:b.lastTop,maxTop:b.maxKeyFrame,direction:b.dir},d=b.listeners.beforerender.call(b,c);if(d!==!1){for(var e=0;e<b.skrollables.length;e++)b._calcSteps(b.skrollables[e],b.curTop);b.lastTop=b.curTop,b.listeners.render.call(b,c)}}k(function(){b._render()})},z.prototype._parseProps=function(a){var b=this;for(var c=0;c<a.keyFrames.length;c++){var d=a.keyFrames[c],e=d.props.split(n),f,g,h;d.props={};for(var i=0;i<e.length-1;i+=2)f=e[i],g=e[i+1],h=f.match(o),h!==null?(f=h[1],h=h[2]):h=j,g=b._parseProp(g),d.props[f]={value:g,easing:x[h]}}},z.prototype._parseProp=function(a){var b=[];return r.lastIndex=0,a=a.replace(r,function(a){return a.replace(q,function(a){return a/255*100+"%"})}),s.lastIndex=0,a=a.replace(s,function(a){return v+a}),a=a.replace(q,function(a){return b.push(+a),"?"}),b.unshift(a),b},z.prototype._fillProps=function(a){var b={},c=this;for(var d=0;d<a.keyFrames.length;d++)c._fillPropForFrame(a.keyFrames[d],b);b={};for(var d=a.keyFrames.length-1;d>=0;d--)c._fillPropForFrame(a.keyFrames[d],b)},z.prototype._fillPropForFrame=function(a,b){for(var c in b)e.call(a.props,c)||(a.props[c]=b[c]);for(var c in a.props)b[c]=a.props[c]},z.prototype._calcInterpolation=function(a,b,c){if(a.length!==b.length)throw"Can't interpolate between \""+a[0]+'" and "'+b[0]+'"';var d=[a[0]];for(var e=1;e<a.length;e++)d[e]=a[e]+(b[e]-a[e])*c;return d},z.prototype._interpolateString=function(a){var b=1;return a[0].replace(/\?/g,function(){return a[b++]})},z.prototype._setStyle=function(a,b,c){var d=a.style;b=b.replace(p,function(a,b){return b.toUpperCase()}).replace("-","");if(b==="zIndex")d[b]=""+(c|0);else try{d[u+b.slice(0,1).toUpperCase()+b.slice(1)]=c,d[b]=c}catch(e){}if(y.setStyle)for(var f=0;f<y.setStyle.length;f++)y.setStyle[0].call(this,a,b,c)};var A=function(a,b){C(a.className).indexOf(C(b))===-1&&(a.className=(a.className+" "+b).replace(l,"$1"))},B=function(a,b){a.className=C(a.className).replace(C(b),"").replace(l,"$1")},C=function(a){return" "+a+" "};a.skrollr={init:function(a){return new z(a)},plugin:function(a,b){y[a]?y[a].push(b):y[a]=[b]},VERSION:"0.3.3"}})(window,document);