Move the li element to the left using jQuery slide effect

Currently, I am working on a slide viewer project. The ul contains 5 lis that are being displayed. I am looking to utilize jQuery to create a sliding effect where the li on the left is no longer visible and one of the hidden ones becomes shown. Can anyone guide me on how to achieve this?

Answer №1

updated on 30th June 2011

When I first answered this question, I was relatively new to jQuery. However, upon receiving an upvote for my answer recently, I revisited it and noticed room for improvement. I realized that the entry of the next element happened too quickly, disrupting the block layout (view here). Therefore, I believe utilizing a callback function after the initial toggle is the proper way to address this issue.

Updated jQuery Code

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

    $("button").click(function() {
        $('li:first').insertAfter('li:last').toggle('clip', 100, function() {
            // called after the first .toggle() is finished
            $('li:eq(4)').toggle('scale', 100);
        });
});

View the updated live example here.

.toggle()

.toggle( [duration,] [easing,] [callback] )
Duration: A string or number indicating how long the animation will run.
Easing: A string specifying which easing function to use for the transition.
Callback: A function to execute once the animation is complete.

Original Approach:

JQUERY

    $('li:gt(4)').css('display', 'none');
    $("button").click(function() {  
    $('li:first').fadeOut('fast').insertAfter('li:last')
    $('li:eq(4)').fadeIn(); });

HTML

<ul>
    <li class="slider"> Item-1 </li>
    <li class="slider"> Item-2 </li>
    <li class="slider"> Item-3 </li>
    <li class="slider"> Item-4 </li>
    <li class="slider"> Item-5 </li>
    <li class="slider"> Item-6 </li>
    <li class="slider"> Item-7 </li>
    <li class="slider"> Item-8 </li>
    <li class="slider"> Item-9 </li>
    <li class="slider"> Item-10 </li>
</ul>


<button> Next > </button>

For a clearer and more concise solution, consider using the updated code provided above. It offers improved readability and flexibility.

The updated functionality selects any list item greater than index 4 (zero-based) and hides it. When the button is clicked, the first item eases out and moves to the end, while the fourth item eases in. The animations can be customized further by utilizing jQuery UI effects with toggle. This revised method proves to be efficient and easy to understand.

EDIT: Upon further enhancements, I decided to create a separate question and share the improved answer along with a fiddle demonstration.

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

$("button").click(function() {  
    $('li:first').insertAfter('li:last').toggle('clip',100);
    $('li:eq(4)').toggle('scale', 100);
});

Explore the updated fiddle link here: http://jsfiddle.net/67Wu7/

Answer №2

Markup

<ul>
    <li>A</li>
    <li>B</li>
    <li>C</li>
    <li>D</li>
    <li>E</li>
    <li>F</li>
</ul>
<br/>
<button> click here </button>

Styling

li {
    float: left;   
    display: none; 
}

Interactive Script

$("li").filter(function(index) {
    return index < 4;
}).css("display", "inline");

$("button").click(function() {
    var listItems = $("li");
    
    var firstItem = listItems.first().detach().appendTo("ul");
    
    $("li").filter(function(index) {
        return index < 4;
    }).css("display", "inline");
    
    $("li").filter(function(index) {
        return index >= 4;
    }).css("display", "none");

});

View the Live Demo Here

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Is there a way to add a fade-in and slide-in effect to this dropdown JavaScript, as well as a fade-out and

Although I lack the necessary knowledge of Javascript, I am aware that my request may be a bit much. The code I currently have is directly from the w3school dropdown-list demo. Would it be possible for you to help me implement a fade in and slide in effect ...

Vue components not responding to TailwindCSS breakpoints as expected

After successfully integrating Tailwind into my Vue project and ironing out the initial kinks, I am now facing a roadblock. Despite having code completion in VS Code, I am unable to utilize breakpoints within my project. <div class="container mx-auto"& ...

I'm encountering a situation where the displayName is coming up as null during authentication triggers

Why am I receiving null when trying to use user data in the authentication trigger for my code? exports.sendWelcomeMessage = functions.auth.user().onCreate((user) => { const name = user.displayName; console.log(name); return null; }) ...

Revamping jQuery for a React component

As I transition away from using jQuery for quick hides, shows, and CSS changes in favor of React components that require re-rendering without triggering the jQuery actions requiring a page refresh, I find myself needing to set state within each component. ...

Displaying an image extracted from a database as a blob data type on a fresh PHP page

I have created a code that successfully retrieves an image from a database. However, I am facing a challenge with opening the image on a new page when the user clicks on it. I have attempted a few solutions but have been unsuccessful... Below is the code ...

There seems to be an issue with the multiple file upload feature as no data is being transmitted to

I need to implement a multiple file upload feature on my website. On the client side, I currently have the following code: $(document).on( "click", ".save-button", function () { var data = new FormData(); data.append(&apos ...

Retrieve the choices for the second dropdown based on the selection made in the first dropdown

Hello, I need some assistance. I am currently working on implementing 2 dropdown menus. The first one allows me to select the name of a city <select id="select1" class="select" name="city"> <option value="101000000">city1</option> <o ...

What is preventing my hidden field from being filled by a JavaScript function?

I've recently developed a JavaScript function that generates a specific string required for another form on the website I'm currently working on. Initially, I decided to store this generated value in a hidden field and then submit it within an HT ...

Issue with checkbox alignment in Twitter Bootstrap v3.x

Greetings fellow siblings. I am facing an issue with Twitter Bootstrap checkboxes, particularly when viewed in Opera, Chrome, and IE. Here is the problem: I have come across this snippet of code <div class="form-group"> ...

Evening rotation featuring a CSS design reminiscent of f.lux

Would it be feasible to develop a feature akin to f.lux for my website? f.lux emulates a night shift - I am interested in implementing a similar function on my Web-App. For instance, a CSS property that adjusts the colors to a specific temperature. Curr ...

I am looking to extract specific data from a webpage using Webscraping and page_soup.findAll, but I am unsure of the correct method to do

I'm currently working on a web scraping project and I'm trying to extract the keywords from a webpage. I attempted to use page_soup.findAll() for extraction, but I'm unsure of what to include between the parentheses to get the desired inform ...

Stopping CSS animations at a specific point along the path without using timing functions can be achieved by implementing a targeted

Is there a way to pause an animation at the 50% mark for 2 seconds and then resume? P.S. The setInterval method is not recommended! function pauseAnimation() { document.getElementById("0").style.animationPlayState = "paused"; }; var pauseInterval = set ...

Experiencing a challenge with D365/JavaScript integration: Seeking assistance in adding an OnSave event to prevent users from saving a form

I have successfully created a JavaScript OnSave event that scans a grid of a "Sales Quota Distribution" entity on an "Opportunity" entity form, looking for duplicates in the "Resource" field. When a duplicate is found, a warning message is displayed. Every ...

Multiple instances of Google Maps embedded on a single webpage using a consistent design

Is it possible to have multiple Google Maps on a single page? I currently have this code, but I am unsure of how to implement this. What additional steps are needed? I would like the other maps to have the same styles as well. Another issue I am facing ...

The C# function in asp.net core 2.0 is not receiving the Data Parameters values from the .ajax() method

When implementing an ajax() method call in asp.net core 2.0, I am facing an issue where the value passed using the data parameter is not being received by the controller function. Although the C# function gets called, the controller function parameter fai ...

I am having trouble selecting Bootstrap radio buttons on my ASP.NET Core MVC application

My asp.net mvc core web application has the following code to display 2 radio buttons:- <div class="form-group"> <label class="control-label" style="font-weight:bold"> ...

What could be causing some elements to not be generated while utilizing HTTrack or Save All Resources on an HTML webpage?

I'm interested in understanding the code and logic behind an online game called Paper.io. In order to accomplish this, I decided to save the entire webpage along with its resources on my computer and observe how each element reacts individually. My ...

JavaScript: Perform multiplication and addition on two arrays

I am looking for a way to multiply pairs of values in two arrays that have the same length, and then sum up the results based on their indexes. As an example, var arr1 = [2,3,4,5]; var arr2 = [4,3,3,1]; The output would be 34 (4*2+3*3+4*3+5*1). What is ...

Displaying jQuery content tabs with excessive whitespace visible

My project utilizes a jQuery content tab plugin featuring 6 tabs with different content sections (Company, The founders, Team, Accolades, Careers, Philosophy). I am encountering an issue in Chrome where clicking on the Company tab after any other tab res ...

Retrieve the present cursor location in a multiline textbox without the need to select any text

Is there a way to determine the current cursor position in a multi-line textbox without selecting any data within the textbox? ...