评分,显示星星数,按照小数显示星星亮度;希望得到你的优化! 背景图片和前端代码,(来自于网络) 1. [代码] [PHP]代码 /** * 根据评分数,显示几颗星星 * @param $score int 评分 * @dat
背景图片和前端代码,(来自于网络)
1. [代码][PHP]代码
/** * 根据评分数,显示几颗星星 * @param $score int 评分 * @date 2016/07/14 * @return int */ protected function handle_score($score = 0) { $offset = 90; if(!is_numeric($score) || $score <= 0) { return $offset; } //判断是否是小数 if(is_float($score)) { $score_arr = explode('.',round($score,1)); $score_int = $score_arr[1]; //取小数点后数字 } //星星背景偏移量计算 if($score > 0 && $score < 1) { $offset = 90 - $score_int; } elseif($score == 1) { $offset = 70 - $score_int + 5; } elseif($score > 1 && $score < 2) { $offset = 70 - $score_int + 1; } elseif($score == 2) { $offset = 50 - $score_int + 6; } elseif($score > 2 && $score < 3) { $offset = 50 - $score_int + 2; } elseif($score == 3) { $offset = 30 - $score_int + 7; } elseif($score > 3 && $score < 4) { $offset = 30 - $score_int + 3; } elseif($score == 4) { $offset = 10 - $score_int + 8; } elseif($score >= 4 && $score < 5) { $offset = 10 - $score_int + 4; } elseif($score >= 5) { $offset = 0; } return $offset; } //以下是view 视图代码 $love_val = $this->handle_score(4.9); <img src="星星图片地址" style="background-color:#ddd; background-image:url('背景图片地址'; background-repeat:no-repeat; background-position: $love_val px;"/>