Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

JavaScript Forum


You are currently viewing our JavaScript Forum as a guest. Please register to participate.
Login



Reply
Javascript animation code isn't working?
Old 07-03-2010, 10:28 AM Javascript animation code isn't working?
Physicsguy's Avatar
404 - Title not found

Posts: 919
Name: Scott Kaye
Location: Ontario
Trades: 0
HTML Code:
<script type="text/javascript">
function grow() {
  var i=document.getElementById('growBox');
  j = parseFloat(i.getAttribute('height'));
  i.setAttribute('style','height:'+j+'px;background:blue;color:white;position:absolute;');
  i.innerHTML=j;
  j++;
}
setInterval("grow()", 10);
</script>
<div id="growBox" onclick="grow()" height="100" style="background:red;position:absolute;height:100px;">Hello there</div>
I'm wondering why that code isn't working , what it should do is make the blue (or red) box grow at 1px per 10-somethings. It does change to blue, but it fails to grow?

Any ideas? Thanks

-PG
__________________
Check out my
Please login or register to view this content. Registration is FREE
or my
Please login or register to view this content. Registration is FREE
!
Physicsguy is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 07-05-2010, 08:21 AM Re: Javascript animation code isn't working?
Extreme Talker

Posts: 246
Trades: 0
the j variable is being incremented at the end but at the beginning is being overridden, making it so that it is always 100. Here's some code that works.

<html>

<head>

<script type="text/javascript">

var j = 100;

function grow() {

var div = document.getElementById("growBox");

div.style.backgroundColor = '#0000FF';

j = parseFloat(div.offsetHeight);

j++;

div.style.height = j;

div.innerHTML = j;
}

setInterval("grow()", 10);
</script>


</head>

<body>

<div id="growBox" style="background-color: #FF0000; position: absolute; height: 100px; color: #FFFFFF">Hello there</div>

</body>
</html>
__________________

Please login or register to view this content. Registration is FREE


Please login or register to view this content. Registration is FREE


Please login or register to view this content. Registration is FREE
stbuchok is offline
Reply With Quote
View Public Profile
 
Old 07-05-2010, 08:35 AM Re: Javascript animation code isn't working?
Physicsguy's Avatar
404 - Title not found

Posts: 919
Name: Scott Kaye
Location: Ontario
Trades: 0
Thanks! It works like a dream!!

One question, is there an easy way to make it stop and reverse?

Like after it hits a height of 200, stop growing, and then when somebody clicks on it (the blue div), go back to 0?
__________________
Check out my
Please login or register to view this content. Registration is FREE
or my
Please login or register to view this content. Registration is FREE
!
Physicsguy is offline
Reply With Quote
View Public Profile
 
Old 07-05-2010, 08:44 AM Re: Javascript animation code isn't working?
Extreme Talker

Posts: 246
Trades: 0
Something like this?

<html>

<head>

<script type="text/javascript">

var j = 100;

function grow() {

var div = document.getElementById("growBox");

div.style.backgroundColor = '#0000FF';

j = parseFloat(div.offsetHeight);

j++;

div.style.height = j;

div.innerHTML = j;

if(j < 200){

setTimeout('grow()', 10);
}
else
{
setTimeout('shrink()', 10);
}
}

function shrink() {

var div = document.getElementById("growBox");

div.style.backgroundColor = '#0000FF';

j = parseFloat(div.offsetHeight);

j--;

div.style.height = j;

div.innerHTML = j;

if(j > 100){

setTimeout('shrink()', 10);
}
else
{
setTimeout('grow()', 10);
}
}

</script>


</head>

<body onload="grow();">

<div id="growBox" style="background-color: #FF0000; position: absolute; height: 100px; color: #FFFFFF">Hello there</div>

</body>
</html>
__________________

Please login or register to view this content. Registration is FREE


Please login or register to view this content. Registration is FREE


Please login or register to view this content. Registration is FREE
stbuchok is offline
Reply With Quote
View Public Profile
 
Old 07-06-2010, 10:15 AM Re: Javascript animation code isn't working?
Physicsguy's Avatar
404 - Title not found

Posts: 919
Name: Scott Kaye
Location: Ontario
Trades: 0
Yes! Thank you
__________________
Check out my
Please login or register to view this content. Registration is FREE
or my
Please login or register to view this content. Registration is FREE
!
Physicsguy is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Javascript animation code isn't working?
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.36559 seconds with 12 queries