写了个评分组件,效果如下

组件Rate.js

import React, { Component } from 'react'
import './Rate.less' export default class Rate extends Component {
state = {
count: this.props.number || 5,
num: this.props.def || 0,
enter: 0,
leave: this.props.def || 0,
state: ['不满意', '满意', '超满意']
}
/** 页面渲染前 */
componentWillMount = () => {}
/** 页面渲染后 */
componentDidMount = () => {}
/** 数据更新前 */
componentWillUpdate = () => {
this.showState()
}
showState() {
let { count, num, enter, state } = this.state
let f = Math.ceil(count / 2)
if (num == 0 && enter == 0) {
return ''
} else if (num < f && enter < f) {
return state[0]
} else if (
num == count ||
enter == count
) {
return state[2]
} else {
return state[1]
}
}
/** 数据更新后 */
componentDidUpdate = () => {}
render() {
let { count, num, enter, leave } = this.state
return (
<div className="rate">
<p className="photo">
{new Array(count).fill().map((item, index) => (
<span
key={index}
onClick={() => {
num = index + 1
leave = num
this.setState({ num, leave })
}}
onMouseEnter={() => {
enter = index + 1
num = 0
this.setState({ enter, num })
}}
onMouseLeave={() => {
enter = 0
num = leave
this.setState({ enter, num })
}}
>
{enter > index ? (
<i className="high" />
) : num > index ? (
<i className="high" />
) : (
<i className="nohigh" />
)}
</span>
))}
{this.showState()}
</p>
</div>
)
}
}

组件样式 Rate.less

.rate .photo span {
position: relative;
display: inline-block;
width: 0.4rem;
height: 0.4rem;
overflow: hidden;
margin-right: 0.1rem;
cursor: pointer;
}
.rate .photo span:last-child {
margin-right: 0px;
}
.rate .photo span .nohigh {
background-color: red;
position: absolute;
width: 0.4rem;
height: 0.4rem;
top: 0;
left: 0;
background: url('./star.png') no-repeat;
background-size: 0.4rem 0.4rem;
}
.rate .photo span .high {
background-color: purple;
position: absolute;
width: 0.4rem;
height: 0.4rem;
top: 0;
left: 0;
background: url('./star_active.png') no-repeat;
background-size: 0.4rem 0.4rem;
}
.rate .starNum {
font-size: 26px;
color: #de4414;
margin-top: 0.04rem;
margin-bottom: 0.1rem;
}
.rate .bottoms {
height: 54px;
border-top: 1px solid #d8d8d8;
}
.rate .photo {
margin-top: 30px;
}
.rate .bottoms a {
margin-bottom: 0;
}
.rate .bottoms .garyBtn {
margin-right: 57px !important;
}
.rate .bottoms a {
width: 130px;
height: 35px;
line-height: 35px;
border-radius: 3px;
display: inline-block;
font-size: 16px;
transition: all 0.2s linear;
margin: 16px 0 22px;
text-align: center;
cursor: pointer;
}
.garyBtn {
margin-right: 60px !important;
background-color: #e1e1e1;
color: #999999;
}
.blueBtn {
background-color: #1968b1;
color: #fff;
}
.blueBtn:hover {
background: #0e73d0;
}

  

背景图

调用

<Rate number={10} def={5} />
number:为评分总数,默认为5
def:为评分数,默认为0

最新文章

  1. 《web全栈工程师的自我修养》读书笔记
  2. 从up6-down2升级到down3
  3. 斯坦福第七课:正则化(Regularization)
  4. 判断Set里的元素是否重复、==、equals、hashCode方法研究-代码演示
  5. uva 11752 - The Super Powers
  6. 顺序表的基本操作(C)
  7. iOS蓝牙中的进制转换
  8. 未完待续的JAVA基础知识
  9. iOS开发概述UIkit动力学,讲述UIKit的Dynamic特性,UIkit动力学是UIkit框架中模拟真实世界的一些特性。
  10. Unity3D - Animator Controller循环依赖
  11. 0513JS数组内置方法、数学函数、时间函数
  12. USACO15DEC最大流MaxFlow
  13. bzoj 1002
  14. vue-router-1-安装与基本使用
  15. 20135202闫佳歆--week5 分析system_call中断处理过程--实验及总结
  16. CentOS系统很卡的基本排查方法
  17. EnterpriseDb公司的Postgres Enterprise Manager 安装图解
  18. UVALive3713_Astronauts
  19. Unity3D - 详解Quaternion类(一)
  20. python学习笔记:第8天 文件操作

热门文章

  1. 变异的功能 | variants function | coding | non-coding
  2. Comparable接口的使用
  3. 【转发】jquery实现自动打开新的页签
  4. Linux下挂载超过2T的磁盘
  5. Scrapy爬虫Demo 爬取资讯分类
  6. OKEx交易所交易记录日期时间转毫秒级时间戳
  7. vue做页面按钮权限--分析
  8. IIS部署FLASK网站
  9. Laradock中文文档
  10. kafka高吞吐量的分布式发布订阅的消息队列系统