I want to create a simple HTML rating bar. My rating bar got has a dynamic number of containers. For example 5:
And I need to calculate the color values [first+2,last-1]. The first and last colors are given.
$(document).ready(function() {
ratingElements = $(".ratingEle"); // Find all rating containers
ratingColors.push(initialColorDefault); // if not selected, set the container to gray // index 0
ratingColors.push(initialColorMin); // red color // index 1
// !! Calculate the missing colors here !!
// from index 2 to ratingColors.length - 1
// ratingColors.push("#" + hexValue);
updateRatingBar(0); // set all containers to gray
$(ratingElements).each(function(index, element) { // add some event handlers to the containers
var ele = $(element);
var i = index + 1;
ele.click(function() {
setRating(i);
});
ele.hover(function() {
updateRatingBar(i);
});
});
});
var ratingElements;
var ratingColors = [];
var initialColorDefault = "#cccccc"; // pre set colors
var initialColorMin = "#ff0000";
var initialColorMax = "#80ff00";
function setRating(currentValue) { // click handler
$("#ratingOutput").html(currentValue + " / " + ratingElements.length);
updateRatingBar(currentValue);
}
function updateRatingBar(currentValue) { // set colors
$(ratingElements).each(function(index, element) {
var i = index;
if (i > currentValue) {
i = 0;
}
$(element).css('background-color', ratingColors[i]);
});
}
.ratingEle {
float: left;
cursor: pointer;
height: 10px;
width: 30px;
margin-left: 5px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="rating">
<div>
<div class="ratingEle"></div>
<div class="ratingEle"></div>
<div class="ratingEle"></div>
<div class="ratingEle"></div>
<div class="ratingEle"></div>
</div>
<div>
<p id="ratingOutput"></p>
</div>
</div>
So as you can see, I need to calculate the colors to make it work. I set a missing part in my JS file and need help, calculating it.
I don't know if I should go for hex or rgb values.
When hovering over the containers the colors should get updated automatically. When clicking a container, it should set a value.
But I already archieved this I just need to calculate the missing color range.