/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] Progress Bar problem

I’m trying to make a simple progress bar.
The problem is that the initial display width, although set to 50%, shows much less.
Adding more or less (range 0..100) to the percentage, has very little effect.

Can someone spot what I’m doing wrong here, either in the CSS or the math?

[code]
<html>
<head>
<title>progressbar Demo</title>

<style type=”text/css”>
.chart {position:relative; width:100%; background:#AAA; }
.bar {position:absolute; left:0px; background:#0F0; color:#000; }
</style>

<script type=”text/javascript”>
var maxChart = 100;
function moveBar(amt) {
var sel = document.getElementById(‘bar’);
var v = sel.style.width;
v = v.substr(0,v.indexOf(‘%’));
v = ((Number(v)+Number(amt))/maxChart)*maxChart;
if (v < 0) { v = 0; }
if (v > maxChart) { v = maxChart; }
v = v.toFixed(0)+’%’;
sel.style.width = v;
sel.innerHTML = v;
}
</script>
</head>
<body>
<div style=”width:100px; background-color: #888″>
<span class=”chart” id=”chart”>&nbsp;
<span class=”bar” id=”bar” style=”width:50%”>50%</span>
</span>
</div>

<button onclick=”moveBar(-5)”>Less</button>
<button onclick=”moveBar(5)”>More</button>

</body>
</html>
[/code]

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@FangMar 27.2010 — &lt;html&gt;
&lt;head&gt;
&lt;title&gt;progressbar Demo&lt;/title&gt;

&lt;style type="text/css"&gt;
.bar {background:#0F0; color:#000; }
&lt;/style&gt;

&lt;script type="text/javascript"&gt;
var maxChart = 100;
function moveBar(amt) {
var sel = document.getElementById('bar');
var v = sel.style.width;
v = v.substr(0,v.indexOf('&amp;#37;'));
v = ((Number(v)+Number(amt))/maxChart)*maxChart;
if (v &lt; 0) { v = 0; }
if (v &gt; maxChart) { v = maxChart; }
v = v.toFixed(0)+'%';
sel.style.width = v;
sel.innerHTML = v;
}
&lt;/script&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;div style="width:100px; background-color: #888"&gt;
&lt;div class="bar" id="bar" style="width:50%"&gt;50%&lt;/div&gt;
&lt;/div&gt;

&lt;button onclick="moveBar(-5)"&gt;Less&lt;/button&gt;
&lt;button onclick="moveBar(5)"&gt;More&lt;/button&gt;

&lt;/body&gt;
&lt;/html&gt;
Copy linkTweet thisAlerts:
@JMRKERauthorMar 27.2010 — So the problem was the combination of using the nested <span> tags.

My initial thoughts in my version were that the outer <div> was to position the display

while the CSS chart span would show the outer bounds of the progress bar

while the inner CSS bar span would show the actual progress.

Thanks for the more efficient code and the correction.

Again, thank you 'Fang'.

?
×

Success!

Help @JMRKER spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.29,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...