$(function() {
let stars = $('.rating_wrap .star');
let starsCurrent = $('.stars_current');
let message = $('.rating_message');
stars.on('click', function () {
let currentStar = $(this);
if (currentStar.data('current_width') === 20) {
starsCurrent.css("width",currentStar.data('current_width') + '%');
starsCurrent.attr('data-rating',currentStar.data('rating_value'));
starsCurrent.data('rating',currentStar.data('rating_value'));
message.text(currentStar.data('message'));
} else if (currentStar.data('current_width') === 40) {
starsCurrent.css("width",currentStar.data('current_width') + '%');
starsCurrent.attr('data-rating',currentStar.data('rating_value'));
starsCurrent.data('rating',currentStar.data('rating_value'));
message.text(currentStar.data('message'));
} else if (currentStar.data('current_width') === 60) {
starsCurrent.css("width",currentStar.data('current_width') + '%');
starsCurrent.attr('data-rating',currentStar.data('rating_value'));
starsCurrent.data('rating',currentStar.data('rating_value'));
message.text(currentStar.data('message'));
} else if (currentStar.data('current_width') === 80) {
starsCurrent.css("width",currentStar.data('current_width') + '%');
starsCurrent.attr('data-rating',currentStar.data('rating_value'));
starsCurrent.data('rating',currentStar.data('rating_value'));
message.text(currentStar.data('message'));
} else if (currentStar.data('current_width') === 100) {
starsCurrent.css("width",currentStar.data('current_width') + '%');
starsCurrent.attr('data-rating',currentStar.data('rating_value'));
starsCurrent.data('rating',currentStar.data('rating_value'));
message.text(currentStar.data('message'));
}
});
stars.on('mouseover', function () {
let currentStar = $(this);
if (currentStar.data('current_width') === 20) {
starsCurrent.css("width",currentStar.data('current_width') + '%');
message.text(currentStar.data('message'));
} else if (currentStar.data('current_width') === 40) {
starsCurrent.css("width",currentStar.data('current_width') + '%');
message.text(currentStar.data('message'));
} else if (currentStar.data('current_width') === 60) {
starsCurrent.css("width",currentStar.data('current_width') + '%');
message.text(currentStar.data('message'));
} else if (currentStar.data('current_width') === 80) {
starsCurrent.css("width",currentStar.data('current_width') + '%');
message.text(currentStar.data('message'));
} else if (currentStar.data('current_width') === 100) {
starsCurrent.css("width",currentStar.data('current_width') + '%');
message.text(currentStar.data('message'));
}
});
stars.on('mouseout', function () {
if (starsCurrent.data('rating') === 0) {
starsCurrent.css("width",0);
message.text('Без оценки');
} else {
let width = stars.filter('[data-rating_value='+starsCurrent.data('rating')+']').data('current_width');
let messageVal = stars.filter('[data-rating_value='+starsCurrent.data('rating')+']').data('message');
starsCurrent.css("width",width+'%');
message.text(messageVal);
}
});
});
.rating {
float: left;
position: relative;
width: 150px;
height: 24px;
font-size: 0;
line-height: 0;
background: url("https://i.sstatic.net/ceEGP.jpg") 0 -26px no-repeat;
}
.rating .star {
position: relative;
z-index: 1;
margin: 0;
padding: 0;
height: 24px;
display: inline-block;
width: 20%;
background: none;
cursor: pointer;
border-radius: 0;
}
.rating .stars_current {
position: absolute;
left: 0;
top: 0;
bottom: 0;
display: block;
width: 0;
background: url("https://i.sstatic.net/ceEGP.jpg") 0 0 no-repeat;
}
.rating_message {
margin: 0 0 0 13px;
float: left;
position: relative;
padding: 0 0 0 30px;
color: #999999;
}
.rating_message:before {
content: "—";
position: absolute;
left: 0;
top: 0;
}
<div class="rating_wrap clearfix">
<div class="rating">
<span class="star" data-current_width="20" data-rating_value="1" data-message="Очень плохо"></span>
<span class="star" data-current_width="40" data-rating_value="2" data-message="Плохо"></span>
<span class="star" data-current_width="60" data-rating_value="3" data-message="Нормально"></span>
<span class="star" data-current_width="80" data-rating_value="4" data-message="Хорошо"></span>
<span class="star" data-current_width="100" data-rating_value="5" data-message="Отлично"></span>
<span class="stars_current" data-rating="0"></span>
</div>
<!-- <div class="rating_message" data-message="Без оценки">Без оценки</div> -->
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>