前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >纯CSS制作一个评星组件(说出去都没人信)

纯CSS制作一个评星组件(说出去都没人信)

作者头像
Javanx
发布2019-09-04 10:36:24
5150
发布2019-09-04 10:36:24
举报
文章被收录于专栏:web秀web秀

前言

纯CSS制作一个评星组件,说出去都没人信,你信吗?

纯CSS制作一个评星组件(说出去都没人信)
纯CSS制作一个评星组件(说出去都没人信)

如果让你制作上面图的效果,你会怎么开发了?可以下发评论说说你的想法。今天就来看看纯CSS是如何实现这个效果的。

HTML

代码语言:javascript
复制
<div class="radio-stars">
  <input class="sr-only" id="radio-5" type="radio" name="radio-star" value="5" />
  <label class="radio-star" for="radio-5">5</label>
  <input class="sr-only" id="radio-4" type="radio" name="radio-star" value="4" checked="checked" />
  <label class="radio-star" for="radio-4">4</label>
  <input class="sr-only" id="radio-3" type="radio" name="radio-star" value="3" />
  <label class="radio-star" for="radio-3">3</label>
  <input class="sr-only" id="radio-2" type="radio" name="radio-star" value="2" />
  <label class="radio-star" for="radio-2">2</label>
  <input class="sr-only" id="radio-1" type="radio" name="radio-star" value="1" />
  <label class="radio-star" for="radio-1">1</label>
  <span class="radio-star-total"></span>
</div>

radio-stars是容器,radio-starsr-only一 一对应,默认是 4 颗星,radio-star-total展示当前星数/总星数。

下面就是重头戏了

css

代码语言:javascript
复制
.radio-stars {
  display: inline-block;
  position: relative;
  unicode-bidi: bidi-override;
  direction: rtl;
  counter-reset: star-rating;
  font-size: 0;
}
.radio-stars:hover .radio-star::before {
  content: "☆";
}
.radio-stars:hover .radio-star:hover::before,
.radio-stars:hover .radio-star:hover ~ .radio-star::before {
  content: "★";
}

.radio-star {
  display: inline-block;
  overflow: hidden;
  cursor: pointer;
  padding: 0 2px;
  width: 0.7em;
  direction: ltr;
  color: #1199ff90;
  font-size: 40px;
  white-space: nowrap;
}
.radio-star::before {
  content: "☆";
}
.radio-star:hover, .radio-star:hover ~ .radio-star, input:checked ~ .radio-star {
  color: #1199ff;
}
input:checked ~ .radio-star {
  counter-increment: star-rating;
}
input:checked ~ .radio-star::before {
  content: "★";
}

.radio-star-total {
  pointer-events: none;
  direction: ltr;
  unicode-bidi: bidi-override;
  position: absolute;
  right: -2em;
  bottom: 0.5em;
  color: gray;
  color: white;
  font-size: 20px;
}
.radio-star-total::before {
  content: counter(star-rating) "/5";
}

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  margin: -1px;
  padding: 0;
  clip: rect(0, 0, 0, 0);
  border: 0;
}

html {
  display: flex;
  justify-content: center;
  align-items: center;
  background: #f577a8;
  height: 100%;
}

主要用到contentcountercounter-incrementcounter-reset等属性。

说明

兼容性

纯CSS制作一个评星组件(说出去都没人信)
纯CSS制作一个评星组件(说出去都没人信)

counter描述

插入计数器。 在CSS2.1中counter()只能被使用在content属性上。 如果想重复多次计数器可以使用 counters()

counter-increment描述

counter-increment 属性设置某个选取器每次出现的计数器增量。默认增量是 1 如果使用了 "display: none",则无法增加计数。如使用 "visibility: hidden",则可增加计数。

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018年12月11日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 前言
  • HTML
  • css
  • 说明
    • 兼容性
      • counter描述
        • counter-increment描述
        相关产品与服务
        容器服务
        腾讯云容器服务(Tencent Kubernetes Engine, TKE)基于原生 kubernetes 提供以容器为核心的、高度可扩展的高性能容器管理服务,覆盖 Serverless、边缘计算、分布式云等多种业务部署场景,业内首创单个集群兼容多种计算节点的容器资源管理模式。同时产品作为云原生 Finops 领先布道者,主导开源项目Crane,全面助力客户实现资源优化、成本控制。
        领券
        问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档