123

Pen Settings

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Code Indentation

     

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

            
              <div class="container">
  <div class="col-md-12">
    <div class="row">
      <h2>Simple Health Bar</h2>
      <p>This was a simple experiment to generate a bar that interacts to a percentage input and whether I could all control the css using the same calculations.</p>
    </div>
    <div class="area">
      <div class="row">
        <div class="add-damage btn" type="button">Damage</div>
        <div class="add-heal btn" type="button">Heal</div>
      </div>
      <div class="total"></div>
      <div class="message-box col-md-12">Make your move!</div>
      <div class="health-box">
        <div class="health-bar-red"></div>
        <div class="health-bar-blue"></div>
        <div class="health-bar"></div>
        <div class="health-bar-text"></div>
      </div>
    </div>
  </div>
</div>
            
          
!
            
              .area {
  padding: 10px;
  text-align: center;
}

h2 {
  padding: 5px;
  text-align: center;
}

p {
  width: 500px;
  padding: 1%;
  margin: auto;
}

.btn {
  text-align: center;
  width: 100px;
  background-color: #A8C2C4;
  border: 2px solid #97aeb0;
  padding: 20px;
  display: inline-block;
  margin: 30px 10px;
  user-select: none;
  -moz-user-select: none;
  -khtml-user-select: none;
  -webkit-user-select: none;
  -o-user-select: none;
  cursor: pointer;
}

.health-box {
  background-color: #ccc;
  height: 30px;
  width: 500px;
  margin: 0 auto;
  border: solid 1px #aaa;
}

.health-bar {
  background-color: #007f00;
  width: inherit;
  height: 28px;
  position: relative;
  bottom: 56px;
}

.health-bar-red {
  width: 100%;
  height: 100%;
  background-color: #cc0000;
}

.health-bar-blue {
  width: 100%;
  height: 100%;
  background-color: #3bd3df;
  bottom: 28px;
  position: relative;
}

.health-bar-text {
  position: relative;
  bottom: 80px;
}

.message-box {
  text-align: center;
  padding: 5px;
}

.total,
.message-box {
  font-size: 16px;
  margin: 5px;
}
            
          
!
            
              $ = jQuery;
var maxHealth = 500,
  curHealth = maxHealth;
$('.total').html(maxHealth + "/" + maxHealth);
$(".health-bar-text").html("100%");
$(".health-bar").css({
  "width": "100%"
});
$(".add-damage").click(function() {
  if (curHealth == 0) {
    $('.message-box').html("Is this the end??");
  } else {
    var damage = Math.floor((Math.random() * 100) + 50);
    $(".health-bar-red, .health-bar").stop();
    curHealth = curHealth - damage;
    if (curHealth < 0) {
      curHealth = 0;
      restart();
    } else {
      $('.message-box').html("You took " + damage + " points of damage!");
    }
    applyChange(curHealth);
  }
});
$(".add-heal").click(function() {
  if (curHealth == maxHealth) {
    $('.message-box').html("You are already at full health");
  } else {
    var heal = Math.floor((Math.random() * 100) + 5);
    $(".health-bar-red, .health-bar-blue, .health-bar").stop();
    curHealth = curHealth + heal;
    if (curHealth > maxHealth) {
      curHealth = maxHealth;
      $('.message-box').html("You're at full health");
    } else if (curHealth == 0) {
      $('.message-box').html("Miraculously! You regained your health by " + heal + " points and get back on to your feet!");
    } else {
      $('.message-box').html("You regained your health by " + heal + " points!");
    }
    applyChange(curHealth);
  }
});

function applyChange(curHealth) {
  var a = curHealth * (100 / maxHealth);
  $(".health-bar-text").html(Math.round(a) + "%");
  $(".health-bar-red").animate({
    'width': a + "%"
  }, 700);
  $(".health-bar").animate({
    'width': a + "%"
  }, 500);
  $(".health-bar-blue").animate({
    'width': a + "%"
  }, 300);
  $('.total').html(curHealth + "/" + maxHealth);
}

function restart() {
  //Was going to have a game over/restart function here. 
  $('.health-bar-red, .health-bar');
  $('.message-box').html("You've been knocked down! Thing's are looking bad.");
}
            
          
!
999px
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.

Console