tags:

views:

27

answers:

3

Hey guys quick question, I have a function and it works when you click close, it slides up. However I want it to slide down by default when you first go to the page and it is just appearing right now, not sliding down.

$(document).ready(function(){

     $("#related2").slideDown();  

 $(".close2").click(function(event) {  
     event.preventDefault();  
     $("#related2").slideUp();  
 });  
 }); 
+1  A: 

I don't know if this helps much, but I use this to do something similar to what your talking about.

$(document).ready(function()
{
  //shows the all of the elements
  $(".viewSectionBody").show();

  //toggle the body on head click
  $(".viewSectionHead").live('click', function()
  {
    $(this).next(".viewSectionBody").slideToggle(600);
  });
});
Jisaak
+1  A: 

Your code should work as long as you have the style of the #related element set to display:none. That way it can animate the showing of the element when it hits your ready() code.

Mark
completely forgot about that, thanks a lot man appreciate it.
Scarface
at least now it will be enforced in my head lol.
Scarface
+1  A: 

If I understand correctly, the code you posted already does what you want, and you want to understand in detail what's going on, yes?

So:

$(document).ready(function() {
...
}

This means that the enclosed code (inside the brackets) should be executed when the page has finished loading.

$("#related2").slideDown();

This makes the selected element to show up using a slideDown effect. Since this is inside (document).ready, this happens as soon as the page finishes loading, and presumably the element is hidden before that (with CSS).

$(".close2").click(function(event) {  
...     
});

This attaches all elements with the class ".close2" a click event, so whenever these elements are clicked, the enclosed code is executed. Again, since this is inside the (document).ready block, this click event is attached as soon as the page finishes loading.

event.preventDefault();

This makes it so that if the element already has a default event attached it is not executed (e.g., a link - - will already have a click event attached, so that when it is clicked the browser is taken to the url on the href). The behaviour of this method will depend if the element actually has a click event attached or not, in this case. It may well do nothing.

$("#related2").slideUp();

Slides up the selected element.

EDIT: Ok, just noticed your comment on your original post. It's not sliding down by default probably because the element is not hidden by default, which is required since the slideDown method really just "unhides" and element using a fancy effect. So you have to hide #related2 using CSS, or with inline style on the HTML. Which translates to either:

#related2 { display: none; }

on the CSS, or:

<div id="#related2" style="display:none;"></div>

on the HTML (of course, I'm presuming the element is a div).

Fernando Figueiredo
no lol, I am sorry for adding that last part to my question, it confused a lot of people. I thought it was my javascript that was messing up so I was really confused and wanted an explanation to why it did not work so I wasn't confused even if I figured it out on my own. I thought maybe I needed to add another funeryction around slide down to make it work with jquery but it was just a css issue.
Scarface
your comment was still useful to me thought because I was not fully sure what some of the function words did such as event.preventDefault. +1 vote.
Scarface