Jos*_*ker 5 javascript jquery hover rating-system
目前我正在为网上商店制作评级系统。基本上我希望它如何工作:
如果访问者以前从未评价过:
我使用的字体很棒,所以我没有使用任何图像。现在的问题是,如果我将鼠标悬停在一颗星星上,它可以工作,但如果我想从一颗星星移动到另一颗星星,它就会出现故障(因为星星之间有一点间隙,这意味着它会先重置星星)。
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');
});
}
Run Code Online (Sandbox Code Playgroud)
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>
Run Code Online (Sandbox Code Playgroud)
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);
}
Run Code Online (Sandbox Code Playgroud)
我知道有很多库可以让它变得更容易,但如果可以的话,我想将它保留为我自己的代码。
您可以使用纯 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;
}Run Code Online (Sandbox Code Playgroud)
<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>Run Code Online (Sandbox Code Playgroud)
我改变了这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);
Run Code Online (Sandbox Code Playgroud)
所以现在star_element是li.
另外,如果您喜欢 jsfiddle,这里有一个链接
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);
Run Code Online (Sandbox Code Playgroud)
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');
});
}Run Code Online (Sandbox Code Playgroud)
.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);
}Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4399 次 |
| 最近记录: |