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

组件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;
}
背景图


调用
react 实现评分组件的更多相关文章
- ExtJS 4.2 评分组件
上一文章是扩展ExtJS自带的Date组件.在这里将创建一个评分组件. 目录 1. 介绍 2. 示例 3. 资源下载 1. 介绍 代码参考的是 Sencha Touch 2上的一个RatingStar ...
- React Native 之 组件化开发
前言 学习本系列内容需要具备一定 HTML 开发基础,没有基础的朋友可以先转至 HTML快速入门(一) 学习 本人接触 React Native 时间并不是特别长,所以对其中的内容和性质了解可能会有所 ...
- React jQuery公用组件开发模式及实现
目前较为流行的react确实有很多优点,例如虚拟dom,单向数据流状态机的思想.还有可复用组件化的思想等等.加上搭配jsx语法和es6,适应之后开发确实快捷很多,值得大家去一试.其实组件化的思想一直在 ...
- 干货之运用CALayer创建星级评分组件(五角星)
本篇记录星级评分组件的创建过程以及CALayer的运用. 为了实现一个星级评分的组件,使用了CALayer,涉及到mask.CGPathRef.UIBezierPath.动画和一个计算多角星关键节点的 ...
- React Native的组件ListView
React Native的组件ListView类似于iOS中的UITableView和UICollectionView,也就是说React Native的组件ListView既可以实现UITableV ...
- React Native交互组件之Touchable
React Native交互组件之Touchable:只要在组件外面包一个Touchable组件就可以实现点击交互. TouchableHighlight:高亮触摸 当点击时,组件的透明度会改变,可以 ...
- React中父组件与子组件之间的数据传递和标准化的思考
React中父组件与子组件之间的数据传递的的实现大家都可以轻易做到,但对比很多人的实现方法,总是会有或多或少的差异.在一个团队中,这种实现的差异体现了每个人各自的理解的不同,但是反过来思考,一个团队用 ...
- 聊聊React高阶组件(Higher-Order Components)
使用 react已经有不短的时间了,最近看到关于 react高阶组件的一篇文章,看了之后顿时眼前一亮,对于我这种还在新手村晃荡.一切朝着打怪升级看齐的小喽啰来说,像这种难度不是太高同时门槛也不是那么低 ...
- React——高阶组件
1.在React中higher-order component (HOC)是一种重用组件逻辑的高级技术.HOC不是React API中的一部分.HOC是一个函数,该函数接收一个组件并且返回一个新组件. ...
随机推荐
- 科研黑帮 | Molecular Genetic Anatomy and Risk Profile of Hirschsprung’s Disease
PNAS又来一篇:Gene- and tissue-level interactions in normal gastrointestinal development and Hirschsprung ...
- etcd启动报错:couldn't find local name "default" in the initial cluster configuration
启动etcd的时候报错: # systemctl restart etcd Job for etcd.service failed because the control process exited ...
- Xamarin.FormsShell基础教程(3)Shell项目构成
Xamarin.FormsShell基础教程(3)Shell项目构成 在创建的ShellDemo解决方案中,有3个子项目,分别为ShellDemo.ShellDemo.Android和ShellDem ...
- PHP计算思源字体宽度, 并把文字绘制到图片上
2019-6-19 9:18:54 星期三 思源字体是一套开源的字体, 那字体宽度是多少呢? 测试场景, 将包含汉字, 数字, 大小写字符的一段文字写到图片中去, 但不能出现超出的情况, 这就要计算 ...
- (转)How To Create a Sudo User on Ubuntu
转自:https://linuxize.com/post/how-to-create-a-sudo-user-on-ubuntu/ The sudo command is designed to al ...
- WEB程序设计 第7版
WEB程序设计 第7版 D11章基础知识1.1internet简介1.1.1起源1.1.2internet的含义1.1.3ip地址1.1.4域名1.2万维网1.2.1起源1.2.2web还是int ...
- 工控随笔_C#连接PLC_之_C#入门_03_基本数据类型
using System; using System.Collections.Generic; using System.Linq; using System.Text; //namespace 关键 ...
- 阿里开源 OpenJDK 发行版 Dragonwell
日有消息显示,阿里将于 21 日重磅发布其 OpenJDK 发行版 Alibaba Dragonwell. 我们知道 OpenJDK 是基于 GPL v2/Classpath Exception 的 ...
- java 方法传值浅拷贝验证
source code import org.junit.Test; public class SigmoidTruncatedSuit { @Test public void main_test() ...
- IBM X3650 M4 M5 设置服务器用UEFI模式启动支持磁盘GPT分区
1 系统启动 2 按 F1 3 进入BIOS 4 进入 System Configuration 5 找到 Boot Manager 6 找到Boot Modes 7 进入Boot Modes, 找到 ...