react 实现评分组件
2019-08-14 10:06:51来源:博客园 阅读 ()
写了个评分组件,效果如下

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


调用
原文链接:https://www.cnblogs.com/reaf/p/11136677.html
如有疑问请与原作者联系
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
- js防止表单重复提交实现代码 2020-03-29
- 基于JQuery的多标签实现代码 2020-03-29
- js实现翻页后保持checkbox选中状态的实现方法 2020-03-25
- NiftyCube实现圆角边框的方法 2020-03-20
- JS实现标签页切换效果 2020-03-12
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash
