首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用html和css创建评级星

用html和css创建评级星
EN

Stack Overflow用户
提问于 2022-01-05 06:50:03
回答 2查看 285关注 0票数 3

我想用我的html脚本创建一个速率星。如果我的html脚本像

<input type="radio" name="rating" id="rating-5"><label for="rating-5" class="fas fa-star"></label>

但我想这样跑

<label for="rating-5" class="fas fa-star"> <input type="radio" name="rating" id="rating-5"> </label>

就像下面的示例脚本和css无法工作一样

示例:

代码语言:javascript
复制
{
  margin: 0;
  padding: 0;
  box-sizing: border-box;
  font-family: arial;
}

.star-rating input {
  display: none;
}

.star-rating {
  margin: 50px auto;
  display: table;
  width: 350px;
}

.star-rating label {
  padding: 10px;
  float: right;
  font-size: 44px;
  color: #eee;
}

.star-rating input:not(:checked)~label:hover,
.star-rating input:not(:checked)~label:hover~label {
  color: #ffc107;
}

.star-rating input:checked {
  color: #ffc107;
}
代码语言:javascript
复制
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.10.2/css/all.css" />

<div class="star-rating">
  <div class="star-input">
    <label for="rating-5" class="fas fa-star">
        <input type="radio" name="rating" id="rating-5"></label>
    <label for="rating-4" class="fas fa-star">  
        <input type="radio" name="rating" id="rating-4"></label>
    <label for="rating-3" class="fas fa-star">    
        <input type="radio" name="rating" id="rating-3"></label>
    <label for="rating-2" class="fas fa-star">       
        <input type="radio" name="rating" id="rating-2"></label>
    <label for="rating-1" class="fas fa-star">     
        <input type="radio" name="rating" id="rating-1"></label>
  </div>
</div>

请帮助我的问题谢谢以前和对不起我的糟糕的英语。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2022-01-05 07:31:18

这可能是不使用JS的解决方案。

代码语言:javascript
复制
 .rating {
  display: inline-block;
  position: relative;
  height: 50px;
  line-height: 50px;
  font-size: 25px;
}

.rating label {
  position: absolute;
  top: 0;
  left: 0;
  height: 100%;
  cursor: pointer;
}

.rating label:last-child {
  position: static;
}

.rating label:nth-child(1) {
  z-index: 5;
}

.rating label:nth-child(2) {
  z-index: 4;
}

.rating label:nth-child(3) {
  z-index: 3;
}

.rating label:nth-child(4) {
  z-index: 2;
}

.rating label:nth-child(5) {
  z-index: 1;
}

.rating label input {
  position: absolute;
  top: 0;
  left: 0;
  opacity: 0;
}

.rating label .icon {
  float: left;
  color: transparent;
}

.rating label:last-child .icon {
  color: #000;
}

.rating:not(:hover) label input:checked ~ .icon,
.rating:hover label:hover input ~ .icon {
  color: #ffa904;
}

.rating label input:focus:not(:checked) ~ .icon:last-child {
  color: #000;
  text-shadow: 0 0 5px #ffa904;
}
代码语言:javascript
复制
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.10.2/css/all.css" />
<form class="rating">
  <label>
    <input type="radio" name="stars" value="1" />
    <span class="fa fa-star icon"></span>
  </label>
  <label>
    <input type="radio" name="stars" value="2" />
    <span class="fa fa-star icon"></span>
    <span class="fa fa-star icon"></span>
  </label>
  <label>
    <input type="radio" name="stars" value="3" />
    <span class="fa fa-star icon"></span>
    <span class="fa fa-star icon"></span>
    <span class="fa fa-star icon"></span>   
  </label>
  <label>
    <input type="radio" name="stars" value="4" />
    <span class="fa fa-star icon"></span>
    <span class="fa fa-star icon"></span>
    <span class="fa fa-star icon"></span>
    <span class="fa fa-star icon"></span>
  </label>
  <label>
    <input type="radio" name="stars" value="5" />
    <span class="fa fa-star icon"></span>
    <span class="fa fa-star icon"></span>
    <span class="fa fa-star icon"></span>
    <span class="fa fa-star icon"></span>
    <span class="fa fa-star icon"></span>
  </label>
</form>

票数 4
EN

Stack Overflow用户

发布于 2022-01-05 07:54:51

如果我们把输入从标签上取下来呢?

编辑!

HTML:

代码语言:javascript
复制
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.10.2/css/all.css" />

<div class="star-rating">
  <div class="star-input">
      <input type="radio" name="rating" id="rating-5">
    <label for="rating-5" class="fas fa-star">
    </label>
        <input type="radio" name="rating" id="rating-4">
    <label for="rating-4" class="fas fa-star">  
     </label>
    <input type="radio" name="rating" id="rating-3">
    <label for="rating-3" class="fas fa-star">    
    </label>
          <input type="radio" name="rating" id="rating-2">
     
    <label for="rating-2" class="fas fa-star">       
       </label>
              <input type="radio" name="rating" id="rating-1">
    <label for="rating-1" class="fas fa-star">     
     </label>
   
  </div>
</div>

CSS:

代码语言:javascript
复制
{
  margin: 0;
  padding: 0;
  box-sizing: border-box;
  font-family: arial;
}

.star-rating input {
  display: none;
}

.star-rating {
  margin: 50px auto;
  display: table;
  width: 350px;
}

.star-rating label {
  padding: 10px;
  float: right;
  font-size: 44px;
  color: #eee;
}

.star-rating input:not(:checked)~label:hover,
.star-rating input:not(:checked)~label:hover~label {
  color: #ffc107;
}
.star-rating input[type="radio"]:checked ~label  
  {
  color: red!important;
}

小提琴

现在看来,选择是可行的。不会在特殊情况下和孩子发生争执等等。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70588824

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档