星级评定系统悬停
Star rating system hover
目前我正在为网上商店制作评级系统。基本上我希望它如何工作:
如果访问者之前从未评价过:
- 访客悬停在星星上
- 上一个和当前的星星将为黄色,下一个星星为灰色(用 class 完成)
- 如果访问者离开悬停,将所有星星重置为旧状态
- 如果访问者点击一颗星,保存它,计算下一个星值并更新数组。
我使用的字体很棒,所以我没有使用任何图像。现在的问题是,如果我将鼠标悬停在一颗星星上,它会起作用,但如果我想从一颗星星移动到另一颗星星,它就会出现故障(因为星星之间有一点间隙,这意味着它会先重置星星)。
jsfiddle: https://jsfiddle.net/uappvz3y/
JS:
var current_star_statusses = [];
star_elements = $('.fa-star');
star_elements.each(function(i, elem)
{
current_star_statusses.push($(elem).hasClass('yellow'));
});
star_elements.mouseenter(changeRatingStars);
star_elements.mouseleave(resetRatingStars);
/**
* Changes the rating star colors when hovering over it.
*/
function changeRatingStars()
{
// Current star hovered
var star = $(this);
// Removes all colors first from all stars
$('.fa-star').removeClass('gray').removeClass('yellow');
// Makes the current hovered star yellow
star.addClass('yellow');
// Makes the previous stars yellow and the next stars gray
star.parent().prevAll().children('.fa-star').addClass('yellow');
star.parent().nextAll().children('.fa-star').addClass('gray');
}
/**
* Resets the rating star colors when not hovered anymore.
*/
function resetRatingStars()
{
star_elements.each(function(i, elem)
{
$(elem).removeClass('yellow').removeClass('gray').addClass(current_star_statusses[i] ? 'yellow' : 'gray');
});
}
HTML:
<ul class="list-inline rating-list">
<li><i class="fa fa-star yellow"></i></li>
<li><i class="fa fa-star yellow"></i></li>
<li><i class="fa fa-star yellow"></i></li>
<li><i class="fa fa-star yellow"></i></li>
<li><i class="fa fa-star gray"></i></li>
</ul>
CSS:
.fa-star:before {
content: "\f005";
}
.rating-list li i.yellow {
color: #FFD700;
}
.rating-list li i.gray {
color: #bbb;
}
.list-inline>li {
display: inline-block;
padding-right: 5px;
padding-left: 5px;
}
.rating-list li {
padding: 0px;
}
.fa {
display: inline-block;
font: normal normal normal 14px/1 FontAwesome;
font-size: inherit;
text-rendering: auto;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
transform: translate(0, 0);
}
我知道有很多库可以使它更容易,但如果可以的话,我想保留我自己的代码。
我改了这4行代码
star_elements = $('.fa-star').parent();
star_elements.find(".fa-star").each(function(i, elem) {
current_star_statusses.push($(elem).hasClass('yellow'));
});
star_elements.find(".fa-star").mouseenter(changeRatingStars);
star_elements.find(".fa-star").mouseleave(resetRatingStars);
所以现在 star_element
是 li
。
此外,如果您喜欢 jsfiddle,这里有一个 link
var current_star_statusses = [];
star_elements = $('.fa-star').parent();
star_elements.find(".fa-star").each(function(i, elem) {
current_star_statusses.push($(elem).hasClass('yellow'));
});
star_elements.find(".fa-star").mouseenter(changeRatingStars);
star_elements.find(".fa-star").mouseleave(resetRatingStars);
/**
* Changes the rating star colors when hovering over it.
*/
function changeRatingStars() {
// Current star hovered
var star = $(this);
// Removes all colors first from all stars
$('.fa-star').removeClass('gray').removeClass('yellow');
// Makes the current hovered star yellow
star.addClass('yellow');
// Makes the previous stars yellow and the next stars gray
star.parent().prevAll().children('.fa-star').addClass('yellow');
star.parent().nextAll().children('.fa-star').addClass('gray');
}
/**
* Resets the rating star colors when not hovered anymore.
*/
function resetRatingStars() {
star_elements.each(function(i, elem) {
$(elem).removeClass('yellow').removeClass('gray').addClass(current_star_statusses[i] ? 'yellow' : 'gray');
});
}
.fa-star:before {
content: "\f005";
}
.rating-list li i.yellow {
color: #FFD700;
}
.rating-list li i.gray {
color: #bbb;
}
.list-inline>li {
display: inline-block;
padding-right: 5px;
padding-left: 5px;
}
.rating-list li {
padding: 0px;
}
.fa {
display: inline-block;
font: normal normal normal 14px/1 FontAwesome;
font-size: inherit;
text-rendering: auto;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
transform: translate(0, 0);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet" />
<ul class="list-inline rating-list">
<li><i class="fa fa-star yellow"></i></li>
<li><i class="fa fa-star yellow"></i></li>
<li><i class="fa fa-star yellow"></i></li>
<li><i class="fa fa-star yellow"></i></li>
<li><i class="fa fa-star gray"></i></li>
</ul>
您可以使用纯 CSS 进行星级评分。向右浮动星星,并为具有填充的 li
应用悬停效果。
.rating-list li {
float: right;
color: #ddd;
padding: 10px 5px;
}
.rating-list li:hover,
.rating-list li:hover ~ li {
color: #ffd700;
}
.rating-list {
display: inline-block;
list-style: none;
}
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet"/>
<ul class="list-inline rating-list">
<li><i class="fa fa-star" title="Rate 5"></i></li>
<li><i class="fa fa-star" title="Rate 4"></i></li>
<li><i class="fa fa-star" title="Rate 3"></i></li>
<li><i class="fa fa-star" title="Rate 2"></i></li>
<li><i class="fa fa-star" title="Rate 1"></i></li>
</ul>
有一个更简单的解决方案:改为在 .fa
元素上使用填充,并为列表项使用 float: left
,这意味着每个星号之间没有间距。
这几条规则足以达到你想要的效果:
.list-inline {
list-style: none;
padding: 0;
margin: 0;
overflow: hidden;
}
.list-inline > li {
float: left;
}
.rating-list li {
padding: 0px;
}
.rating-list li .fa {
padding-right: 5px;
}
这是一个概念验证示例,您的 JS 代码保持不变:
$(function() {
var current_star_statusses = [];
star_elements = $('.fa-star');
star_elements.each(function(i, elem) {
current_star_statusses.push($(elem).hasClass('yellow'));
});
star_elements.mouseenter(changeRatingStars);
star_elements.mouseleave(resetRatingStars);
/**
* Changes the rating star colors when hovering over it.
*/
function changeRatingStars() {
// Current star hovered
var star = $(this);
// Removes all colors first from all stars
$('.fa-star').removeClass('gray').removeClass('yellow');
// Makes the current hovered star yellow
star.addClass('yellow');
// Makes the previous stars yellow and the next stars gray
star.parent().prevAll().children('.fa-star').addClass('yellow');
star.parent().nextAll().children('.fa-star').addClass('gray');
}
/**
* Resets the rating star colors when not hovered anymore.
*/
function resetRatingStars() {
star_elements.each(function(i, elem) {
$(elem).removeClass('yellow').removeClass('gray').addClass(current_star_statusses[i] ? 'yellow' : 'gray');
});
}
});
.fa-star:before {
content: "\f005";
}
.rating-list li i.yellow {
color: #FFD700;
}
.rating-list li i.gray {
color: #bbb;
}
.list-inline {
list-style: none;
padding: 0;
margin: 0;
overflow: hidden;
}
.list-inline>li {
float: left;
}
.rating-list li {
padding: 0px;
}
.rating-list li .fa {
padding-right: 5px;
}
.fa {
display: inline-block;
font: normal normal normal 14px/1 FontAwesome;
font-size: inherit;
text-rendering: auto;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
transform: translate(0, 0);
}
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul class="list-inline rating-list">
<li><i class="fa fa-star yellow"></i></li>
<li><i class="fa fa-star yellow"></i></li>
<li><i class="fa fa-star yellow"></i></li>
<li><i class="fa fa-star yellow"></i></li>
<li><i class="fa fa-star gray"></i></li>
</ul>
目前我正在为网上商店制作评级系统。基本上我希望它如何工作:
如果访问者之前从未评价过:
- 访客悬停在星星上
- 上一个和当前的星星将为黄色,下一个星星为灰色(用 class 完成)
- 如果访问者离开悬停,将所有星星重置为旧状态
- 如果访问者点击一颗星,保存它,计算下一个星值并更新数组。
我使用的字体很棒,所以我没有使用任何图像。现在的问题是,如果我将鼠标悬停在一颗星星上,它会起作用,但如果我想从一颗星星移动到另一颗星星,它就会出现故障(因为星星之间有一点间隙,这意味着它会先重置星星)。
jsfiddle: https://jsfiddle.net/uappvz3y/
JS:
var current_star_statusses = [];
star_elements = $('.fa-star');
star_elements.each(function(i, elem)
{
current_star_statusses.push($(elem).hasClass('yellow'));
});
star_elements.mouseenter(changeRatingStars);
star_elements.mouseleave(resetRatingStars);
/**
* Changes the rating star colors when hovering over it.
*/
function changeRatingStars()
{
// Current star hovered
var star = $(this);
// Removes all colors first from all stars
$('.fa-star').removeClass('gray').removeClass('yellow');
// Makes the current hovered star yellow
star.addClass('yellow');
// Makes the previous stars yellow and the next stars gray
star.parent().prevAll().children('.fa-star').addClass('yellow');
star.parent().nextAll().children('.fa-star').addClass('gray');
}
/**
* Resets the rating star colors when not hovered anymore.
*/
function resetRatingStars()
{
star_elements.each(function(i, elem)
{
$(elem).removeClass('yellow').removeClass('gray').addClass(current_star_statusses[i] ? 'yellow' : 'gray');
});
}
HTML:
<ul class="list-inline rating-list">
<li><i class="fa fa-star yellow"></i></li>
<li><i class="fa fa-star yellow"></i></li>
<li><i class="fa fa-star yellow"></i></li>
<li><i class="fa fa-star yellow"></i></li>
<li><i class="fa fa-star gray"></i></li>
</ul>
CSS:
.fa-star:before {
content: "\f005";
}
.rating-list li i.yellow {
color: #FFD700;
}
.rating-list li i.gray {
color: #bbb;
}
.list-inline>li {
display: inline-block;
padding-right: 5px;
padding-left: 5px;
}
.rating-list li {
padding: 0px;
}
.fa {
display: inline-block;
font: normal normal normal 14px/1 FontAwesome;
font-size: inherit;
text-rendering: auto;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
transform: translate(0, 0);
}
我知道有很多库可以使它更容易,但如果可以的话,我想保留我自己的代码。
我改了这4行代码
star_elements = $('.fa-star').parent();
star_elements.find(".fa-star").each(function(i, elem) {
current_star_statusses.push($(elem).hasClass('yellow'));
});
star_elements.find(".fa-star").mouseenter(changeRatingStars);
star_elements.find(".fa-star").mouseleave(resetRatingStars);
所以现在 star_element
是 li
。
此外,如果您喜欢 jsfiddle,这里有一个 link
var current_star_statusses = [];
star_elements = $('.fa-star').parent();
star_elements.find(".fa-star").each(function(i, elem) {
current_star_statusses.push($(elem).hasClass('yellow'));
});
star_elements.find(".fa-star").mouseenter(changeRatingStars);
star_elements.find(".fa-star").mouseleave(resetRatingStars);
/**
* Changes the rating star colors when hovering over it.
*/
function changeRatingStars() {
// Current star hovered
var star = $(this);
// Removes all colors first from all stars
$('.fa-star').removeClass('gray').removeClass('yellow');
// Makes the current hovered star yellow
star.addClass('yellow');
// Makes the previous stars yellow and the next stars gray
star.parent().prevAll().children('.fa-star').addClass('yellow');
star.parent().nextAll().children('.fa-star').addClass('gray');
}
/**
* Resets the rating star colors when not hovered anymore.
*/
function resetRatingStars() {
star_elements.each(function(i, elem) {
$(elem).removeClass('yellow').removeClass('gray').addClass(current_star_statusses[i] ? 'yellow' : 'gray');
});
}
.fa-star:before {
content: "\f005";
}
.rating-list li i.yellow {
color: #FFD700;
}
.rating-list li i.gray {
color: #bbb;
}
.list-inline>li {
display: inline-block;
padding-right: 5px;
padding-left: 5px;
}
.rating-list li {
padding: 0px;
}
.fa {
display: inline-block;
font: normal normal normal 14px/1 FontAwesome;
font-size: inherit;
text-rendering: auto;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
transform: translate(0, 0);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet" />
<ul class="list-inline rating-list">
<li><i class="fa fa-star yellow"></i></li>
<li><i class="fa fa-star yellow"></i></li>
<li><i class="fa fa-star yellow"></i></li>
<li><i class="fa fa-star yellow"></i></li>
<li><i class="fa fa-star gray"></i></li>
</ul>
您可以使用纯 CSS 进行星级评分。向右浮动星星,并为具有填充的 li
应用悬停效果。
.rating-list li {
float: right;
color: #ddd;
padding: 10px 5px;
}
.rating-list li:hover,
.rating-list li:hover ~ li {
color: #ffd700;
}
.rating-list {
display: inline-block;
list-style: none;
}
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet"/>
<ul class="list-inline rating-list">
<li><i class="fa fa-star" title="Rate 5"></i></li>
<li><i class="fa fa-star" title="Rate 4"></i></li>
<li><i class="fa fa-star" title="Rate 3"></i></li>
<li><i class="fa fa-star" title="Rate 2"></i></li>
<li><i class="fa fa-star" title="Rate 1"></i></li>
</ul>
有一个更简单的解决方案:改为在 .fa
元素上使用填充,并为列表项使用 float: left
,这意味着每个星号之间没有间距。
这几条规则足以达到你想要的效果:
.list-inline {
list-style: none;
padding: 0;
margin: 0;
overflow: hidden;
}
.list-inline > li {
float: left;
}
.rating-list li {
padding: 0px;
}
.rating-list li .fa {
padding-right: 5px;
}
这是一个概念验证示例,您的 JS 代码保持不变:
$(function() {
var current_star_statusses = [];
star_elements = $('.fa-star');
star_elements.each(function(i, elem) {
current_star_statusses.push($(elem).hasClass('yellow'));
});
star_elements.mouseenter(changeRatingStars);
star_elements.mouseleave(resetRatingStars);
/**
* Changes the rating star colors when hovering over it.
*/
function changeRatingStars() {
// Current star hovered
var star = $(this);
// Removes all colors first from all stars
$('.fa-star').removeClass('gray').removeClass('yellow');
// Makes the current hovered star yellow
star.addClass('yellow');
// Makes the previous stars yellow and the next stars gray
star.parent().prevAll().children('.fa-star').addClass('yellow');
star.parent().nextAll().children('.fa-star').addClass('gray');
}
/**
* Resets the rating star colors when not hovered anymore.
*/
function resetRatingStars() {
star_elements.each(function(i, elem) {
$(elem).removeClass('yellow').removeClass('gray').addClass(current_star_statusses[i] ? 'yellow' : 'gray');
});
}
});
.fa-star:before {
content: "\f005";
}
.rating-list li i.yellow {
color: #FFD700;
}
.rating-list li i.gray {
color: #bbb;
}
.list-inline {
list-style: none;
padding: 0;
margin: 0;
overflow: hidden;
}
.list-inline>li {
float: left;
}
.rating-list li {
padding: 0px;
}
.rating-list li .fa {
padding-right: 5px;
}
.fa {
display: inline-block;
font: normal normal normal 14px/1 FontAwesome;
font-size: inherit;
text-rendering: auto;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
transform: translate(0, 0);
}
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul class="list-inline rating-list">
<li><i class="fa fa-star yellow"></i></li>
<li><i class="fa fa-star yellow"></i></li>
<li><i class="fa fa-star yellow"></i></li>
<li><i class="fa fa-star yellow"></i></li>
<li><i class="fa fa-star gray"></i></li>
</ul>