[英]Dynamic CSS where the number of elements is variable
So I have some code that produces a table (although not using table tags as that would obviously be wrong!) where each row has a bar that is coloured in depending on a percentage. 因此,我有一些代码可以生成一个表(尽管不使用表标签,因为这显然是错误的!),其中每行都有一个根据百分比进行着色的条形图。 This is currently done using inline styles as you can see below (using the smarty template system).
当前,这是使用嵌入式样式完成的,如下所示(使用smarty模板系统)。
I want to move move these styles to the stylesheet but I'm not sure how I can make the classes dynamic as I don't know how many rows there will ever be. 我想将这些样式移动到样式表中,但是我不确定如何使类动态化,因为我不知道会有多少行。
<div class="fullbar">
<div class="bar">
{if $responses[all].responses eq 0}
<div class="innerbar allna" style="width: 100%;"> All responses marked as n/a</div>
{elseif $responses[all].score lt $withholdBelow}
<div class="innerbar shortbar" style="width: {$withholdBelow * 10}%;"> Withheld - Less than {$withholdBelow}</div>
{else}
{if $responses[all].responses lt 4}
<div class="innerbar na" style="width: {$responses[all].score * 10}%;"> Two or more responses n/a</div>
{else}
<div class="innerbar" style="width: {$responses[all].score * 10}%;"></div>
{/if}
{/if}
</div>
<div class="scale-container-results">
<div class="scaleleft-results">Totally Disagree</div>
<div class="scalecenter-results">Neutral</div>
<div class="scaleright-results">Totally Agree</div>
</div>
</div>
Is what I am asking possible and if so how? 我要问的可能吗?如果可以,怎么办?
I think you're trying to create a progressbar. 我认为您正在尝试创建进度条。 There's nothing wrong with using classes for the shared properties and inline styling to set the dynamic properties, eg a
width: 43%;
将类用于共享属性和内联样式来设置动态属性没有错,例如,
width: 43%;
for the percentage of the progressbar, as you already do. 进度条的百分比,就像您已经做的那样。
This is exactly how for example Bootstrap does it, see http://getbootstrap.com/components/#progress 例如,这正是Bootstrap的工作方式,请参见http://getbootstrap.com/components/#progress
It's ok as it is, and I don't think there's a way to do it with css, except creating a class for each 10% increment (and that's supposing that the scores go from 0 to 10 without decimals in increments of 1) like: 这样就可以了,我认为没有办法用CSS来做到这一点,除了为每个10%的增量创建一个类(并且假设分数从0到10且没有小数的1) :
.w_0{width:0;}
.w_1{width:10%;}
.w_2{width:20%;}
and so on and then in your html: 等等,然后在您的html中:
<div class="innerbar na w_{$responses[all].score}"></div>
another option would be using jquery: 另一个选择是使用jQuery:
<div class="innerbar na" data-width="{$responses[all].score}"></div>
processing the value in data-width with jquery to calculate and change the width of the layer 使用jquery处理data-width中的值以计算和更改图层的宽度
$('.innerbar').each(function(){
bar_width=$(this).data('width');
$(this).width(bar_width*10+'%');});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.