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 7801103
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T00:50:44+00:00 2026-06-02T00:50:44+00:00

i m building a little script to animate a list. Here is my html

  • 0

i m building a little script to animate a list. Here is my html structure:

<ul>
   <li class="slider"> Item-1 </li>
   <li class="slider"> Item-2 </li>
   <li class="slider"> Item-3 </li>
   ...
   <li class="slider"> Item-13 </li>
   <li class="slider"> Item-14 </li>
   <li class="slider"> Item-15 </li>
</ul>

<button> Next </button>

I’m displaying only four li at one time, the “next” button fadeOut the displayed four li et fadeIn the next four ones. But the fades are applying both together. I’ve tried to use callback function on the first fade but i can’t make it work.

here is the script:

$('li:gt(3)').css('display', 'none');

//Define the interval of li to display
var start = 0;
var end = 4;

//Get the ul length
var listlength = $("li").length;

$("button").click(function() { 

  // FadeOut the four displayed li 
  $('ul li').slice(start,end).fadeOut(500, function(){

        // Define the next interval of four li to show
        start = start+4;
        end = end+4;

        // Test to detect the end of list and reset next interval
        if( start > listlength ){
          start = 0;
          end = 4;
        }

        //Display the new interval
        $('ul li').slice(start,end).fadeIn(500);
  });    
});

Any clues?

  • 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-06-02T00:50:46+00:00Added an answer on June 2, 2026 at 12:50 am

    The problem is that the .fadeOut() callback is called once per animated element, not once at the end. You could modify your code to keep a counter of how many times it has been called, but far easier – assuming at least jQuery 1.6 – is to use .promise(), which will resolve after all the associated animations complete:

    $(document).ready(function () {
        var $lis = $("li.slider"),
            start = 0;
    
        $lis.hide()
            .slice(start, start + 4)
            .show();
    
        $("button").click(function () {
            $lis.slice(start, start + 4)
                .fadeOut(500)
                .promise()
                .done(function () {
                    start += 4;
                    if (start > $lis.length) {
                        start = 0;
                    }
                    $lis.slice(start, start + 4).fadeIn();
                });
        });
    });
    

    Demo: http://jsfiddle.net/w7Yuk

    I made a couple of other changes to your code, e.g., caching the jQuery object with the li elements, and removing the "end" variable.

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

Sidebar

Related Questions

I'm building a little upload script. My form has a target to an iFrame
I'm building a little script that will connect to an IMAP account and grab
I'm building a little script that changes content and css live on the page
I'm building little class called Output ... It's meant to be something that will
I could do with a little guidance if possible.. I'm building a class library
I'm building a little search application (I know I can use match against). But
I'm building a little tool that will download files using wget, reading the urls
I'm currently building a little CMS for a smaller site. Now I want to
I'm building a little Android game using libgdx. For now I have a copy
I'm getting a little tired of building web applications. Feels like same thing over

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.