react 實現評分組件

来源:https://www.cnblogs.com/reaf/archive/2019/07/05/11136677.html
-Advertisement-
Play Games

寫了個評分組件,效果如下 組件Rate.js 組件樣式 Rate.less 背景圖 調用 <Rate number={10} def={5} /> number:為評分總數,預設為5 def:為評分數,預設為0 ...


寫了個評分組件,效果如下

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

 


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • vue中watch如何監控對象的屬性、Watch和computed的區別 ...
  • Vue 實例有一個完整的生命周期,也就是從開始創建、初始化數據、編譯模板、掛載Dom→渲染、更新→渲染、卸載等一系列過程,我們稱這是 Vue 的生命周期。通俗說就是 Vue 實例從創建到銷毀的過程,就是生命周期。 destroyed : 子父組件的生命周期 僅當子組件完成掛載後,父組件才會掛載 當子 ...
  • axios 設置headers token ...
  • 2019-07-03T01:39:51.691242+08:00 ...
  • vue學習(1) vue-cli 項目搭建 一、windows環境 1. 下載node.js安裝包 官網:https://nodejs.org/en/download/ 選擇LTS下載 2. 安裝 下載完成後點擊安裝包安裝到自己指定的文件夾 windows + r 打開cmd,在命令行中輸入node ...
  • 由於不同瀏覽器,不同版本性能不一,且控制台本質是是套用了一大堆 "eval" ,沙盒化程度高,所以需使用 "node" 環境測試來提高準確性 比第二種方法更簡潔 倒敘簡潔版 兩個分號之間的表達式為 true 會一直執行直到 判斷為 false (i = 0) 正序簡潔版 當 i 大於等於數組長度或a ...
  • 摘要: 玩轉npm。 作者:前端小智 原文: "13 個 npm 快速開發技巧" "Fundebug" 經授權轉載,版權歸原作者所有。 為了保證的可讀性,本文采用意譯而非直譯。 每天,數以百萬計的開發人員使用 或 來構建項目。運行 或`npx create response app`等命令幾乎構建J ...
  • display對象可以獲取所有顯示屏此處演示程式啟動是投放新視窗至另一屏幕 import { app, BrowserWindow } from 'electron' const electron = require('electron') //獲取electron對象 let newWindow ...
一周排行
    -Advertisement-
    Play Games
  • 示例項目結構 在 Visual Studio 中創建一個 WinForms 應用程式後,項目結構如下所示: MyWinFormsApp/ │ ├───Properties/ │ └───Settings.settings │ ├───bin/ │ ├───Debug/ │ └───Release/ ...
  • [STAThread] 特性用於需要與 COM 組件交互的應用程式,尤其是依賴單線程模型(如 Windows Forms 應用程式)的組件。在 STA 模式下,線程擁有自己的消息迴圈,這對於處理用戶界面和某些 COM 組件是必要的。 [STAThread] static void Main(stri ...
  • 在WinForm中使用全局異常捕獲處理 在WinForm應用程式中,全局異常捕獲是確保程式穩定性的關鍵。通過在Program類的Main方法中設置全局異常處理,可以有效地捕獲並處理未預見的異常,從而避免程式崩潰。 註冊全局異常事件 [STAThread] static void Main() { / ...
  • 前言 給大家推薦一款開源的 Winform 控制項庫,可以幫助我們開發更加美觀、漂亮的 WinForm 界面。 項目介紹 SunnyUI.NET 是一個基於 .NET Framework 4.0+、.NET 6、.NET 7 和 .NET 8 的 WinForm 開源控制項庫,同時也提供了工具類庫、擴展 ...
  • 說明 該文章是屬於OverallAuth2.0系列文章,每周更新一篇該系列文章(從0到1完成系統開發)。 該系統文章,我會儘量說的非常詳細,做到不管新手、老手都能看懂。 說明:OverallAuth2.0 是一個簡單、易懂、功能強大的許可權+可視化流程管理系統。 有興趣的朋友,請關註我吧(*^▽^*) ...
  • 一、下載安裝 1.下載git 必須先下載並安裝git,再TortoiseGit下載安裝 git安裝參考教程:https://blog.csdn.net/mukes/article/details/115693833 2.TortoiseGit下載與安裝 TortoiseGit,Git客戶端,32/6 ...
  • 前言 在項目開發過程中,理解數據結構和演算法如同掌握蓋房子的秘訣。演算法不僅能幫助我們編寫高效、優質的代碼,還能解決項目中遇到的各種難題。 給大家推薦一個支持C#的開源免費、新手友好的數據結構與演算法入門教程:Hello演算法。 項目介紹 《Hello Algo》是一本開源免費、新手友好的數據結構與演算法入門 ...
  • 1.生成單個Proto.bat內容 @rem Copyright 2016, Google Inc. @rem All rights reserved. @rem @rem Redistribution and use in source and binary forms, with or with ...
  • 一:背景 1. 講故事 前段時間有位朋友找到我,說他的窗體程式在客戶這邊出現了卡死,讓我幫忙看下怎麼回事?dump也生成了,既然有dump了那就上 windbg 分析吧。 二:WinDbg 分析 1. 為什麼會卡死 窗體程式的卡死,入口門檻很低,後續往下分析就不一定了,不管怎麼說先用 !clrsta ...
  • 前言 人工智慧時代,人臉識別技術已成為安全驗證、身份識別和用戶交互的關鍵工具。 給大家推薦一款.NET 開源提供了強大的人臉識別 API,工具不僅易於集成,還具備高效處理能力。 本文將介紹一款如何利用這些API,為我們的項目添加智能識別的亮點。 項目介紹 GitHub 上擁有 1.2k 星標的 C# ...