Finding next hidden div and showing it using jQuery selector

I am creating a slideshow in jQuery that allows the user to see four images and navigate back and forth through them by adding a new div with the image at the bottom via the .load and then hiding the top div. I am very new to programming.

I am having a problem working out a selector to allow the user to "go back" by showing the next hidden div after the first div shown and hiding the last displayed fake code example below.

<div class="slideShow" >image one (display = none)</div>
<div class="slideShow" >image two (display = none)</div>
<div class="slideShow" >image three </div>
<div class="slideShow" >image four </div>
<div class="slideShow" >image five </div>
<div class="slideShow">image six </div>

<a href="#" class="scrollUp" >Scrollup</a>
<a href="#" class="scrollDown" >ScrollDown</a>

      

JQuery to load a new image and attach to the bottom of the screen and hide the first displayed div.

$('.scrollDown').click(function() {
$('.slideShow:last').after('<div class="slideShow"></div>'); // add a new div to the bottom.
$('.appendMe:last').load('myimagescript.py'); // load in the image to the new div.

// here I need to find a way of selecting in this example the first shown image (image three) and applying a .slideUp(); to it

});

      

JQuery allows the user to go back to the image they previously saw and hide the last div shown at the bottom

$('.scrollUp').click(function() {

// here I need to find a way of selecting in this example the first hidden div (image two) after the first shown div (image three) and applying a slideDown(); to it.

$('.slideShow:last').slideUp(); // hide the last image on the page - trouble is what happens  if they user now clicks scrollDown - how do I reshow this div rather than just loading a new one?


});

      

+2


source to share


4 answers


I don't quite get it right, however this information might help ... you need to align the first visible div and then use .prevAll () and filter to get the hidden sibling



$('div.slideShow:visible:first').prevAll(':hidden:first').slideDown();

      

+6


source


I spent hours on this site today trying to do something very similar to what was posted in this question.

What I have Previous | The next navigation link is done through a series of divs, hide and show.

Although what I got was different from the answer here ... it was the one that I liked the most, where I needed to be.



So thanks.

And in case anyone is interested, here's what I did:

<script language="javascript">

$(function() {
    $("#firstPanel").show();
});

$(function(){
    $(".nextButton").click(function () {
        $(".panel:visible").next(".panel:hidden").show().prev(".panel:visible").hide();
    });
});
$(function(){
    $(".backButton").click(function () {
        $(".panel:visible").prev(".panel:hidden").show().next(".panel:visible").hide();
    });
});
</script>

<style type="text/css">
    .defaultHidden { display: none; }
    .navigation { display: block; width: 700px; text-align: center; }
    #contentWrapper { margin-top: 20px !important; width: 700px; }
    .nextButton { cursor: pointer; }
    .backButton { cursor: pointer; }
</style>

<div class="navigation">
    <span class="backButton"><< Previous</span>&nbsp; | &nbsp;<span class="nextButton">Next >></span></button>
</div>
<div id="contentWrapper">
    <div id="firstPanel" class="panel defaultHidden">
        <img src="images/quiz/Slide1.jpg" width="640" />
    </div>

    <div class="panel defaultHidden">
    <h1>Information Here</h1>       
    <p>Text for the paragraph</p>
    </div>

    <div class="panel defaultHidden">
    <h1>Information Here</h1>       
    <p>Text for the paragraph</p>
    </div>

    <div class="panel" style="display: none;">
     <img src="images/quiz/Slide4.jpg" width="640" />
        </div>

    <div class="panel defaultHidden">
    <h1>Information Here</h1>       
    <p>Text for the paragraph</p>
    </div>

    <div class="panel defaultHidden">
    <img src="images/quiz/Slide6.jpg" width="640" />
    </div>
    Repeat ad naseum...
</div>

      

+2


source


shot in the dark, but ...

by selecting the first div shown and sliding it up

$('.slideShow:visible:first').slideUp();

      

selecting the first hidden div after the first div shown and sliding down ...

$('.slideShow:visible:first').next('.slideShow:hidden').slideDown()

      

psuedo selectors FTW!

+1


source


Something like the following should do the trick

$(function() {
   $(".scrollUp").click(function() {
     //Check if any previous click animations are still running
     if ($("div.slideShow:animated").length > 0) return;
     //Get the first visible div
     var firstVisibleDiv = $("div.slideShow:visible:first");

     //Get the first hidden element before the first available div
     var hiddenDiv = firstVisibleDiv.prev("div.slideShow"); 
     if (hiddenDiv.length === 0) return; //Hit the top so early escape
     $("div.slideShow:visible:last").slideUp();
     hiddenDiv.slideDown();
   });
   $(".scrollDown").click(function() {
     if ($("div.slideShow:animated").length > 0) return;
     var lastVisibleDiv = $("div.slideShow:visible:last");
     if (lastVisibleDiv.next("div.slideShow").length === 0) {
         //No next element load in content (or AJAX it in)
         $("<div>").addClass("slideShow")
                   .css("display", "none")
                   .text("Dummy")
                   .insertAfter(lastVisibleDiv);
     }
     $("div.slideShow:visible:first").slideUp();
     lastVisibleDiv.next().slideDown();
   });
});

      

The only thing this solution does is check if an animated element that was previously invisible is now. This resolves some of the issues related to multiple link clicks that occur before the animation finishes. If you are using AJAX you will need to do something like this (like enable or disable the global variable) or just disable the scroll down link to avoid making multiple requests to the server at the same time ...

0


source







All Articles