好的,我创建了一个耐力容器,彼此之间具有三个耐力条,当您单击“攻击”按钮时,一些耐力会从第一个耐力条上移走。但是,如果您一直单击第一个耐力条,直到其耗尽至0,它将开始消耗第二个耐力条,依此类推。现在,这就是它打算发生的方式,但是我无法使其那样运行。这是它的jsfiddle,每当我多次单击攻击按钮时,耐力条就会滞后,并且一旦耐力条完成,它仍然会耗尽耐力条并重新进行自我修复。您必须尝试jsfiddle才能理解我在说什么。

的HTML

<body>
<div id="container">
<div class="hBar"> <div class="health"></div></div><!--hbar -->
<div class="sBar">
<div class="s3">
<div class="s2">
<div class="s1">

</div><!--s1 -->
</div><!--s2 -->
</div><!--s3 -->
</div><!--sBar -->
<div class="attack">attack</div>
</div><!--container -->
</body>


的CSS

*{ margin:0px; padding:0px;}
.hBar{width:400px; height:40px; border:1px solid grey; margin-bottom:20px;}
.health{background:green;width:100%; height:100%;}
.sBar{ width:400px; height:40px; border:1px solid grey; margin-bottom:20px;}
.s3{ width:100%; height:100%; background:red;}
.s2{width:100%; height:100%; background:orange;}
.s1{width:100%; height:100%; background:yellow;}
#container{background:white; width:80%; padding:10px; margin:auto;}
body{background:#CCC;}
.attack{ border-radius:90px; border:black solid 1px; height:75px; width:75px; text-align:center; line-height:75px;}
.attack:hover{cursor:pointer;}


Java脚本

$(document).ready(function () {


    // one , two, and three variables are collecting the stamina bars
    var one = $('.s1');
    var two = $('.s2');
    var three = $('.s3');
    var oneWidth = one.width();
    var twoWidth = two.width();
    var threeWidth = three.width();
    var stam = $('.sBar').width();
    var damage;
    var subtractHealth;
    var num;

    $('.attack').click(function () {


        // timer is supposed to be the variable for a setInterval function
        let timer;
        // damage is supposed to take away the stamina
        damage = 100;

        // This function is supposed to stop the interval and the animation done on the
        // stamina bars
        function stopAnimate() {
            clearInterval(timer);
            one.stop();
            two.stop();
            three.stop();

        }


        // if the  first and the second stamina bar is below 0, then add subtract the width to .s3
        if (oneWidth <= 0 && twoWidth <= 0) {
            subtractHealth = $('.s3').width() - damage;

            three.animate({
                'width': subtractHealth
            }, 'fast');

            // if the first stamina bar is less than 0, the subtract the width of .s2
        } else if (oneWidth <= 0) {


            subtractHealth = $('.s2').width() - damage;

            two.animate({
                'width': subtractHealth
            }, 'fast');

            // if the first stamina bar is not below 0 then run the content in this
        } else {



            subtractHealth = $('.s1').width() - damage;

            one.animate({
                'width': subtractHealth
            }, 'fast');


        }



// regenerates all the three stamina bars with the animate method
        function regenerate(stam1, stam2, stam3) {




            stam1.animate({
                'width': stam
            }, 1000, function () {

                if (stam1.width() == stam) {


                    stam2.animate({
                        'width': stam
                    }, 1000, function () {

                        if (stam2.width() == stam) {


                            stam3.animate({
                                'width': stam
                            }, 1000)

                        }// if stam2


                    });//stam2.animate


                }//if stam.width()


            })//stam1.animate

            setTimeout(stopAnimate(), 5000); //end function


        }; //end regenerate

        // run setInterval and assign the method to timer
        timer = setInterval(regenerate(one, two, three), 1000);




    }); //end click

}); //end ready

最佳答案

我不是100%肯定我会满足您的要求,但是如果没有,我认为您应该能够修改此代码以获得所需的结果。如果您需要其他帮助,请发表评论,我很乐意看到我能做些什么。



  var staminaMax = 1000;
  var staminaCurrent = staminaMax;
  var staminaHealInterval;
  var staminaTick = 100;
  var staminHealPerTick = 10;

  var $sBar3 = $(".sBar .sBarStatus.s3");
  var $sBar2 = $(".sBar .sBarStatus.s2");
  var $sBar1 = $(".sBar .sBarStatus.s1");

  var healStamina = function() {
    staminaCurrent = Math.min(staminaCurrent + staminHealPerTick, staminaMax);
    var rawPct = staminaCurrent / staminaMax;

    var s1Pct = (function() {
      if (rawPct <= (2 / 3)) { return 0; }
      return (rawPct - (2 / 3)) / (1 / 3);
    })();

    var s2Pct = (function() {
      if (rawPct <= (1 / 3)) { return 0; }
      if (rawPct >= (2 / 3)) { return 1; }
      return (rawPct - (1 / 3)) / (1 / 3);
    })();

    var s3Pct = (function() {
      if (rawPct >= (1 / 3)) { return 1; }
      return (rawPct - (0 / 3)) / (1 / 3);
    })();

    $sBar3.css("width", 100 * s3Pct + "%");
    $sBar2.css("width", 100 * s2Pct + "%");
    $sBar1.css("width", 100 * s1Pct + "%");

    if (staminaCurrent >= staminaMax) {
      clearInterval(staminaHealInterval);
      staminaHealInterval = null;
    }
  };

  var dingStamina = function(amount) {
    staminaCurrent = Math.max(staminaCurrent - amount, 0);
    if (!staminaHealInterval) {
      staminaHealInterval = setInterval(healStamina, staminaTick);
    }
  }

  $('.attack').click(function() {
    dingStamina(100);
  });

* {
  margin: 0px;
  padding: 0px;
}
.hBar {
  width: 400px;
  height: 10px;
  border: 1px solid grey;
  margin-bottom: 20px;
}
.health {
  background: green;
  width: 100%;
  height: 100%;
}
.sBar {
  width: 400px;
  height: 10px;
  border: 1px solid grey;
  margin-bottom: 20px;
  position: relative;
}
.sBar .sBarStatus {
  position: absolute;
  width: 100%;
  height: 100%;
}
.s3 {
  background: red;
}
.s2 {
  background: orange;
}
.s1 {
  background: yellow;
}
#container {
  background: white;
  width: 80%;
  padding: 10px;
  margin: auto;
}
body {
  background: #CCC;
}
.attack {
  border-radius: 90px;
  border: black solid 1px;
  height: 75px;
  width: 75px;
  text-align: center;
  line-height: 75px;
}
.attack:hover {
  cursor: pointer;
}

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div id="container">
  <div class="hBar">
    <div class="health"></div>
  </div>

  <div class="sBar">
    <div class="sBarStatus s3"></div>
    <div class="sBarStatus s2"></div>
    <div class="sBarStatus s1"></div>
  </div>

  <div class="attack">attack</div>
</div>

关于javascript - 如何创建3个嵌套的耐力条,从最后到第一个重新生成,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/37646418/

10-11 05:52