Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 07-28-2011, 07:14 PM   PM User | #1
eddo
New to the CF scene

 
Join Date: Jul 2011
Posts: 4
Thanks: 2
Thanked 0 Times in 0 Posts
eddo is an unknown quantity at this point
Problems with simple object fade code

Hi all. I'm learning JS from pretty much scratch and the first thing I'm trying to do is simply make an image fade function. I just can't seem to make it work though, the image starts off at the right opacity (im this case 0.5) but after that the page goes white and the script never stops. It does seem to be counting up normally "opacity: 0.6 opacity: 0.7" etc. though.

Code:
			<img src="1.jpg" id="pic1">
	
			<script type="text/javascript">
				var amount = 0.5;
				var target = document.getElementById('pic1');
				
				function fade() {				
											
				if (target.style.opacity < 1.0) {
					
					target.style.opacity = amount;
					amount = amount + 0.1;

					document.write("<br>opacity: " + target.style.opacity);
					
					setTimeout("fade()", 1000)					
					//fade();
					}
					
				}
				
				fade();

			</script>
edit I rewrote it to make it simpler but still having basically the same problem D:!

Last edited by eddo; 07-29-2011 at 08:16 AM..
eddo is offline   Reply With Quote
Old 07-29-2011, 08:17 AM   PM User | #2
eddo
New to the CF scene

 
Join Date: Jul 2011
Posts: 4
Thanks: 2
Thanked 0 Times in 0 Posts
eddo is an unknown quantity at this point
Any ideas on this one? I rewrote it to make it simpler but still getting the same basic problem :/
eddo is offline   Reply With Quote
Old 07-29-2011, 08:38 AM   PM User | #3
vwphillips
Senior Coder

 
Join Date: Mar 2005
Location: Portsmouth UK
Posts: 4,355
Thanks: 3
Thanked 458 Times in 445 Posts
vwphillips is a jewel in the roughvwphillips is a jewel in the roughvwphillips is a jewel in the rough
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<head>
  <title></title>
</head>

<body>
			<img src="http://www.vicsjavascripts.org.uk/StdImages/Egypt5.jpg" id="pic1">

			<script type="text/javascript">
				var amount = 0.5;
				var target = document.getElementById('pic1');

				function fade() {

				if (target.style.opacity < 1.0) {

					target.style.opacity = amount;
					amount = amount + 0.1;

					target.style.opacity=amount;

					setTimeout("fade()", 1000)
					//fade();
					}

				}

				fade();

			</script>
</body>

</html>
__________________
Vic

God Loves You and will never love you less.

http://www.vicsjavascripts.org.uk/

If my post has been useful please donate to http://www.operationsmile.org.uk/
vwphillips is offline   Reply With Quote
Users who have thanked vwphillips for this post:
eddo (07-29-2011)
Old 07-29-2011, 10:47 AM   PM User | #4
eddo
New to the CF scene

 
Join Date: Jul 2011
Posts: 4
Thanks: 2
Thanked 0 Times in 0 Posts
eddo is an unknown quantity at this point
Dang, I'd tried that before and it didn't work so I was trying to make the reverse work instead. No idea what I did wrong the first time! Many thanks.
eddo is offline   Reply With Quote
Old 07-29-2011, 10:57 AM   PM User | #5
devnull69
Senior Coder

 
Join Date: Dec 2010
Posts: 2,245
Thanks: 10
Thanked 531 Times in 525 Posts
devnull69 will become famous soon enough
What went wrong? It's the document.write() .... this will always create a new document if applied after page load. So document.write() is really ONLY useful to show content during the initial load of the page.
devnull69 is offline   Reply With Quote
Users who have thanked devnull69 for this post:
eddo (07-29-2011)
Old 07-29-2011, 12:20 PM   PM User | #6
eddo
New to the CF scene

 
Join Date: Jul 2011
Posts: 4
Thanks: 2
Thanked 0 Times in 0 Posts
eddo is an unknown quantity at this point
Quote:
Originally Posted by devnull69 View Post
What went wrong? It's the document.write() .... this will always create a new document if applied after page load. So document.write() is really ONLY useful to show content during the initial load of the page.
Ahhhhhhhhhhhhh so that's what it was! So what can I use if I want to print a var's current value for debugging purposes?
eddo is offline   Reply With Quote
Old 07-29-2011, 01:18 PM   PM User | #7
devnull69
Senior Coder

 
Join Date: Dec 2010
Posts: 2,245
Thanks: 10
Thanked 531 Times in 525 Posts
devnull69 will become famous soon enough
It depends on which browser you are using. You can use

1. console.log() in some cases, if you have the possibility to access the javascript console or you have extensions like Firebug for Firefox or the Development Tools for IE
2. alert() ... which shows you a dialogue window
3.
Code:
HTML
<div id="messagearea"></div>

Javascript:
document.getElementById('messagearea').innerHTML = "This is a message";
devnull69 is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

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

Forum Jump


All times are GMT +1. The time now is 07:29 PM.


Advertisement
Log in to turn off these ads.