dongxunhua2054 2013-01-09 18:31
浏览 93
已采纳

编写基于动态PHP css的进度条 - 数学百分比问题

I'm coding a PHP progress bar that will update itself via a percent number that is given to it but I'm having issues working out the percents.

My code currently is as follows:

PHP

$percent = $_GET['percent'];

if($percent < 5)
    //low-image.jpg

elseif($percent > 95)
   //high-image.jpg

else{
$lowpercent = 100 - $percent;

$highwidth = 270 / 100 * $percent;
$lowwidth = 270 / 100 * $lowpercent;

HTML

<div class="first"></div>
<div class="mid-first" style="width:<?=$highwidth?>px;"></div>
<div class="mid-second" style="width:<?=$lowwidth?>px;"></div>
<div class="last"></div>

CSS

.first. mid-first, .mid-second, .last{
    display:block;
    float:left;
    height:62px;
    }
.first{
    background:url(low.png);
    width:31px;
}
.mid-first{
    background:url(mid-first.png) repeat-x;
}
.mid-second{
    background:url(mid-second.png);
}
.last{
    background:url(high.png);
    width:32px;
}

The Problem

At the moment the percents are worked out slightly incorrectly, my maths brain seems to have been misplaced today...

There are 4 divs, the first and last div take up 5% each so 10%, the middle divs then equal the other 90%.

This means when the figure 50 is given passed in via the $_GET it will work out 50% of the middle bars not including the 5% first bar, which is wrong, it should take the first 5% into account and then work out the 50% pixel widths?

How can I change the maths behind the percent to fix the two middle bars so that when 50% is applied both middle bars are equal in pixels?

  • 写回答

3条回答 默认 最新

  • doulun1939 2013-01-09 18:46
    关注

    There's no good reason to use pixels at all. Wrap your divs inside a containing div and use percentages in your CSS.

    PHP:

    $lowpercent = 100 - $percent;
    

    HTML:

    <div class="barwrapper">
        <div class="first"></div>
        <div class="mid-first" style="width:<?=($percent-5)?>px;"></div>
        <div class="mid-second" style="width:<?=($lowpercent-5)?>px;"></div>
        <div class="last"></div>
    </div>
    

    CSS:

    .first{
        background:url(low.png);
        width:5%;
    }
    .last{
        background:url(high.png);
        width:5%;
    }
    

    OR, if you don't want first and last to be taken away from the 100%:

    <div class="first"></div>
    <div class="barwrapper">
        <div class="mid-first" style="width:<?=($percent)?>px;"></div>
        <div class="mid-second" style="width:<?=($lowpercent)?>px;"></div>
    </div>
    <div class="last"></div>
    

    CSS:

    .first{
        background:url(low.png);
        width:30px;
    }
    .last{
        background:url(high.png);
        width:30px;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 51单片机中C语言怎么做到下面类似的功能的函数(相关搜索:c语言)
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起