Tell me more ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

The function of this code is to show/hide divs by way of fadeIn/fadeOut, starting with an empty div (home) and fading in one of the other divs (work,cms,contact,) on click, then fading out the last div and fading in the next on click, and then fading out any div and fading in 'panel' (an empty div) when you click on home.

<script type='text/javascript'>
 $(function(){
      $('.panel').hide();

      $('.work_button').click(function(){
            $('#cms,#contact').fadeOut(function(){
            $('#work').fadeIn();
            });
      });

      $('.cms_button').click(function(){
            $('#work,#contact').fadeOut(function(){
                  $('#cms').fadeIn();
            });
      });

      $('.contact_button').click(function(){
            $('#cms,#work').fadeOut(function(){
                  $('#contact').fadeIn();
            });
      });

      $('.home_button').click(function(){
            $('.panel:visible').fadeOut();
      });
});
</script>

<div class="menu">
<ul class="menu">
<li class="home_button">home</li>
<li class="work_button">work</li>
<li class="cms_button">cms</a></li>
<li class="contact_button">contact</a></li>
</ul>
</div>

<div class="panel" id="work">
<p>...</p>
</div>

<div class="panel" id="cms">
<p>...</p>
</div>

<div class="panel" id="contact">
<p>...</p>
</div>
share|improve this question

1 Answer

up vote 1 down vote accepted

I have no problem setting this up in html - it makes sense in this case.

<div class="menu">
<ul class="menu">
<li class="home_button">home</li>
<li class="work_button"><a href="#work" data-fadeOut="#cms,#contact">work</a></li>
<li class="cms_button"><a href="#cms" data-fadeOut="#work,#contact">cms</a></li>
<li class="contact_button"><a href="#contact" data-fadeOut="#cms,#work">contact</a></li>
</ul>
</div>

Set a target on each one

$(function() {
    $('li a', '.menu').click(function(e) {
      e.preventDefault();
      var target = $($(this).attr('href'));
      $($(this).data('fadeOut')).fadeOut(function(){ 
        target.fadeIn();
      });
    })
    $('.panel').hide();
    $('.home_button').click(function(){
        $('.panel:visible').fadeOut();
    });
})

You get the nifty coincidental benefit here of this working ok even without javascript since <a href="#id"></a> also happens to be the the syntax for 'scroll-to element'

Also, you should not name functions with an uppercase capital. Because javascript has no native way of determining which functions are meant to be run in a functional style and which are meant to be used as constructors with the new keyword, it is a very well known convention to use upper case names only for functions which are meant to be invoked with the new keyword. Learn more about javascript capitalization conventions here.

Edit: Totally misread your code. I believe my rewrite now reflects what you're trying to do

share|improve this answer
Methinks someone editied my original code, or I put the wrong code in. I'll edit it to what it should be. – Adam Mar 30 '12 at 16:10
@bigsilk Updated – George Mauer Mar 30 '12 at 16:27
Worked like a charm... Thanks. – Adam Mar 30 '12 at 17:02

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.