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

组件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

react 实现评分组件的更多相关文章

  1. ExtJS 4.2 评分组件

    上一文章是扩展ExtJS自带的Date组件.在这里将创建一个评分组件. 目录 1. 介绍 2. 示例 3. 资源下载 1. 介绍 代码参考的是 Sencha Touch 2上的一个RatingStar ...

  2. React Native 之 组件化开发

    前言 学习本系列内容需要具备一定 HTML 开发基础,没有基础的朋友可以先转至 HTML快速入门(一) 学习 本人接触 React Native 时间并不是特别长,所以对其中的内容和性质了解可能会有所 ...

  3. React jQuery公用组件开发模式及实现

    目前较为流行的react确实有很多优点,例如虚拟dom,单向数据流状态机的思想.还有可复用组件化的思想等等.加上搭配jsx语法和es6,适应之后开发确实快捷很多,值得大家去一试.其实组件化的思想一直在 ...

  4. 干货之运用CALayer创建星级评分组件(五角星)

    本篇记录星级评分组件的创建过程以及CALayer的运用. 为了实现一个星级评分的组件,使用了CALayer,涉及到mask.CGPathRef.UIBezierPath.动画和一个计算多角星关键节点的 ...

  5. React Native的组件ListView

    React Native的组件ListView类似于iOS中的UITableView和UICollectionView,也就是说React Native的组件ListView既可以实现UITableV ...

  6. React Native交互组件之Touchable

    React Native交互组件之Touchable:只要在组件外面包一个Touchable组件就可以实现点击交互. TouchableHighlight:高亮触摸 当点击时,组件的透明度会改变,可以 ...

  7. React中父组件与子组件之间的数据传递和标准化的思考

    React中父组件与子组件之间的数据传递的的实现大家都可以轻易做到,但对比很多人的实现方法,总是会有或多或少的差异.在一个团队中,这种实现的差异体现了每个人各自的理解的不同,但是反过来思考,一个团队用 ...

  8. 聊聊React高阶组件(Higher-Order Components)

    使用 react已经有不短的时间了,最近看到关于 react高阶组件的一篇文章,看了之后顿时眼前一亮,对于我这种还在新手村晃荡.一切朝着打怪升级看齐的小喽啰来说,像这种难度不是太高同时门槛也不是那么低 ...

  9. React——高阶组件

    1.在React中higher-order component (HOC)是一种重用组件逻辑的高级技术.HOC不是React API中的一部分.HOC是一个函数,该函数接收一个组件并且返回一个新组件. ...

随机推荐

  1. Mybatis报错:org.apache.ibatis.builder.IncompleteElementException

    org.apache.ibatis.builder.IncompleteElementException: Could not find result map java.lang.Integer遇到这 ...

  2. python做上位机

    参考文章: https://blog.csdn.net/dgut_guangdian/article/details/78391270 https://www.cnblogs.com/lanceyu/ ...

  3. python 3环境下,离线安装模块(modules)

    说明: 需要在环境中安装python的模块,但是无法联网,就通过在Pypi上下载离线模块的包进行安装 安装过程: 1.下载模块,如PyMySQL-0.9.3.tar.gz,下载地址:https://f ...

  4. Java学习-056-Jsoup爬虫获取中国所有的三级行政区划数据(一)

    在涉及地址服务时,经常需要用到地址信息的获取,而行政区划可能不定期的发生变化,所以我们需要获取最新的行政区划信息.因行政区划数据量较大,Java中可以使用Jsoup进行数据的获取.处理. 大家经常用到 ...

  5. pipenv使用学习

    参考https://realpython.com/pipenv-guide/#package-distribution Pipenv: A Guide to the New Python Packag ...

  6. BATJ都爱问的多线程面试题

    # 一 面试中关于 synchronized 关键字的 5 连击 ### 1.1 说一说自己对于 synchronized 关键字的了解 synchronized关键字解决的是多个线程之间访问资源的同 ...

  7. https://www.jianshu.com/p/1038c6170775

    import os # 方法一: os.walk实现 def items_dir(rootname): l = [] for main_dir, dirs, file_name_list in os. ...

  8. Nexus上传python包

    参考 https://blog.csdn.net/m0_37607365/article/details/79998955 1.首先创建pypi仓库 其中,PyPI类的服务,支持三种: proxy,提 ...

  9. linux驱动开发学习一:创建一个字符设备

    首先是内核初始化函数.代码如下.主要是三个步骤.1 生成设备号. 2 注册设备号.3 创建设备. #include <linux/module.h> #include <linux/ ...

  10. UE4 Animation]IK Related

    转自:https://dawnarc.com/2018/05/ue4animationik-related/ Examples 工程1 在油管上看到一个UE4 IK动画的demo工程示例 该示例作者的 ...