473,398 Members | 2,525 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

Javascript to work with CSS units

Hi,

I've got the following problem: i need to build a "progress bar" with
JavaScript. So far i have the following configuration: i've got a DIV
inside of a DIV. Each DIV has a style attribute that defines its width and
other style information.

I need to calculate the size of the incremental block based on various
factor like Width of the outside DIV, incremental interval and number of
increments. My original solution was to assume that all of the units are in
'px' so i could just deal with integers. However now i hit a brick wall,
when i realised that this is anything but scalable.

So what i need to do now is to obtain a width of the DIV from its style
attribute (had it hard coded before), do the calculation, and then use the
result to increment the size of the inner DIV (width element in the style
attribute)

I had a brief look on google and failed to locate anything appropriate. So
i'm hoping that someone can point me in the right direction

Kind regards,
Nick Goloborodko

Jul 23 '05 #1
1 1889
Nick Goloborodko wrote:
Hi,

I've got the following problem: i need to build a "progress bar" with
JavaScript. So far i have the following configuration: i've got a DIV
inside of a DIV. Each DIV has a style attribute that defines its width and
other style information.

I need to calculate the size of the incremental block based on various
factor like Width of the outside DIV, incremental interval and number of
increments. My original solution was to assume that all of the units are in
'px' so i could just deal with integers. However now i hit a brick wall,
when i realised that this is anything but scalable.

So what i need to do now is to obtain a width of the DIV from its style
attribute (had it hard coded before), do the calculation, and then use the
result to increment the size of the inner DIV (width element in the style
attribute)

I had a brief look on google and failed to locate anything appropriate. So
i'm hoping that someone can point me in the right direction

Kind regards,
Nick Goloborodko

I posted code for this about 2 days ago. Here's another version,
it is lightly commented and should do what you wish.

--
Rob.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title> Resize element </title>
<meta http-equiv="Content-Type"
content="text/html; charset=ISO-8859-1">

<style type="text/css">
..div0 {border: 1px solid red; width: 10em; height: 15em;}
..div1 {border: 1px solid blue;}
</style>

<script type="text/javascript">
function sameSize(a,b){
var msg = '';
b = document.getElementById(b);

// Zilla stuff
if (window.getComputedStyle) {
var h = document.defaultView.getComputedStyle(a,
'').getPropertyValue('height');
var w = document.defaultView.getComputedStyle(a,
'').getPropertyValue('width');
msg += 'getComputedStyle (h,w): ' + h + ', ' + w;

// IE stuff
} else if (a.currentStyle) {
var h = eval('a.currentStyle.height');
var w = eval('a.currentStyle.width');
msg += 'currentStyle (h,w): ' + h + ', ' + w;
}

// Split off numbers and units
hu = h.replace(/[\d|.]/g,'');
//h = h.replace(/[^\d|.]/g,'');
h = h.replace(hu,'');
wu = w.replace(/[\d|.]/g,'');
//w = w.replace(/[^\d|.]/g,'');
w = w.replace(wu,'');

// Apply to the target element
if (b.style) {
msg += '<br>Setting ' + b.id + ' to (h,w):'
+ h + hu + ', ' + w + wu;
b.style.height = h + hu;
b.style.width = w + wu;
}

document.getElementById('div0').innerHTML = msg;
}
function slideOut(el) {
if (el.timer) window.clearTimeout(el.timer);
var h0 = 0, // Current height
w0 = 0, // Current width
hu, wu; // Height & width units
if (window.getComputedStyle) {
var h = document.defaultView.getComputedStyle(el,
'').getPropertyValue('height');
var w = document.defaultView.getComputedStyle(el,
'').getPropertyValue('width');
} else if (el.currentStyle) {
var h = eval('el.currentStyle.height');
var w = eval('el.currentStyle.width');
}

hu = h.replace(/[\d|.]/g,'');
h = h.replace(hu,'');
wu = w.replace(/[\d|.]/g,'');
w = w.replace(wu,'');
var d = 10; // Delay between increments
var inc = 200; // Number of increments
var hi = h/inc; // Height increments
var wi = w/inc; // Width increments

el.style.height = h0 + hu;
el.style.width = w0 + wu;
el.style.display = '';

el.timer = window.setInterval(function() { // Start timer

h0 += hi;
w0 += wi;
if ( h0 > h) {
h0 = h;
w0 = w;
window.clearTimeout(el.timer); // Stop timer
el.timer = null; // Clear timer
}

el.style.height = h0 + hu;
el.style.width = w0 + wu;

}, d); // End timer code
}

</script>
</head>
<body>

<div id="div0" class="div0" onclick="sameSize(this,'div1');">
This is div0,click on me to make div1 the same size</div>
<div id="div1" class="div1" onclick="slideOut(this);">div1</div>

</body>
</html>
Jul 23 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Dan Webb | last post by:
Hi All, Im currently working on ways of pacakaging javascript functions/variables/objects in a similar way to the Java package statement so that scripts can be interact with each other with the...
15
by: binnyva | last post by:
Hello Everyone, I have just compleated a JavaScript tutorial and publishing the draft(or the beta version, as I like to call it) for review. This is not open to public yet. The Tutorial is...
26
by: Patient Guy | last post by:
Has anyone written code that successfully manipulates binary file data using Javascript? It might---and in the case of doing I/O, will---make use of browser- specific functions (ActiveX/COM with...
2
by: martin | last post by:
Can someone please help me with an issue I have with Firefox that generates in the console below when running the code below.Strange thing is that IE runs it no problem. I don't think it's an...
34
by: electrician | last post by:
Perl has it, Basic has it, Fortran has it. What is so difficult about creating a goto command for JavaScript. Just set up a label and say go to it.
4
by: =?Utf-8?B?R1ROMTcwNzc3?= | last post by:
Hi Guys, thanks for your help yesterday, I've got one more question, then I think I'm done for now,... Is it possible to insert recordset data in a javascript, for instance I have a javascript...
2
by: haft | last post by:
My webpage of discussion contains a javascript powered image gallery of small thumbnail images that when moused-over, display a larger image that corresponds to the current moused-over thumbnail....
16
by: drwyness | last post by:
Hello, Please help with the following problem it is causing me some headaches. The following javascript code is designed to fill in text boxes with numbers on an online game (tribalwars). It...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.