tags:

views:

41

answers:

3

I'm playing with jQuery, and I want to fade out the logo. So basically, I want to have the animation fade out the logo first and then slowly shrink the header, so I can only see the id="nav" div. I tried using slideToggle, but it seems to just make my header div layer disappear rather than shrinking it to the header collapse.

<div id="header"><div id="nav"></div></div>
#header {
    height: 25px;
    margin:0;
    padding:85px 0 0 5px;
    background-image: url("images/logo.png");
    background-repeat: no-repeat;
}

#header-collapse {
    padding: 5px 0 0 5px;
    height: 25px;
    margin: 0;
}

<script>
$("#viewmode").click(function(){ 
    $("#viewmode").click(function(){ 
$("#header").attr("id", "header-collapse");
});

</script>

Update: I am testing this:

$("#header").fadeTo(1000,0).css("background-image", "none");

But this fades the header div which contains the nav div, and I only want to fade the logo. How can I select the headers background-image?

Update: I used this for a link. How can I make click and go back to the original?

<a href="#" id="viewmode">View Mode</a> <br /> 
<script> 
$("#viewmode").click(function(){ 
    $("#logo").fadeTo(1000,0);
    $("#logo").animate({'height':'0'},'fast');

});
</script>
+3  A: 

try

$("#header").fadeTo(1000,0).attr('id','header-collapse')

Or

$("#header").fadeTo(1000,0,function(){$(this).attr('id','header-collapse');});

fadeTo supports a callback function once the animation is complete.

As someone has already mentioned, changing IDs is not required for animating an element.

I missed out the shrinking of the header part, and the fact that you only wanted the background image (logo) to fade.

I would use two elements, one the #header and another called #logo. The background image would be part of the #logo element and #logo would be absolutely positioned within #header. (use the css z-index property to make it appear behind or front).

Then you could use the callback and animate the header once the fade is complete. No need to use classes for the start and end states.

callback refers to a function that is fired at the end of an animation.

see below:

$("#logo").fadeTo(1000,0,function(){
    $(#header).animate({'paddingTop':'5px'},'fast');
});
Moin Zaman
I'm sorry, I should have mentioned there was a navigation bar within my header div. Could you tell me what the callback function is for?
Doug
-1 This just sets the opacity of the header to 0. It does not address the OP's question, which is about adjusting the appearance of a div.
lonesomeday
@Doug, see my updated answer.
Moin Zaman
I am always trying to conserve div usage, but it seems that is "okay" if I am trying to animate things with jQuery to make the jQuery more efficient. Correct?
Doug
Your updated code didn't work, but I fixed it. `$("#header").animate({'paddingTop':'5px'},'fast');`
Doug
Yo, thanks for the help man! I learned a lot, especially the animate feature.
Doug
@Moin I have a question: I tried your code without a callback and just simply used `$("#logo").fadeTo(1000,0); $("#logo").animate({'height':'0'},'fast');` and it worked the same way. Any thoughts?Second question in post.
Doug
I figured out the second question :). I used toggle instead of click.
Doug
@Doug: the reason it works the same way is because, fadeTo causes queueing, so it will always run to completion before the next statement. It was a used as a delay trick before .delay() was introduced. see: http://www.learningjquery.com/2007/01/effect-delay-trick
Moin Zaman
+1  A: 

Firstly, don't change an element's id. It is pretty much never necessary, can break all kinds of things, and is inefficient. Add or remove classes instead.

You can animate the transition between classes using the jQuery UI function switchClass:

$('#header').switchClass('header-full','header-collapse','slow');

NB

  1. You need to make the CSS refer to a header-collapse class rather than ID
  2. You need to install jQuery UI for this to work.
lonesomeday
Advice taken. And I will look at jQuery UI. Could you explain why it is inefficient?
Doug
I believe (and I haven't examined the jQuery source on this one) that to change an element's ID while maintaining a valid DOM, the original element must be removed and cloned, the ID must be changed and then the element must be re-inserted back into the DOM. Much faster just to change a class!
lonesomeday
-1, including jQuery UI just for switchclass is overkill
Moin Zaman
@Moin If you want to do that, be my guest. For animating a change of class, as the OP requested, it is the best way. @Doug I may be making that up -- I can't find the code in the jQuery source. That said, changing an element's ID is semantically *weird*!
lonesomeday
I know nothing about jQuery, so if you tell me that it is weird and tell me this is how you're supposed to do it then I will follow that practice.
Doug
@Doug Think about it as getting a new driving licence number every time you buy a new car. In the philosophy of DOM, the ID identifies the element; the class describes it.
lonesomeday
Fair point, then should I select its CSS to achieve what I want?
Doug
If you create a `.header-full` rule containing what is currently in `#header` and a `.header-collapse` rule with `#header-collapse`, switchClass would be able to animate the change for you. The first argument is the class to remove; the second argument is the class to add. Hope this answers your question...
lonesomeday
Update: I used this for a link. How can I make click and go back to the original? use fadeTo(0,1000) now but remove the height change and also check to see what state the function is in with a if,else statement... I think you need to learn basic Javascript before playing with jQuery if you really wanna do anything.
Neo
+1  A: 
<style>
#header {
    height: 25px;
    margin:0;
    width:[logo image width in px];
    padding: 5px 0 0 5px; /* no need to put space for logo now in padding!*/
    background-image: url("images/logo.png");
    background-repeat: no-repeat;
}
</style>

<script>
$('#viewmode').click(function() {

  $('#header').fadeOut('slow', function() { 
  /*you can still use fadeTo(1000,0,function(){*/

    // Animation complete.
  });

});
</script>

make it look the way it does with the following: you didn't give the nav css so I can't do it for you:

<div class='header_wrapper'>
   <div id="header"></div>
   <div id="nav"></div>
</div>
Neo