views:

83

answers:

3

I have worked out how to fade an element in: Click here to view the page

If you click on the heading Posture 1 : Standing Deep Breathing : you will notice the element fades in as it should. If you now click on posture 2 you will see the element fades in below posture 1. I need to be able to swap posture 1 with posture 2.

I have a total of 26 postures that all have images that need to fade in and then be swapped with another image when another heading is clicked.

$(document).ready(function(){   
$('#section_Q_01,#section_Q_02').hide();

$('h5.trigger#Q_01').click(function(){
    $('#section_Q_01').fadeIn(2000) ;
});

$('h5.trigger#Q_02').click(function(){
    $('#section_Q_02').fadeIn(5000) ;
}); 

});

and the html

                   <div id="section_Q_01" class="01">
                        <div class="pics"> 
                            <img src="../images/multi/poses/pose1/Pranayama._01.jpg"/> 
                            <img src="../images/multi/poses/pose1/Pranayama._02.jpg"/> 
                            <img src="../images/multi/poses/pose1/Pranayama._03.jpg"/> 
                        </div> 
                    </div>


                    <div id="section_Q_02" class="02">
                        <div class="pics"> 
                            <img src="../images/multi/poses/pose2/Half_Moon_Pose_04.jpg" /> 
                            <img  src="../images/multi/poses/pose2/Backward_Bending_05.jpg" /> 
                            <img src="../images/multi/poses/pose2/Hands_to_Feet_Pose_06.jpg" /> 
                        </div> 
                    </div>                  

I need to be able to swap a total of 26 elements #section_Q_01 - #section_Q_26 Any help appreciated

+1  A: 

You need to hide the one already running first.

I might do the following:

$(document).ready(function(){   
  $('#section_Q_01,#section_Q_02').hide();

  $('h5.trigger#Q_01').click(function(){
    $('.running').hide().removeClass('running');
    $('#section_Q_01').fadeIn(2000).addClass('running') ;
  });

  $('h5.trigger#Q_02').click(function(){
    $('.running').hide().removeClass('running');
    $('#section_Q_02').fadeIn(5000).addClass('running') ;
  });
});

although in real code I would define a method such as:

function showSection(id, speed)
{
  $('.running').hide().removeClass('running');
  $(id).fadeIn(speed).addClass('running');
}

and use that.

Edit: Looking at the web site, you might also want to have only the visible set of images cycling, but that is an optimization possibly only needed if the site gets sluggish when everything is running.

Kathy Van Stone
Thanks Kathy it worked how it should. I am using the code below because I don't need to write much code for it to work. In saying that your answer has taught me more about jquery as it relates to the code I had.And Nick I think your right having 26 fading images in the background may be a problem, I have started a new topichttp://stackoverflow.com/questions/2749212/jquery-fade-and-swap-an-element-when-clicked-which-will-also-relate-to-an-accordiaddressing this and a few other points. Thanks again!
Nik
+1  A: 

you have to remove the existing image before showing the other. I don't think you have to repeat your code for all 26 images.

try this:

$("#content_main_left_hold_post > h5.trigger").each(function(){
   var tempid = $(this).attr("id") 
   tempid.bind("click", function(){
     $(".pics > img").hide()
     $("#section_" +  tempid).stop(false, true).fadeIn(2000) // selection of the image according to the id of h5, finishing the lest animation and finaly fades in your element
   })
});
meo
Thanks for having a look. As I am a complete novice I couldn't get this code to work for me.
Nik
+1  A: 

DEMO: http://jsbin.com/aceze/24

$(function() {
$('h5.trigger a').click( function(e) {
e.preventDefault();
var trigger_id = $(this).parent().attr('id'); //get id Q_##
$('.current').removeClass('current').hide(); //add a class for easy access & hide
$('#section_' +  trigger_id).addClass('current').fadeIn(5000); //show clicked one
});
});​

not need to use jquery for hide at start use css instead

#section_Q_01, #section_Q_02 { display:none }
aSeptik
Thanks that worked well and saves me a lot of code. I am hiding the elements with jquery so that if the user has it turned off the images will still appear. The way I need this to work is becoming more complicated, so I have started a new thread http://stackoverflow.com/questions/2749212/jquery-fade-and-swap-an-element-when-clicked-which-will-also-relate-to-an-accordiThanks again for you succinct solutionNik
Nik