将额外的两个变量添加到计数模拟器函数中

时间:2016-11-18 08:50:29

标签: javascript gsap

我创建了以下小提琴https://jsfiddle.net/jnoweb/v421zzbe/2/

目前它有一个变量,使所有三个ID计数到20:

  var game = {score:0},

scoreDisplay = [
  document.getElementById("score1"),
  document.getElementById("score2"),
  document.getElementById("score3")];

function add20() {
  TweenLite.to(game, 1, {score:"+=20", roundProps:"score", onUpdate:updateHandler, ease:Linear.easeNone});
}

function updateHandler() {
  scoreDisplay.forEach(function(display) {
    display.innerHTML = game.score;
  });
}

add20(); 

我想更改此设置,以便每个ID都计入不同的值,例如16,18和20!

有谁知道如何实现这一目标?

3 个答案:

答案 0 :(得分:0)

 var game = {
      score1: 0,
      score2: 0,
      score3: 0
  },

scoreDisplay = [
  document.getElementById("score1"),
  document.getElementById("score2"),
  document.getElementById("score3")
];

function add(scoreIndex, numToAdd) {
  TweenLite.to(game, 1, {score:("+="+numToAdd), roundProps: ("score" + scoreIndex), onUpdate:updateHandler, ease:Linear.easeNone});
}

function updateHandler() {
  scoreDisplay.forEach(function(display, i) {
     var key = ("score" + (i+1))
     display.innerHTML = game[key];
  });
}

add(1 , 16); 
add(2 , 18); 
add(3 , 20); 

答案 1 :(得分:0)

这个怎么样?

  var game = {
    score1:0,
    score2:0,
    score3:0
  },

    scoreDisplay = [
      document.getElementById("score1"),
      document.getElementById("score2"),
      document.getElementById("score3")];

    function add20() {
      TweenLite.to(game, 1, {score1:"+=16", score2:"+=18", score3:"+=20", roundProps:"score", onUpdate:updateHandler, ease:Linear.easeNone});
    }

    function updateHandler() {
      scoreDisplay.forEach(function(display, key) {
        var score = scoreDisplay[key].id;
        display.innerHTML = game[score];
      });
    }

    add20(); 

https://jsfiddle.net/hundma4g/

答案 2 :(得分:0)

这是更优雅,通用,可配置的解决方案。



  function ScoreDisplay(id, increment) {
    this.elm = document.getElementById(id);
    this.inc = increment;
    this.game = {score: 0};
  }
  
  ScoreDisplay.prototype = {
     update: function(){
        this.elm.innerHTML = this.game.score;
     }
  };
  
  var scoreDisplay = [];
  scoreDisplay.push(new ScoreDisplay('score1', 16));
  scoreDisplay.push(new ScoreDisplay('score2', 18));
  scoreDisplay.push(new ScoreDisplay('score3', 20));

    function addScore() {
      scoreDisplay.forEach(function(sd) {
      TweenLite.to(sd.game, 1, {score: "+=" + sd.inc, roundProps:"score", onUpdate:sd.update.bind(sd), ease:Linear.easeNone});
      });
    }

    addScore();   

 #score1 {
            position: relative;
            font-size: 30px;
            font-weight: bold;
            padding: 20px;
            text-align: center;
            background-color: transparent;
            color: $white;
            border-radius: 20px 20px;
            top: -11px;
            left: -42px;
          }
          #score2 {
            position: relative;
            font-size: 30px;
            font-weight: bold;
            padding: 20px;
            text-align: center;
            background-color: transparent;
            color: $white;
            border-radius: 20px 20px;
            top: -11px;
            left: -42px;
          }

          #score3 {
            position: relative;
            font-size: 30px;
            font-weight: bold;
            padding: 20px;
            text-align: center;
            background-color: transparent;
            color: $white;
            border-radius: 20px 20px;
            top: -11px;
            left: -42px;
          }

<!--TweenLite/TweenMax GSAP-->
<script src="https://cdnjs.cloudflare.com/ajax/libs/gsap/latest/plugins/CSSPlugin.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/gsap/latest/easing/EasePack.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/gsap/latest/TweenLite.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/gsap/latest/TimelineLite.min.js"></script>

<script src="http://cdnjs.cloudflare.com/ajax/libs/gsap/latest/plugins/RoundPropsPlugin.min.js"></script>

      <div id="prodArrow"></div>
      <div id="prodCount">
        <div id="score1"></div>
          
      </div>

      <div id="testArrow"></div>
      <div id="testCount">
        <div id="score2"></div>

      </div>

      <div id="devArrow"></div>
      <div id="devCount">
        <div id="score3"></div>

      </div>
&#13;
&#13;
&#13;