tags:

views:

5079

answers:

4

i have the following jquery code.

basically i will have several overlapped divs and a list of links on the right of all those overlapped divs. when hovering over a link, the link's assigned div will fade in.

I have the following code and it works (it uses the default windows' sample pictures), but if someone can think of a way to optimize it or make it generic, i'd appreciate it.

<html>

<script type="text/javascript">
   $(document).ready(function() {

      $("#trigger1").mouseover(function() {

  $(".contentdiv").addClass("inactive");
  $("#divsunset").removeClass("inactive");
  $(".inactive").fadeOut(500);
        $("#divsunset").fadeIn(500);
      });


      $("#trigger2").mouseover(function() {

  $(".contentdiv").addClass("inactive");
  $("#divwinter").removeClass("inactive");
  $(".inactive").fadeOut(500);
        $("#divwinter").fadeIn(500);

      });

      $("#trigger3").mouseover(function() {

      $(".contentdiv").addClass("inactive");
      $("#divbh").removeClass("inactive");
      $(".inactive").fadeOut(500);
            $("#divbh").fadeIn(500);

      });

      $("#trigger4").mouseover(function() {

      $(".contentdiv").addClass("inactive");
      $("#divwl").removeClass("inactive");
      $(".inactive").fadeOut(500);
            $("#divwl").fadeIn(500);

      });

    });

</script>


<style>
 #divsunset
 {
  position: absolute;
  top: 5px;
  left: 5px;
 }
 #divwinter
   {
    position: absolute;
    top: 5px;
    left: 5px;
 }
 #divbh
     {
      position: absolute;
      top: 5px;
      left: 5px;
 }
 #divwl
     {
      position: absolute;
      top: 5px;
      left: 5px;
 }
 #links
     {
      position: absolute;
      top: 800px;
      left: 700px;
 }
 .inactive
 {
 }
</style>


Show Sunset Show Winter Show Blue Hills Show Waterlillies

+4  A: 

The first thing you can do, to make it cleaner is replace all those similar calls with something more generic.

(note: assume all of this is inside of document.ready())

$('#trigger1').data('target', '#divsunset');
$('#trigger2').data('target', '#divwinter');
$('#trigger3').data('target', '#divbh');
$('#trigger4').data('target', '#divwl');
$('#trigger1,#trigger2,#trigger3,#trigger4').mouseover(function() {
    var selector = $(this).data('target');
    $(".contentdiv").addClass("inactive");
    $(selector).removeClass("inactive");
    $(".inactive").fadeOut(500);
    $(selector).fadeIn(500);
});

Using comma-delimited selectors is a great way to obey DRY with jQuery.

I use $(element).data() to set and retrieve a some data on the element, in this case, the selector used to update the appropriate element.

Also, just for a cleaner visual look, you can use the following in the place of $(document).ready(), if you prefer it. It's the exact same thing, just a different syntax.

$(function() {
   //DOM ready
};
TM
A: 

It's good, but you'll need a more generic approach to the id's you've assigned.

The quickest solution that comes to mind is to wrap up the overlapped divs that fade in under a parent div "#wrapped". Take all the links and assign them CSS classes 'trigger NAME' where 'NAME' is 'Sunset, 'Winter', etc. Then you can do something like:


   $(document).ready(function() {

      $(".trigger").mouseover(function() {

                $(".contentdiv").addClass("inactive");
                $("#wrapper ." + $(this).attr('class')[1]).removeClass("inactive");
                $(".inactive").fadeOut(500);
        $("#wrapper ." + $(this).attr('class')[1]).fadeIn(500);
      });

It's not the best possible solution, but hope it gives you an idea.

kRON
+1  A: 

I replied to a similar question some months ago jQuery Swapping Elements if that helps.

Matt

Clarrification, where i've got {id:'1'} you should swap in the id of the div you want to show and make a generic class name on the other DIV's to hide them.

Remeber that you can apply mulitple classes to an element:

<Div class="name1 name2"></div>

which might help if you have style rules attached to the original divs.

Matt Goddard
A: 

Thank you Matt, TM and kRON, your responses really helped.

I dont feel I explained myself totally, but TM provided the answer I was looking for.

I wanted to follow DRY and the code TM provided helped me best this time since it did not require for me to alter my markup, just the jQuery code.

Again, thanks a lot. Its amazing how quickly I got the answer. Keep up the great work.

silverCORE