【问题标题】:Hover with star rating not working properly悬停星级无法正常工作
【发布时间】:2018-10-30 00:57:45
【问题描述】:

所以我在 codepen (https://codepen.io/anon/pen/Edzxaj) 上找到了以下代码来进行星级评分,这对于 Font Awesome 3.x 和 4.x 工作正常,但在 5.x 上悬停时无法产生相同的结果星星。

/* Base setup */
@import url(//use.fontawesome.com/releases/v5.4.2/css/all.css);

/* Ratings widget */
.rate {
    display: inline-block;
    border: 0;
}
/* Hide radio */
.rate > input {
    display: none;
}
/* Order correctly by floating highest to the right */
.rate > label {
    float: right;
}
/* The star of the show */
.rate > label:before {
    display: inline-block;
    font-size: 1.1rem;
    padding: .3rem .2rem;
    margin: 0;
    cursor: pointer;
    font-family: 'Font Awesome 5 Free';
    font-weight: 900;
    content: "\f005"; /* full star */
}

/* Half star trick */
.rate .half:before {
    content: "\f089"; /* half star no outline */
    position: absolute;
    padding-right: 0;
}

/* Click + hover color */
input:checked ~ label, /* color current and previous stars on checked */
label:hover, label:hover ~ label { color: #73B100;  } /* color previous stars on hover */

/* Hover highlights */
input:checked + label:hover, input:checked ~ label:hover, /* highlight current and previous stars */
input:checked ~ label:hover ~ label, /* highlight previous selected stars for new rating */
label:hover ~ input:checked ~ label /* highlight previous selected stars */ { color: #A6E72D;  } 
<fieldset class="rate">
    <input type="radio" id="rating10" name="rating" value="10" /><label for="rating10" title="5 stars"></label>
    <input type="radio" id="rating9" name="rating" value="9" /><label class="half" for="rating9" title="4 1/2 stars"></label>
    <input type="radio" id="rating8" name="rating" value="8" /><label for="rating8" title="4 stars"></label>
    <input type="radio" id="rating7" name="rating" value="7" /><label class="half" for="rating7" title="3 1/2 stars"></label>
    <input type="radio" id="rating6" name="rating" value="6" /><label for="rating6" title="3 stars"></label>
    <input type="radio" id="rating5" name="rating" value="5" /><label class="half" for="rating5" title="2 1/2 stars"></label>
    <input type="radio" id="rating4" name="rating" value="4" /><label for="rating4" title="2 stars"></label>
    <input type="radio" id="rating3" name="rating" value="3" /><label class="half" for="rating3" title="1 1/2 stars"></label>
    <input type="radio" id="rating2" name="rating" value="2" /><label for="rating2" title="1 star"></label>
    <input type="radio" id="rating1" name="rating" value="1" /><label class="half" for="rating1" title="1/2 star"></label>
</fieldset>

使用之前版本的 font awesome,将鼠标悬停在星形的后半部分会填满星形。在 5.x 中,您需要将光标一直悬停到星号的末尾,然后才能被填充(我知道这听起来有点模糊,但只需比较 codepen 和此示例即可了解悬停时的差异)。 有没有办法解决这个问题?

【问题讨论】:

  • Font Awesome 完全改变了 v5 中图标的渲染方式。你有什么不能使用 FA4 的原因吗?
  • 您知道您在需要许可证的地方包含 PRO 版本吗?
  • @BryceHowitson 好吧,除了我网站上的评级,我已经将所有内容都更新到了 FA5,所以仅仅为了这个而回到 FA4 可能是不值得的。
  • @TemaniAfif 我知道,我有自己的免费版本,但我在 codepen 上看到了一些示例,您可以在其中使用专业版来玩耍并查看新图标。我可以用免费版本更新上面的代码,没有什么大问题。
  • 悬停等待直到光标几乎离开半星的“原因”是padding:0。一种解决方案是将星形容器减小到宽度的一半(甚至可以使用全星形)。我只是还没想好怎么做。

标签: html css font-awesome font-awesome-5


【解决方案1】:

我能够通过将星形容器“切成”两半来解决这个问题。角色周围的额外空间导致悬停看起来不正确。

需要注意的是,这需要设置一个宽度,这可能会或可能不会成为问题,具体取决于您的实现。

.half 获得大约一半的完整星形宽度,然后overflow:hidden 删除多余的一半,margin-right 将星形移回左侧。它还为两个显示器使用了完整的星形图标,以便更好地排列。

/* Base setup */
@import url(//use.fontawesome.com/releases/v5.4.2/css/all.css);

/* Ratings widget */
.rate {
    display: inline-block;
    border: 0;
}
/* Hide radio */
.rate > input {
    display: none;
}
/* Order correctly by floating highest to the right */
.rate > label {
    float: right;
}
/* The star of the show */
.rate > label:before {
    display: inline-block;
    font-size: 1.1rem;
    padding: .3rem .2rem;
    margin-right:0;
    cursor: pointer;
    font-family: 'Font Awesome 5 Free';
    font-weight: 900;
    content: "\f005"; /* full star */
}

/* Half star trick */
.rate .half:before {
    content: "\f005"; /* half star no outline */
    position: absolute;
    padding-right: 0;
    width: 0.6rem;
    overflow: hidden;
    margin-right: 0.4rem;
}

/* Click + hover color */
input:checked ~ label, /* color current and previous stars on checked */
label:hover, label:hover ~ label { color: #73B100;  } /* color previous stars on hover */

/* Hover highlights */
input:checked + label:hover, input:checked ~ label:hover, /* highlight current and previous stars */
input:checked ~ label:hover ~ label, /* highlight previous selected stars for new rating */
label:hover ~ input:checked ~ label /* highlight previous selected stars */ { color: #A6E72D;  } 
<fieldset class="rate">
    <input type="radio" id="rating10" name="rating" value="10" /><label for="rating10" title="5 stars"></label>
    <input type="radio" id="rating9" name="rating" value="9" /><label class="half" for="rating9" title="4 1/2 stars"></label>
    <input type="radio" id="rating8" name="rating" value="8" /><label for="rating8" title="4 stars"></label>
    <input type="radio" id="rating7" name="rating" value="7" /><label class="half" for="rating7" title="3 1/2 stars"></label>
    <input type="radio" id="rating6" name="rating" value="6" /><label for="rating6" title="3 stars"></label>
    <input type="radio" id="rating5" name="rating" value="5" /><label class="half" for="rating5" title="2 1/2 stars"></label>
    <input type="radio" id="rating4" name="rating" value="4" /><label for="rating4" title="2 stars"></label>
    <input type="radio" id="rating3" name="rating" value="3" /><label class="half" for="rating3" title="1 1/2 stars"></label>
    <input type="radio" id="rating2" name="rating" value="2" /><label for="rating2" title="1 star"></label>
    <input type="radio" id="rating1" name="rating" value="1" /><label class="half" for="rating1" title="1/2 star"></label>
</fieldset>

【讨论】:

  • 爱它。还要感谢您的简短解释,这确实使事情变得清晰(尤其是对于 CSS 初学者)。
猜你喜欢
  • 2016-04-27
  • 2023-04-01
  • 2011-09-13
  • 2017-03-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多