简体   繁体   English

如何使用Javascript变量作为CSS宽度的值?

[英]How can I use a Javascript variable as a value for CSS width?

I have a designed a scholarship donation bar and would like the bar width to extend as the donation amount increases. 我有一个奖学金捐赠酒吧,并希望随着捐赠金额的增加,酒吧宽度会延长。 I currently have the bar set where it can extend by changing the "width" using CSS. 我目前通过使用CSS更改“宽度”来设置可以扩展的栏。 I am not as familiar with Javascript but I attempted to use a for loop and if statements in order yield the result I am looking for. 我不熟悉Javascript,但我尝试使用for循环和if语句,以便产生我正在寻找的结果。 The result I am looking for is a loop in which if the donation amount (integer) provided is (0 < amount < 500), the result will be 0. If the donation amount provided is (500 < amount < 1000), the result will be 1. If the donation amount provided is (1000 < amount < 1500), the result will be 2. If the donation amount provided is (1500 < amount < 2000), the result will be 3.If the donation amount provided is (2000 < amount < 2500), the result will be 4.If the donation amount provided is (2500 < amount < 3000), the result will be 5. I need this result to be an integer that will represent a variable for an equation I derived in order to determine the amount of pixels required for the width of the bar in order to properly match the donation amount. 我正在寻找的结果是一个循环,其中如果提供的捐赠金额(整数)是(0 <金额<500),结果将是0.如果提供的捐赠金额是(500 <金额<1000),结果将是1.如果提供的捐赠金额是(1000 <金额<1500),结果将是2.如果提供的捐赠金额是(1500 <金额<2000),结果将是3.如果提供的捐赠金额是(2000 <金额<2500),结果将是4.如果提供的捐赠金额是(2500 <金额<3000),结果将是5.我需要这个结果是一个整数,它将代表一个方程的变量我派生的是为了确定条形宽度所需的像素数量,以便恰当地匹配捐赠金额。 Here is the code I came up with so far. 这是我到目前为止提出的代码。 I need assistance with making my goal come to fruition. 我需要帮助才能实现我的目标。 As of right now the code only displays the last number. 截至目前,代码仅显示最后一个数字。 Thank you in advance! 先感谢您!

<script>
{
    var amount = 1163; //example amount
    var x = 0;

    if (0 < amount < 500) {
        x = 0;
    } if (500 < amount < 1000) {
        x = 1;
    } if (1000 < amount < 1500) {
        x = 2;
    } if (1500 < amount < 2000) {
        x = 3;
    } if (2000 < amount < 2500) {
        x = 4;
    } if (2500 < amount < 3000) {
        x = 5;
    }
        document.write(x)
}
</script>

Comparisons in javascript don't work that way. javascript中的比较不起作用。 You need to specify each logical comparison with ANDS and/or ORS. 您需要使用ANDS和/或ORS指定每个逻辑比较。

if (0 < amount && amount < 500) { ... } // or x < 500 whichever logical proceed

According to operator precedence, this evaluates to (0 < 1163 < 500) 根据运算符优先级,计算结果为(0 < 1163 < 500)

This further evaluates to (true < 500) Since 1 == true , these are all evaulating to true , thus x will always be 5. 这进一步评估为(true < 500)因为1 == true ,所以这些都是true ,因此x将始终为5。

you should avoid logical test and optimize your code that way, which give you the result you expect : 你应该避免逻辑测试并以这种方式优化你的代码,这会给你你期望的结果:
x=Math.round(amount/500+.5)-1;
http://jsfiddle.net/scraaappy/erL37s8k/ http://jsfiddle.net/scraaappy/erL37s8k/

To change the width of an element you don't need to modify the CSS, you can change it directly using javascript. 要更改元素的width ,您不需要修改CSS,您可以使用javascript直接更改它。

document.getElementById("progress").style.width = w + "px";

(assuming the id of the element is "progress" and that the value you want in pixel is w ). (假设元素的id"progress"并且像素中的值是w )。

Check this out.. http://jsfiddle.net/wdth4eme/3/ 看看这个... http://jsfiddle.net/wdth4eme/3/

{
var amount = 1163; //example amount
var x = 0;

if (0 < amount && amount < 500) {
    x = 0;
} if (500 < amount && amount < 1000) {
    x = 1;
} if (1000 < amount && amount < 1500) {
    x = 2;
} if (1500 < amount && amount < 2000) {
    x = 3;
} if (2000 < amount && amount < 2500) {
    x = 4;
} if (2500 < amount && amount < 3000) {
    x = 5;
}
   document.querySelector(".inner").style.width=Math.floor(100/x)+"px";
}

return when you know your in range. 当你知道你的射程时返回。 if its less than a certain number it cant be higher. 如果它小于一定数量则不能更高。

function getValue(value) {
if(value < 500) return 0;
if(value < 1000) return 1;
if(value < 1500) return 2;
...
...
}

var size = getValue(1235)

//returns 2 //返回2

Firstly you need logical operator for a matching purpose eg x>0 && x<=50 首先,您需要逻辑运算符以达到匹配目的,例如x> 0 && x <= 50

Let us say in html, 我们在html中说,

<div id="bar">....</div>

In css, 在css中,

#bar { width: 100px; }

can be accessed by 可以访问

document.getElementById('bar').style.width= "500px";

or

document.getElementById('bar').style.width= x + "px";

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

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