phpacademy Forum

Watch, Listen, Learn!

Tutorial: Page Fade In

Share your own code here.

Tutorial: Page Fade In

Postby zztechjoezz » Tue Jul 27, 2010 10:14 pm

I just thought that I would share with you how to make a page fade in using jQuery. It is used on many websites, but one big example is Google. It doesn't just have to be used for a page, it can be used for specific tags in a document.

So here it is:
  1. <html>
  2. <head>
  3. <script type="text/javascript" src="http://code.jquery.com/jquery-1.4.2.js"></script>
  4. <script type="text/javascript">
  5. $(document).ready(function(){   
  6.     $('#container').fadeIn(3000);
  7. });
  8. </script>
  9. </head>
  10.  
  11. <body>
  12. <div id="container" style="display:none; text-align:center;">
  13. Content will fade in Here
  14. <img src="http://www.phpacademy.org/forum/phpacademylogo-225x35.gif">
  15. </div>
  16. </body
  17. </html>


BTW I know that the document isn't set out properly, but I just wanted to focus on the jQuery :D
Please give Karma if I've helped :D
Find me on Social Networks:
I am 'zztechjoezz' everywhere!
User avatar
zztechjoezz
 
Posts: 54
Joined: Tue Jun 15, 2010 3:43 pm
Location: Leeds, UK.
Online: 11h 22m
Karma: 1

Re: Tutorial: Page Fade In

Advertisment

Advertisment
 

Re: Tutorial: Page Fade In

Postby alex » Tue Jul 27, 2010 10:17 pm

Thanks for this.

Moved to Share > PHP Code
User avatar
alex
Founder
 
Posts: 471
Joined: Mon Apr 20, 2009 9:55 pm
Location: London, UK.
Online: 4d 4h 2m 46s
Karma: 11

Re: Tutorial: Page Fade In

Postby zztechjoezz » Tue Jul 27, 2010 10:19 pm

Oops. Sorry Alex, I thought that this section was for PHP code only. Thanks for moving it.
Find me on Social Networks:
I am 'zztechjoezz' everywhere!
User avatar
zztechjoezz
 
Posts: 54
Joined: Tue Jun 15, 2010 3:43 pm
Location: Leeds, UK.
Online: 11h 22m
Karma: 1

Re: Tutorial: Page Fade In

Postby wide_load » Tue Jul 27, 2010 10:23 pm

jQuery = cheating.

this is really just one function call...

I wrote my own fade function instead of relying on jquery

  1. // used by fade.
  2. function perform_fade(e, from, to, total_time, last_time, time_remain){
  3.     var current_time = new Date().getTime();
  4.    
  5.     var new_opacity = ((time_remain / total_time) * (from - to)) + to;
  6.     if (new_opacity < 0){ new_opacity *= -1; }
  7.    
  8.     e.style.opacity = new_opacity;
  9.     e.style.filter = 'alpha(opacity=' + Math.round(new_opacity * 100) + ')';
  10.        
  11.     if (time_remain > 0){
  12.         e.fading = true;
  13.         window.setTimeout(function(){ perform_fade(e, from, to, total_time, current_time, time_remain - (current_time - last_time)); }, 25);
  14.     }else{
  15.         e.fading = false;
  16.     }
  17. }
  18.  
  19. // fade an element.
  20. function fade(e, from, to, time){   
  21.     if (e.fading !== true){
  22.         e.style.opacity = from;
  23.         e.style.filter = 'alpha(opacity=' + from * 100 + ')';
  24.         window.setTimeout(function(){ perform_fade(e, from, to, time, new Date().getTime(), time); }, 25);
  25.     }
  26. }


I don't mean to offend, but is there really any point sharing something that is just one function call.
Image
User avatar
wide_load
Top Contributor
 
Posts: 5375
Joined: Thu Aug 13, 2009 1:04 pm
Online: 12d 14h 21m 3s
Karma: 43

Re: Tutorial: Page Fade In

Postby zztechjoezz » Tue Jul 27, 2010 10:27 pm

wide_load wrote:jQuery = cheating.

this is really just one function call...

I wrote my own fade function instead of relying on jquery

  1. // used by fade.
  2. function perform_fade(e, from, to, total_time, last_time, time_remain){
  3.     var current_time = new Date().getTime();
  4.    
  5.     var new_opacity = ((time_remain / total_time) * (from - to)) + to;
  6.     if (new_opacity < 0){ new_opacity *= -1; }
  7.    
  8.     e.style.opacity = new_opacity;
  9.     e.style.filter = 'alpha(opacity=' + Math.round(new_opacity * 100) + ')';
  10.        
  11.     if (time_remain > 0){
  12.         e.fading = true;
  13.         window.setTimeout(function(){ perform_fade(e, from, to, total_time, current_time, time_remain - (current_time - last_time)); }, 25);
  14.     }else{
  15.         e.fading = false;
  16.     }
  17. }
  18.  
  19. // fade an element.
  20. function fade(e, from, to, time){   
  21.     if (e.fading !== true){
  22.         e.style.opacity = from;
  23.         e.style.filter = 'alpha(opacity=' + from * 100 + ')';
  24.         window.setTimeout(function(){ perform_fade(e, from, to, time, new Date().getTime(), time); }, 25);
  25.     }
  26. }


I don't mean to offend, but is there really any point sharing something that is just one function call.


I just thought that I would share it, because I haven't seen anyone post anything like this yet. There are other more complex ways around it(such as your javascript method), but I find that is easier to use jQuery. I can see your point, but it is really just so people know how to do it.
Find me on Social Networks:
I am 'zztechjoezz' everywhere!
User avatar
zztechjoezz
 
Posts: 54
Joined: Tue Jun 15, 2010 3:43 pm
Location: Leeds, UK.
Online: 11h 22m
Karma: 1

Re: Tutorial: Page Fade In

Postby wide_load » Tue Jul 27, 2010 10:31 pm

zztechjoezz wrote:I just thought that I would share it, because I haven't seen anyone post anything like this yet. There are other more complex ways around it(such as your javascript method), but I find that is easier to use jQuery. I can see your point, but it is really just so people know how to do it.


that's the definition of what I think is wrong with jQuery. People use it because its easy, despite being less efficient. It also prevent them learning real javascript, which eventually limits what you can do.
Image
User avatar
wide_load
Top Contributor
 
Posts: 5375
Joined: Thu Aug 13, 2009 1:04 pm
Online: 12d 14h 21m 3s
Karma: 43

Re: Tutorial: Page Fade In

Postby zztechjoezz » Tue Jul 27, 2010 10:34 pm

wide_load wrote:that's the definition of what I think is wrong with jQuery. People use it because its easy, despite being less efficient. It also prevent them learning real javascript, which eventually limits what you can do.


I can see where your coming from. I am still trying to learn javascript, but at the moment I am finding jQuery easier to use. I am not going to leech off jQuery for everything. I am actually trying to learn Javascript.
Find me on Social Networks:
I am 'zztechjoezz' everywhere!
User avatar
zztechjoezz
 
Posts: 54
Joined: Tue Jun 15, 2010 3:43 pm
Location: Leeds, UK.
Online: 11h 22m
Karma: 1

Re: Tutorial: Page Fade In

Postby CometJack » Fri Jul 30, 2010 12:49 am

Sorry but this isn't working for me :(
User avatar
CometJack
 
Posts: 74
Joined: Thu Jul 15, 2010 10:23 am
Online: 1d 5h 33m 25s
Karma: 0

Re: Tutorial: Page Fade In

Postby zztechjoezz » Fri Jul 30, 2010 3:04 pm

CometJack wrote:Sorry but this isn't working for me :(


  1. $('#container').fadeIn(3000);


Change where it says container to the id of what you want to fade in.

example
  1.  
  2. <html>
  3. <head>
  4. <script type="text/javascript">
  5. $(document).ready(function(){  
  6.      $('#mydivid').fadeIn(3000);
  7.  });
  8. </script>
  9. </head>
  10.  
  11. <body>
  12. <div id="mydivid" style="display:none; text-align:center;">
  13. The content in this div will fade, because it has the id of "mydivid."
  14. </div>
  15. <div id="otherdiv">
  16. Content in this div will not fade, because it doesn't have the div tag "mydivid."
  17. </div>
  18. </body>
  19. </html>
  20.  
Find me on Social Networks:
I am 'zztechjoezz' everywhere!
User avatar
zztechjoezz
 
Posts: 54
Joined: Tue Jun 15, 2010 3:43 pm
Location: Leeds, UK.
Online: 11h 22m
Karma: 1

Re: Tutorial: Page Fade In

Postby Tino » Fri Jul 30, 2010 10:52 pm

And, of course, include jQuery :roll:

I don't consider using frameworks/libraries 'cheating'. Frameworks allow you to perform certain tasks with less work than others, making web development more efficient. That's not to see the core languages shouldn't be learnt, especially not since they are generally required to fully understand the framework. Knowing at least the fundamentals of a language is pretty much required in order to fully understand a framework.

Personally, I'm very fond of the jQuery library, mostly because it makes working with AJAX so incredibly easy. Yes, you can call it cheating, but I'd prefer to call the improvement of libraries like jQuery and MooTools a step forward, not a step backward.
The below statement is false.
The above statement is true.

CodeCanyon
There are some pretty cool items over here, you should check them out.

There are 10 kinds of people, those who understand binary, and those who do not.
User avatar
Tino
Moderator
 
Posts: 1060
Joined: Wed Apr 22, 2009 1:28 pm
Location: The Netherlands
Online: 5d 13h 42m 14s
Karma: 10

Next

Return to Code

Who's online?

Users browsing this forum: No registered users and 2 guests