lundi 27 juin 2016

How to make dynamically change its width according to data from array


So, I have three divs .inner which fills the outer div (a barlike chart if you want), and a target which represents a dotted line. I fetch two datas from an array: $target = $data->data[2][32][3];

Which is 9.83% in this case. and the inner div data is:

$clicks = $data->data[1][32][3];

Which is 7.15% in this case. So I've set the target to width 100% in the outer div. I am trying to make the .inner div procentually fill the outer div up until it reaches the target. CSS: CSS:

.outer,
.inner,
.target {
  height: 14px;
  margin-bottom: 5px;
}
.outer {
  background-color: #cccccc;
  width: 80%;
  margin: 20px auto;
  position: relative;
  font-size: 10px;
  line-height: 14px;
  font-family: sans-serif;
}
.inner {
  background-color: green;
  position: absolute;
  z-index: 1;
  text-align: right;
  width: calc(80% / 100 * <?php $clicks ?>);
  border-right: 2px solid black;
}
.inner:after {
  content: '<?php echo number_format((float)$clicks, 2, '.', ''); ?>%';
  display: inline-block;
  left: 10px;
  position: relative;
  height: 100%;
  top: -14px;
}
.target {
  background-color: transparent;
  width: 19px;
  position: absolute;
  z-index: 2;
  color: black;
  text-align: right;
  border-right: 2px dotted black;
}
.target:after {
  content: 'Target: <?php echo number_format((float)$target, 2, '.', ''); ?>%';
  display: inline-block;
  left: 28px;
  position: relative;
  height: 100%;
  top: 14px;
}

HTML:

<div class="outer" style="width: "<?php echo $base; ?>"> 
    <div class="target" style="width: 80%">
    </div>                   
    <div class="inner" style=" width: calc(<?php echo $bar_width; ?> / 100 * (<?php echo $clicks; ?> / <?php echo $base; ?> * 100))">
    </div>
</div>

PHP:

<?php
    $target = $data->data[2][32][3];
    $bar_width = '80%'; 
    $clicks = $data->data[1][32][3];
    $base   = max($target, $clicks);

?>

I've attached some images of how it should look like: enter image description here

The problem is that I try to make the target dynamically go backwards on the outer div when the inner exceeded it .inner > .target. Any ideas on how should I accomplish this?


Aucun commentaire:

Enregistrer un commentaire