Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7522619
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T02:34:27+00:00 2026-05-30T02:34:27+00:00

I would like to make a little Slideshow with jquery. The first Problem: If

  • 0

I would like to make a little Slideshow with jquery.
The first Problem: If I only have the “next click function”, the images show up properly one after an other. But if I use the “prev” function, the next button wont work anymore.
The other Problem: the slideshow should start again by the first image by clicking the “next button” after the last image.
Maybe someone has an idea. thanks in advance.

         $("#next").click(function(){
            if($("#gallery li").next("#gallery li").length != 0) {
                var next = $(".toppicture").parent("li").next().children("img");
                $("#gallery li").children("img").removeClass('toppicture');
                $(next).addClass('toppicture');
            }
            else {
                $(this).children("img").removeClass('toppicture');
                $("#gallery li:first-child").children("img").addClass('toppicture');
            }
        });

        $("#prev").click(function(){
            if($("#gallery li").prev("#gallery li").length != 0) {
                var prev = $(".toppicture").parent("li").prev().children("img");
                $("#gallery li").children("img").removeClass('toppicture');
                $(prev).addClass('toppicture');
            }
            else {
                $(this).children("img").removeClass('toppicture');
                $("#gallery li:first-child").children("img").addClass('toppicture');
            }
        });

the “ul” with the id “gallery” looks like this:

    <li>
<img class="toppicture" src="images/w1.jpg" title="Title #0"/>
    </li>
    <li>
<img  src="images/w2.jpg" title="Title #1"/>
    </li>
    <li>
<img  src="images/w3.jpg" title="Title #2"/>
    </li>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T02:34:29+00:00Added an answer on May 30, 2026 at 2:34 am

    This isn’t the simplest slider, but I think it is the easiest to understand. jsFiddle

    HTML:

    <ul id="gallery">
        <li> <img src="http://lorempixel.com/output/people-q-c-640-480-5.jpg" title="Title #0"/> </li>
        <li> <img src="http://lorempixel.com/output/abstract-q-c-640-480-8.jpg" title="Title #1"/> </li>
        <li> <img src="http://lorempixel.com/output/food-q-c-640-480-3.jpg" title="Title #2"/> </li>
    </ul>
    <a href="#" id="previous">previous</a> <a href="#" id="next">next</a> 
    

    JS:

    function loadSlide(index){
        $('#gallery li').hide()
            .eq(index).show();
    }
    
    $('#gallery').data('index',0).find('li').hide();
    loadSlide(0);
    
    $('#next').on('click',function(e){
        var index = $('#gallery').data('index'),
        numSlides = $('#gallery li').length;
    
        index = (index + 1) % numSlides;    
        loadSlide(index);
        $('#gallery').data('index',index);
        e.preventDefault();            
    });
    $('#previous').on('click',function(e){
        var index = $('#gallery').data('index'),
        numSlides = $('#gallery li').length;
    
        index = (index + numSlides - 1) % numSlides;
        loadSlide(index);
        $('#gallery').data('index',index);
        e.preventDefault();
    });
    

    It avoids a lot of traversal (like your .next() fiasco) by separating the roles.

    • The slide changing is done by a function, it takes a number and shows that slide.

    • The current slide number is stored on the gallery itself using .data()

    • The next and previous buttons just have to get the current slide and either increment of decrement that number appropriately, then call the slide changer.

    This way you can easily expand on any particular area. Want a transition? Put it in the slide-changer. Want the slides to auto-advance? Set a timer to call the slide changer.

    The only thing that separates this solution from a full featured slider is that usually you would encapsulate everything together, and use custom events rather than a function to change the slide. I’d be happy to extend this example a bit to show you the difference if you’d like.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to make vim my C++ editor. I have very little experience
I would like to make a system of page on android 3.0 a little
I have two almost identically classes written in js. I would like to make
I have an object with which I would like to make follow a bezier
I would like to make my code a little better. This is my array:
I would like to make a wiki syntax parser in Java. I have one
I have a single-threaded linux app which I would like to make parallel. It
I would like make an extension method for the generic class A which takes
Would like to make anapplication in Java that will not automatically parse parameters used
I would like to make a nightly cron job that fetches my stackoverflow page

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.