katiek

Super-easy Heatmap with jQuery

Dec 28th, 2011
384
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
jQuery 0.71 KB | None | 0 0
  1. // This will look at all elements of class sel, check out their 'score' attributes, and set the background color accordingly (lightest = score 0, darkest = score 100)
  2. // Individual element looks like this: <div class='sel otherClass' score=50'>Content</div>
  3.  
  4. $('.sel').css('background-color',function(){
  5.    var how = $(this).attr('score');
  6.    if(how>100 || how<0) how=100;
  7.    how = how/2;
  8.    how = 100-how;
  9.    return "hsl("+180+","+50+"%,"+how+"%)";
  10. });
  11.  
  12. // If you have separately-defined scale in a .css, eg:
  13. //   .scale40{ background-color:hsl(180,50%,80%); }
  14. //   .scale39{ background-color:hsl(180,50%,80.5%); }
  15. // Then can use this:
  16.  
  17. $('.sel').addClass(function(){
  18.    return 'scale'+$(this).attr('score');
  19. });
Advertisement
Add Comment
Please, Sign In to add comment