简体   繁体   English

反应:如果其他逻辑大于100

[英]REACT: If Else logic, greater than 100

I have a react app that has a slider that moves icons between 0 and 100% by 2 inputs, x and y . 我有一个React应用程序,它具有一个滑块,可通过2个输入xy在0和100%之间移动图标。 But when the y input is less than x , it shoots the icons LEFT style property pass the range slider. 但是,当y输入小于x时 ,它将通过范围滑块拍摄图标LEFT 样式属性

I want to create the logic so that if the icon LEFT surpasses 100 , it stays at 100 我想创建逻辑,以便如果图标LEFT超过100 ,它将保持在100

for example, x = 5 and y = 10 so the icon would be half way at 50% 例如, x = 5y = 10,因此图标在50%处处于中间位置

but if x = 5 and y = 4 , the left property which would be 125%, the icon shoots and doesnt pass the slider but stays at 100%. 但如果x = 5且y = 4 ,则left属性为125%,则图标会射击并且不会通过滑块,但会保持100%的状态。

My issue is when I run the for loop , it moves all even less than 100 % to 100% 我的问题是,当我运行for循环时 ,它会将所有小于100 %的东西移动到100%

so if the icon was 10%, after the script it gets a value of 100% 因此,如果图标为10%,则脚本后它的值为100%

for(var i =0; i<= arrayOfIcons.length; i++) {
    if(arrayOfIcons[i].style.left <= '100%') {
        console.log(arrayOfIcons[i].style.left)
    } else {
        arrayOfIcons[i].style.left = '100%'
        console.log('greater than 100% : ' + arrayOfIcons[i].style.left)
    }
}

TL:DR if the value of is greater than 100%, i want it to stick to 100% TL:DR如果的值大于100%,我希望它坚持100%

You need to extract numeric value from string with percent symbol. 您需要从带有百分比符号的字符串中提取数值。

function extractNumberFromPercentString(s) {
    return +s.slice(0, -1);
}

for(var i =0; i<= arrayOfIcons.length; i++) {
    if(extractNumberFromPercentString(arrayOfIcons[i].style.left) <= 100) {
        console.log(arrayOfIcons[i].style.left)
    } else {
        arrayOfIcons[i].style.left = '100%'
        console.log('greater than 100% : ' + arrayOfIcons[i].style.left)
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM