问题描述
我使用 Python 作为我的 React 前端的后端,现在我的后端正在传递 17 或 87 之类的值,我想将该值用作我正在建立的规模的百分比.现在,您可以看到刻度从中间开始:
而且它不会移动到任何地方,因为后端还没有给它一个数字.但是如果后端给出 23 的分数,三角形将移动到 div 的 23% 处(在黑色边框中勾勒出来):
这是我的 React 前端 HTML 部分的代码:
<div类名={!this.state.listening?结果容器":'结果容器关闭'}><div className="结果包装器">{this.state.score === ''?'点击记录并查看你的分数': '你的分数:' + this.state.score}</div></div><div className=规模容器"><div className="bar"><div className=""向上箭头"></div><div className=""scale-meter"></div><span></span></div></div>this.state.score 是我从后端获取分数时存储分数的位置.
这是我的 CSS:
.arrow-up {宽度:0;高度:0;左边框:25px 纯透明;边框右:25px纯透明;文本对齐:居中;行高:35px;位置:绝对;顶部:54px;z-index:1;边框底部:25px 纯紫色;动画:缩放 4s 缓动;}@keyframes 缩放 {从 {左:48%;}}.bar:nth-child(1) .arrow-up{左:23%;}.bar:nth-child(1) .arrow-up 部分是我希望根据分数更改 left 的位置.有没有一种方法可以做到这一点,而不是硬编码 left 值,它可以是 left: score%; 之类的东西?
您可以使用内联样式,其中 this.state.score 将是您的 left 属性值:
style={{ left: `${this.state.score}%` }}您可以使用反引号将 % 字符串与末尾的 score 值连接起来:
<div类名={!this.state.listening?结果容器":'结果容器关闭'}><div className="结果包装器">{this.state.score === ''?'点击记录并查看你的分数': '你的分数:' + this.state.score}</div></div><div className=规模容器"><div className="bar">I'm using Python as the backend for my React frontend and right now my backend is passing in values like 17 or 87 and I want to use that value as a percentage in the scale I'm building. Right now, you can see that the scale starts in the middle:
And it doesn't move anywhere because the backend hasn't given it a number yet. But if the backend gave a score of 23, the triangle would move to exactly 23% of the div (outlined in the black border):
Here is the code in the HTML part of my React frontend:
<div
className={
!this.state.listening
? 'results-container'
: 'results-container-closed'
}
>
<div className="result-wrapper">
{this.state.score === ''
? 'Click record and check your score'
: 'Your Score: ' + this.state.score}
</div>
</div>
<div className="scale-container">
<div className="bar">
<div className="arrow-up"></div>
<div className="scale-meter"></div>
<span></span>
</div>
</div>
this.state.score is where the score is stored when I get it from the backend.
This is my CSS:
.arrow-up {
width: 0;
height: 0;
border-left: 25px solid transparent;
border-right: 25px solid transparent;
text-align: center;
line-height: 35px;
position: absolute;
top: 54px;
z-index: 1;
border-bottom: 25px solid purple;
animation: scale 4s ease;
}
@keyframes scale {
from {
left: 48%;
}
}
.bar:nth-child(1) .arrow-up{
left: 23%;
}
The .bar:nth-child(1) .arrow-up part is where I want the left to be changed based on the score. Is there a way I can make it so that instead of me hardcoding the left value, it can be something like left: score%;?
解决方案 You can use inline styling where this.state.score will be your value for left property:
style={{ left: `${this.state.score}%` }}
You can use back-ticks to concatenate % string with score value at the end:
<div
className={
!this.state.listening
? 'results-container'
: 'results-container-closed'
}
>
<div className="result-wrapper">
{this.state.score === ''
? 'Click record and check your score'
: 'Your Score: ' + this.state.score}
</div>
</div>
<div className="scale-container">
<div className="bar">
<div className="arrow-up" style={{ left: `${this.state.score}%` }}></div>
<div className="scale-meter"></div>
<span></span>
</div>
</div>
这篇关于如何将值从后端传递到 React 前端并在 CSS 中将该值用于前端?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!
The End
相关推荐
1. 如果是ajax嵌套了 页面, 请确保 只有最外层的页面引入了layui.css 和 layui.js ,内层页面 切记不要再次引入 2. 具体代码如下 layui.use(['form', 'upload'], function(){ var form = layui.form; form.render(); // 加入这一句});...
2024-11-09
前端开发问题
313
经常用到layui的朋友都知道,layui tree默认是不能自定义图标的,那么我们要自定义的话要怎么操作呢? 首先用编辑器软件(修改时候用编辑器记得编码),打开layui.js。搜索: i class="layui-icon layui-icon-file" 改为如下代码: i class="'+ (i.icon || "l...
2024-10-26
前端开发问题
493
问题描述 我想改变layui时间日历布局大小,这个要怎么操作呢? 解决办法 可以用css样式对时间日历进行重新布局,具体代码如下: !DOCTYPE htmlhtmlheadmeta charset="UTF-8"title/titlelink rel="stylesheet" href="../../layui/css/layui.css" /style#test-...
2024-10-24
前端开发问题
271
quot;Each child in an array should have a unique key propquot; only on first time render of page(“数组中的每个孩子都应该有一个唯一的 key prop仅在第一次呈现页面时)...
2024-04-20
前端开发问题
5
CoffeeScript always returns in anonymous function(CoffeeScript 总是以匿名函数返回)...
2024-04-20
前端开发问题
13
getFullYear returns year before on first day of year(getFullYear 在一年的第一天返回前一年)...
2024-04-20
前端开发问题
6
热门文章
1错误 [ERR_REQUIRE_ESM]:不支持 ES 模块的 require()
2vue中yarn install报错:info There appears to be trouble with you
3为什么 Chrome(在 Electron 内部)会突然重定向到 chrome-error://chromewebdat
4“aria-hidden 元素不包含可聚焦元素"显示模态时的问题
5使用选择器在 CSS 中选择元素的前一个兄弟
6js报错:Uncaught SyntaxError: Unexpected string
7layui怎么刷新当前页面?
8将模式设置为“no-cors"时使用 fetch 访问 API 时出错
热门精品源码
最新VIP资源
1多功能实用站长工具箱html功能模板
2多风格简历在线生成程序网页模板
3论文相似度查询系统源码
4响应式旅游景点宣传推广页面模板
5在线起名宣传推广网站源码
6酷黑微信小程序网站开发宣传页模板
7房产销售交易中介网站模板
8小学作业自动生成程序


大气响应式网络建站服务公司织梦模板
高端大气html5设计公司网站源码
织梦dede网页模板下载素材销售下载站平台(带会员中心带筛选)
财税代理公司注册代理记账网站织梦模板(带手机端)
成人高考自考在职研究生教育机构网站源码(带手机端)
高端HTML5响应式企业集团通用类网站织梦模板(自适应手机端)