火箭军大屏html静态页面
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.

61 lines
2.1 KiB

  1. function DigitRoll(opts) {
  2. this.container=document.querySelector(opts.container);
  3. this.width=opts.width || 1;
  4. if (!this.container) {
  5. throw Error('no container');
  6. }
  7. this.container.style.overflow='hidden';
  8. this.rollHeight=parseInt(getComputedStyle(this.container).height);
  9. if (this.rollHeight<1) {
  10. this.container.style.height='36px';
  11. this.rollHeight=32;
  12. }
  13. this.setWidth();
  14. }
  15. DigitRoll.prototype = {
  16. roll: function (n) {
  17. var self=this;
  18. this.number=parseInt(n)+'';
  19. if (this.number.length<this.width) {
  20. this.number=new Array(this.width - this.number.length + 1).join('0') + this.number;
  21. } else if (this.number.length>this.width) {
  22. this.width=this.number.length;
  23. this.setWidth();
  24. }
  25. Array.prototype.forEach.call(this.container.querySelectorAll('.num'), function (item,i) {
  26. var currentNum=parseInt(item.querySelector('div:last-child').innerHTML);
  27. var goalNum=parseInt(self.number[i]);
  28. var gapNum=0;
  29. var gapStr='';
  30. if (currentNum==goalNum) {
  31. return ;
  32. }else if(currentNum<goalNum) {
  33. gapNum=goalNum-currentNum;
  34. for (var j=currentNum; j<goalNum+1; j++) {
  35. gapStr+='<div>'+j+'</div>'
  36. }
  37. } else {
  38. gapNum=10-currentNum+goalNum;
  39. for (var j=currentNum; j<10; j++) {
  40. gapStr+='<div>'+j+'</div>'
  41. }
  42. for (var j=0; j<goalNum+1; j++) {
  43. gapStr+='<div>'+j+'</div>'
  44. }
  45. }
  46. item.style.cssText += '-webkit-transition-duration:0s;-webkit-transform:translateY(0)';
  47. item.innerHTML = gapStr;
  48. setTimeout(function () {
  49. item.style.cssText+='-webkit-transition-duration:1s;-webkit-transform:translateY(-'+(self.rollHeight+2)*gapNum+'px)';
  50. },50)
  51. })
  52. },
  53. setWidth:function (n) {
  54. n=n||this.width;
  55. var str='';
  56. for (var i=0; i<n; i++) {
  57. str+='<div class="num" style="float:left; margin-right:2px; line-height:'+this.rollHeight+'px"><div>0</div></div>';
  58. }
  59. this.container.innerHTML=str;
  60. }
  61. }