views:

32

answers:

2

Hi,

I am making a website, and it allows users to change view options. I use jQuery to smooth animations for font changing. It fades the whole page out and back in again with the new fonts.

The fade out animation is fine, but when it fades back in, there's no fade. It just pops up, no animation.

The problematic jQuery is in http://xsznix.my3gb.com/options.php.

The code I have so far is this:

$('#font-classic').click(function(){
    $(document.body).animate({opacity: '0%'},{duration: 1000, complete: function(){
        // font changing code here
        $(document.body).animate({opacity: '100%'}, 1000);
    }});
});

Thanks,

xsznix

+2  A: 

Why not use jQuery's built-in functions fadeIn and fadeOut?

$('#font-classic').click(function(){
    $('body').fadeOut('normal', function(){
        $('body').fadeIn();
    }});
});
Jacob Relkin
I guess I could, but I really don't want to redo my brackets again... because I am just that lazy.
xsznix
@xsznix, it's much cleaner as well. `animate` is meant for **custom** animations.
Jacob Relkin
I guess you're right, I'll do that.
xsznix
+1  A: 

jQuery's .animate() takes values from 0 to 1.

$(document.body).animate({opacity: 0}, 1000);
$(document.body).animate({opacity: 1}, 1000);

I'm sure that .animate() must call .parseFloat() (or something) on the values you're passing, which would make your 0% into 0 (which is correct), but your 100% into 100, which would be incorrect.

patrick dw
it worked! yay!
xsznix
@xsznix - FYI `.animate()` is "meant" for whatever animation you want. There's no rule about custom vs non-custom (whatever that means). When you call `.fadeOut()` it is *directly* calling `.animate()`, so you're just a step abstracted from it. Use whichever you prefer, and don't be influenced by people who make up arbitrary rules. :o)
patrick dw
I get that... just .animate() uses that many extra bytes. :P
xsznix
.fadeIn()/fadeOut() is also "cleaner" and therefore that much easier to maintain.
xsznix
@xsznix - True, you save a few bytes and spend a few processor cycles. :o) Using `fadeOut()` is nice for simple cases, but can cause issues in a situation where you need to stop and reverse the effect. (Common for `mouseenter/mouseleave` events.) It tends to lose its place, and you end up being stuck semi-transparent. Something to keep in mind if that ever happens. :o)
patrick dw
$('#item').stop().fadeIn(); $('item').stop().fadeOut();Simple as that.How do you do inline code like that?
xsznix
@xsznix - Here's an example of what I meant. http://jsfiddle.net/Abjq9/ Try to hover rapidly over and off the blue text. You'll see that the opacity gets stuck in the middle. Using `.animate()` solves this. You can change the code, and click **Run** at the top to test your changes. Here's the same example, but using `.animate()`.
patrick dw
...and the inline code formatting is done by using the key above your `left tab key` at the `beginning` and `end` of the `code` you want to format. :o) (I don't know what that character is called.)
patrick dw
Oops... forgot to paste the `.animate()` version above. http://jsfiddle.net/Abjq9/1/
patrick dw
OK, but my website uses `fadeIn()` and `fadeOut()` so that they are always together and never broken up. Thanks for the help. http://en.wikipedia.org/wiki/%60
xsznix