Commits

Chris Nanney  committed 4dd5e5e

Added custom ID prefix option.

  • Participants
  • Parent commits af8783c
  • Tags v0.5

Comments (0)

Files changed (2)

File js/flipcounter.js

 		auto: true,
 		tFH: 39,
 		bFH: 64,
-		fW: 53
+		fW: 53,
+		idPre: 'counter'
 	};
 	
 	var o = options || {},
 	}
 	
 	function animateDigit(n, oldDigit, newDigit){
-		var speed, step = 0, w,
+		var speed, step = 0, w, a,
 		bp = [
 			'-' + o.fW + 'px -' + (oldDigit * o.tFH) + 'px',
 			(o.fW * -2) + 'px -' + (oldDigit * o.tFH) + 'px',
 		function animate(){
 			if (step < 7){
 				w = step < 3 ? 't' : 'b';
-				doc.getElementById("counter_" + w + "_d" + n).style.backgroundPosition = bp[step];
+				a = doc.getElementById(o.idPre + "_" + w + "_d" + n);
+				if (a) a.style.backgroundPosition = bp[step];
 				step++;
 				if (step != 3) setTimeout(animate, speed);
 				else animate();
 
 	// Adds new digit
 	function addDigit(len, digit){
-		console.log('add');
 		var li = Number(len) - 1;
 		newDigit = doc.createElement("ul");
 		newDigit.className = 'cd';
-		newDigit.id = 'counter_d' + li;
-		newDigit.innerHTML = '<li class="t" id="counter_t_d' + li + '"></li><li class="b" id="counter_b_d' + li + '"></li>';
+		newDigit.id = o.idPre + '_d' + li;
+		newDigit.innerHTML = '<li class="t" id="' + o.idPre + '_t_d' + li + '"></li><li class="b" id="' + o.idPre + '_b_d' + li + '"></li>';
 		
 		if (li % 3 == 0){
 			newComma = doc.createElement("ul");
 		}
 		
 		div.insertBefore(newDigit, div.firstChild);
-		doc.getElementById("counter_t_d" + li).style.backgroundPosition = '0 -' + (digit * o.tFH) + 'px';
-		doc.getElementById("counter_b_d" + li).style.backgroundPosition = '0 -' + (digit * o.bFH) + 'px';
+		doc.getElementById(o.idPre + "_t_d" + li).style.backgroundPosition = '0 -' + (digit * o.tFH) + 'px';
+		doc.getElementById(o.idPre + "_b_d" + li).style.backgroundPosition = '0 -' + (digit * o.bFH) + 'px';
 	}
 	
 	// Removes digit
 	function removeDigit(id){
-		var remove = doc.getElementById("counter_d" + id);
+		var remove = doc.getElementById(o.idPre + "_d" + id);
 		div.removeChild(remove);
 
 		// Check for leading comma
 		for (i = 0; i < count; i++){
 			newDigit = doc.createElement("ul");
 			newDigit.className = 'cd';
-			newDigit.id = 'counter_d' + i;
-			newDigit.innerHTML = newDigit.innerHTML = '<li class="t" id="counter_t_d' + i + '"></li><li class="b" id="counter_b_d' + i + '"></li>';
+			newDigit.id = o.idPre + '_d' + i;
+			newDigit.innerHTML = newDigit.innerHTML = '<li class="t" id="' + o.idPre + '_t_d' + i + '"></li><li class="b" id="' + o.idPre + '_b_d' + i + '"></li>';
 			div.insertBefore(newDigit, div.firstChild);
 			if (bit != (count) && bit % 3 == 0){
 				newComma = doc.createElement("ul");
 		// Sets them to the right number
 		var digits = splitToArray(initial);
 		for (i = 0; i < count; i++){
-			doc.getElementById("counter_t_d" + i).style.backgroundPosition = '0 -' + (digits[i] * o.tFH) + 'px';
-			doc.getElementById("counter_b_d" + i).style.backgroundPosition = '0 -' + (digits[i] * o.bFH) + 'px';
+			doc.getElementById(o.idPre + "_t_d" + i).style.backgroundPosition = '0 -' + (digits[i] * o.tFH) + 'px';
+			doc.getElementById(o.idPre + "_b_d" + i).style.backgroundPosition = '0 -' + (digits[i] * o.bFH) + 'px';
 		}
 		// Do first animation
 		if (o.auto === true) nextCount = setTimeout(doCount, o.pace);

File js/flipcounter.min.js

  * Licensed under MIT
  * http://www.opensource.org/licenses/mit-license.php
  */
-var flipCounter=function(w,D){function s(){l=a.value;a.value+=a.inc;h=a.value;q(l,h);a.auto===!0&&(m=setTimeout(s,a.pace))}function x(c,e,b){var d=a.value,f=typeof e=="undefined"?!1:e,g=typeof b=="undefined"?1:b;f===!0&&g--;d!=c?(l=a.value,a.auto=!0,d+a.inc<=c&&g!=0?d+=a.inc:d=c,a.value=d,h=a.value,q(l,h),m=setTimeout(function(){x(c,f,g)},a.pace)):a.auto=!1}function q(c,e){var b,c=c.toString(),e=e.toString();t=y(c);u=y(e);if(e.length>c.length)for(b=e.length-c.length;b>0;){var d=e.length-b+1,f=u[e.length-
-b];console.log("add");d=Number(d)-1;i=j.createElement("ul");i.className="cd";i.id="counter_d"+d;i.innerHTML='<li class="t" id="counter_t_d'+d+'"></li><li class="b" id="counter_b_d'+d+'"></li>';if(d%3==0)n=j.createElement("ul"),n.className="cd",n.innerHTML='<li class="s"></li>',k.insertBefore(n,k.firstChild);k.insertBefore(i,k.firstChild);j.getElementById("counter_t_d"+d).style.backgroundPosition="0 -"+f*a.tFH+"px";j.getElementById("counter_b_d"+d).style.backgroundPosition="0 -"+f*a.bFH+"px";b--}if(e.length<
-c.length)for(b=c.length-e.length;b>0;){f=j.getElementById("counter_d"+(c.length-b));k.removeChild(f);f=k.firstChild.firstChild;if((" "+f.className+" ").indexOf(" s ")>-1)f=f.parentNode,k.removeChild(f);b--}for(b=0;b<t.length;b++)u[b]!=t[b]&&E(b,t[b],u[b])}function E(c,e,b){function d(){if(g<7)h=g<3?"t":"b",j.getElementById("counter_"+h+"_d"+c).style.backgroundPosition=i[g],g++,g!=3?setTimeout(d,f):d()}var f,g=0,h,i=["-"+a.fW+"px -"+e*a.tFH+"px",a.fW*-2+"px -"+e*a.tFH+"px","0 -"+b*a.tFH+"px","-"+a.fW+
+var flipCounter=function(w,D){function s(){l=a.value;a.value+=a.inc;h=a.value;q(l,h);a.auto===!0&&(m=setTimeout(s,a.pace))}function x(c,e,b){var d=a.value,f=typeof e=="undefined"?!1:e,g=typeof b=="undefined"?1:b;f===!0&&g--;d!=c?(l=a.value,a.auto=!0,d+a.inc<=c&&g!=0?d+=a.inc:d=c,a.value=d,h=a.value,q(l,h),m=setTimeout(function(){x(c,f,g)},a.pace)):a.auto=!1}function q(c,e){var b,c=c.toString(),e=e.toString();t=y(c);u=y(e);if(e.length>c.length)for(b=e.length-c.length;b>0;){var d=u[e.length-b],f=Number(e.length-
+b+1)-1;i=k.createElement("ul");i.className="cd";i.id=a.idPre+"_d"+f;i.innerHTML='<li class="t" id="'+a.idPre+"_t_d"+f+'"></li><li class="b" id="'+a.idPre+"_b_d"+f+'"></li>';if(f%3==0)n=k.createElement("ul"),n.className="cd",n.innerHTML='<li class="s"></li>',j.insertBefore(n,j.firstChild);j.insertBefore(i,j.firstChild);k.getElementById(a.idPre+"_t_d"+f).style.backgroundPosition="0 -"+d*a.tFH+"px";k.getElementById(a.idPre+"_b_d"+f).style.backgroundPosition="0 -"+d*a.bFH+"px";b--}if(e.length<c.length)for(b=
+c.length-e.length;b>0;){d=k.getElementById(a.idPre+"_d"+(c.length-b));j.removeChild(d);d=j.firstChild.firstChild;if((" "+d.className+" ").indexOf(" s ")>-1)d=d.parentNode,j.removeChild(d);b--}for(b=0;b<t.length;b++)u[b]!=t[b]&&E(b,t[b],u[b])}function E(c,e,b){function d(){if(g<7){h=g<3?"t":"b";if(i=k.getElementById(a.idPre+"_"+h+"_d"+c))i.style.backgroundPosition=j[g];g++;g!=3?setTimeout(d,f):d()}}var f,g=0,h,i,j=["-"+a.fW+"px -"+e*a.tFH+"px",a.fW*-2+"px -"+e*a.tFH+"px","0 -"+b*a.tFH+"px","-"+a.fW+
 "px -"+e*a.bFH+"px",a.fW*-2+"px -"+b*a.bFH+"px",a.fW*-3+"px -"+b*a.bFH+"px","0 -"+b*a.bFH+"px"];if(a.auto===!0&&a.pace<=300)switch(c){case 0:f=a.pace/6;break;case 1:f=a.pace/5;break;case 2:f=a.pace/4;break;case 3:f=a.pace/3;break;default:f=a.pace/1.5}else f=80;f=f>80?80:f;d()}function y(a){for(var e=[],b=0;b<a.length;b++)A=a.length-(b+1),B=a.length-b,e[b]=a.substring(A,B);return e}function C(a,e,b,d,f){var g={result:!0};g.cond1=a/e>=1?!0:!1;g.cond2=e*b<=a?!0:!1;g.cond3=Math.abs(e*b-a)<=10?!0:!1;g.cond4=
-Math.abs(e*d-f)<=100?!0:!1;g.cond5=e*d<=f?!0:!1;if(g.cond1&&g.cond2&&g.cond4&&g.cond5){a=Math.abs(a-e*b)+Math.abs(e*d-f);if(o.q===null)o.q=a;if(a<=o.q)o.pace=d,o.inc=b}for(b=1;b<=5;b++)if(g["cond"+b]===!1)g.result=!1;return g}function p(a){return!isNaN(parseFloat(a))&&isFinite(a)}function z(){clearTimeout(m);m=null}var v={value:0,inc:1,pace:1E3,auto:!0,tFH:39,bFH:64,fW:53},a=D||{},j=window.document,k=w&&w!=""?j.getElementById(w):j.getElementById("counter"),r;for(r in v)a[r]=r in a?a[r]:v[r];var t=
-[],u=[],A,B,l,h,m=null,i,n,o={q:null,pace:0,inc:0};this.setValue=function(c){if(p(c))l=a.value,h=c,a.value=c,q(l,h);return this};this.setIncrement=function(c){a.inc=p(c)?c:v.inc;return this};this.setPace=function(c){a.pace=p(c)?c:v.pace;return this};this.setAuto=function(c){if(c&&!a.atuo)a.auto=!0,s();if(!c&&a.auto)m&&z(),a.auto=!1;return this};this.step=function(){a.auto||s();return this};this.add=function(c){if(p(c))l=a.value,a.value+=c,h=a.value,q(l,h);return this};this.subtract=function(c){if(p(c))l=
-a.value,a.value-=c,a.value>=0?h=a.value:(h="0",a.value=0),q(l,h);return this};this.incrementTo=function(c,e,b){m&&z();if(typeof e!="undefined"){var e=p(e)?e*1E3:1E4,b=typeof b!="undefined"&&p(b)?b:a.pace,d=typeof c!="undefined"&&p(c)?c-a.value:0,f,g,h,i=0;o.q=null;b=e/d>b?Math.round(e/d/10)*10:b;f=Math.floor(e/b);g=Math.floor(d/f);h=C(d,f,g,b,e);if(d>0){for(;h.result===!1&&i<100;)b+=10,f=Math.floor(e/b),g=Math.floor(d/f),h=C(d,f,g,b,e),i++;i==100?(a.inc=o.inc,a.pace=o.pace):(a.inc=g,a.pace=b);x(c,
-!0,f)}}else x(c)};this.getValue=function(){return a.value};this.stop=function(){m&&z();return this};(function(c){var c=c.toString(),e=c.length,b=1,d;for(d=0;d<e;d++){i=j.createElement("ul");i.className="cd";i.id="counter_d"+d;i.innerHTML=i.innerHTML='<li class="t" id="counter_t_d'+d+'"></li><li class="b" id="counter_b_d'+d+'"></li>';k.insertBefore(i,k.firstChild);if(b!=e&&b%3==0)n=j.createElement("ul"),n.className="cd",n.innerHTML='<li class="s"></li>',k.insertBefore(n,k.firstChild);b++}c=y(c);for(d=
-0;d<e;d++)j.getElementById("counter_t_d"+d).style.backgroundPosition="0 -"+c[d]*a.tFH+"px",j.getElementById("counter_b_d"+d).style.backgroundPosition="0 -"+c[d]*a.bFH+"px";a.auto===!0&&(m=setTimeout(s,a.pace))})(a.value)};
+Math.abs(e*d-f)<=100?!0:!1;g.cond5=e*d<=f?!0:!1;if(g.cond1&&g.cond2&&g.cond4&&g.cond5){a=Math.abs(a-e*b)+Math.abs(e*d-f);if(o.q===null)o.q=a;if(a<=o.q)o.pace=d,o.inc=b}for(b=1;b<=5;b++)if(g["cond"+b]===!1)g.result=!1;return g}function p(a){return!isNaN(parseFloat(a))&&isFinite(a)}function z(){clearTimeout(m);m=null}var v={value:0,inc:1,pace:1E3,auto:!0,tFH:39,bFH:64,fW:53,idPre:"counter"},a=D||{},k=window.document,j=w&&w!=""?k.getElementById(w):k.getElementById("counter"),r;for(r in v)a[r]=r in a?
+a[r]:v[r];var t=[],u=[],A,B,l,h,m=null,i,n,o={q:null,pace:0,inc:0};this.setValue=function(c){if(p(c))l=a.value,h=c,a.value=c,q(l,h);return this};this.setIncrement=function(c){a.inc=p(c)?c:v.inc;return this};this.setPace=function(c){a.pace=p(c)?c:v.pace;return this};this.setAuto=function(c){if(c&&!a.atuo)a.auto=!0,s();if(!c&&a.auto)m&&z(),a.auto=!1;return this};this.step=function(){a.auto||s();return this};this.add=function(c){if(p(c))l=a.value,a.value+=c,h=a.value,q(l,h);return this};this.subtract=
+function(c){if(p(c))l=a.value,a.value-=c,a.value>=0?h=a.value:(h="0",a.value=0),q(l,h);return this};this.incrementTo=function(c,e,b){m&&z();if(typeof e!="undefined"){var e=p(e)?e*1E3:1E4,b=typeof b!="undefined"&&p(b)?b:a.pace,d=typeof c!="undefined"&&p(c)?c-a.value:0,f,g,h,i=0;o.q=null;b=e/d>b?Math.round(e/d/10)*10:b;f=Math.floor(e/b);g=Math.floor(d/f);h=C(d,f,g,b,e);if(d>0){for(;h.result===!1&&i<100;)b+=10,f=Math.floor(e/b),g=Math.floor(d/f),h=C(d,f,g,b,e),i++;i==100?(a.inc=o.inc,a.pace=o.pace):
+(a.inc=g,a.pace=b);x(c,!0,f)}}else x(c)};this.getValue=function(){return a.value};this.stop=function(){m&&z();return this};(function(c){var c=c.toString(),e=c.length,b=1,d;for(d=0;d<e;d++){i=k.createElement("ul");i.className="cd";i.id=a.idPre+"_d"+d;i.innerHTML=i.innerHTML='<li class="t" id="'+a.idPre+"_t_d"+d+'"></li><li class="b" id="'+a.idPre+"_b_d"+d+'"></li>';j.insertBefore(i,j.firstChild);if(b!=e&&b%3==0)n=k.createElement("ul"),n.className="cd",n.innerHTML='<li class="s"></li>',j.insertBefore(n,
+j.firstChild);b++}c=y(c);for(d=0;d<e;d++)k.getElementById(a.idPre+"_t_d"+d).style.backgroundPosition="0 -"+c[d]*a.tFH+"px",k.getElementById(a.idPre+"_b_d"+d).style.backgroundPosition="0 -"+c[d]*a.bFH+"px";a.auto===!0&&(m=setTimeout(s,a.pace))})(a.value)};