Strange behavior observed in fading slides using jQuery

I have been experimenting with jQuery to create a slide feature. However, I am encountering an issue where the next slide fades in before the previous one completely fades out when I click the next arrow. Does anyone know how to fix this problem?

<html>
    <head>
        <style>
            #mainWrapper{
                padding:10px 0 10px 0;
                width:960px;
                border:1px solid grey;
                margin:auto;
            }
            #jumbotron{
                height:400px;
                width:500px;
                margin:auto;
            }
            .slide{
                width:400px;
                margin:auto;
                margin-top:100px;
                display:none;
            }
            .active-slide{
                display:block;
            }
            .dots{
                list-style-type:none;
                margin:0;
                padding:0;
            }
            .dot{
                float:left;
                margin:0 10px 0 10px;
                color:rgb(214,214,214);
            }
            .active-dot{
                color:black;
            }
            .arrow-prev,.arrow-next,.dots{
                float:left;
                margin:0 20px 0 20px;
            }
            .slider-nav{
            margin-top:10px;
                position:relative;
                left:125px;
            }
            .large{
                height:200px;
                width:400px;
            }
        </style>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
    <head>
    <body>
        <div id ="mainWrapper" >
            <div id="jumbotron">
            <div class="">
                <div class="slide active-slide">
                    <img src="desert.jpg" alt="desert" class="large"/>
                </div>
                <div class="slide">
                    <img src="tulips.jpg" alt="tulips" class="large"/>
                </div>
                <div class="slide">
                    <img src="penguins.jpg" alt="penguins" class="large"/>
                </div>
                </div>
                <div class="slider-nav">
                    <a href="#" class="arrow-prev">
                        <img src="http://s3.amazonaws.com/codecademy-content/courses/ltp2/img/flipboard/arrow-prev.png">
                    </a>

                    <ul class="dots">
                        <li class="dot active-dot">&bull;</li>
                        <li class="dot">&bull;</li>
                        <li class="dot">&bull;</li>
                    </ul>

                    <a href="#" class="arrow-next">
                        <img src="http://s3.amazonaws.com/codecademy-content/courses/ltp2/img/flipboard/arrow-next.png">
                    </a>
                </div>
            </div>
        </div>

        <script>
            $(document).ready(function(){
                $('.arrow-next').click(function(){
                    var currentSlide = $('.active-slide');
                    var nextSlide = currentSlide.next();

                    if (nextSlide.length === 0) {
                        nextSlide = $('div.slide').first();
                    }
                    currentSlide.fadeOut(600).removeClass('active-slide');
                    nextSlide.fadeIn(600).addClass('active-slide');

                })
            })
        </script>
    </body>
</html>

Answer №1

One reason for the issue is that you are triggering both fadeOut and fadeIn simultaneously. It is recommended to initiate fadeIn only after fadeOut has completed its process.

currentSlide.fadeOut(600, function(){
    nextSlide.fadeIn(600);
});

Answer №2

The main idea is to:

currentSlide.fadeOut(600).removeClass('active-slide');
nextSlide.fadeIn(600).addClass('active-slide');

To ensure the second animation only runs after the first one is completed, you need to set it as a callback of the first animation. Modify it to:

currentSlide.fadeOut(600).removeClass('active-slide', function(){
    nextSlide.fadeIn(600).addClass('active-slide')
});

By making this adjustment, the functionality should meet your expectations.

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 combine the input tag's name with a growing integer variable?

Within a while loop, I need to modify the names of input tags by concatenating them with an increasing integer variable. However, attempts to use the $ symbol have not yielded successful results. <html> <head> <meta http-equiv="Content- ...

<ul> hover effect and text </ul>

Looking for a solution to activate hover state on both the box and text when rolling over tiled images with text underneath. Check out this example in the Fiddle: http://jsfiddle.net/techydude/GF8tS/ Any suggestions on how I can achieve this effect? ...

What is the process of adding a phone number with an extension on an iPhone web application?

When building a web application, inserting the following number: <a href="tel:888-123-4567">call now</a> The iPhone will automatically convert that link into an instant call function when clicked. But what should I do if the number includes ...

Can I Intercept POST Array with JQuery before Sending it?

Can I capture the POST Array with JQuery before sending the form? I'm aware of using preventDefault() to access all variables, but that's not my desired approach. I need to retrieve the POST Array prior to sending it via AJAX to a php file. ...

Building an AJAX form with error validation intact using simple_form

Seeking assistance from the experienced rails community, I present a challenging question that has left me stuck for days. In an effort to learn more about rendering, I am working on a dummy app based on this ajax/jquery tutorial. Following the guide, I s ...

Creating dynamic and interactive web pages can be achieved by utilizing either $_POST or $_GET with Modal,

In the snippet below, you'll find the HTML code that pulls an array of 6 objects from a database and displays them in a Bootstrap row successfully. <div class="row products"> <?php while($product = mysqli_fetch_assoc($featured)) ...

Deleting the HTML element

Can someone assist me with my script issue? I am facing a problem: when I open my file on fullscreen (over 768px), and move my pointer around the logo div, nothing happens. However, if I resize my browser to below 768px, then back again above 768px, the ...

What is the process for incorporating linear-gradient coloring into the background of a Material UI Chip component?

Is it possible to incorporate a linear-gradient below color as a background for Material UI Chip? linear-gradient(to right bottom, #430089, #82ffa1) The version of Material UI I am working with is v0.18.7. <Chip backgroundColor={indigo400} style={{widt ...

What is the best way to assign a default value to a dropdown list within a jTemplate?

I am currently utilizing jTemplates to dynamically generate form elements based on data received from the server. While I have been successful in setting the values of input fields, I have encountered difficulty in selecting a value for a dropdown list. A ...

Scrolling using JavaScript's 'smooth scrolling' feature is currently disabled

Background of the Issue: I am in the process of creating a one-page website using Twitter Bootstrap 3 within an ASP.NET MVC project. The Challenge: My current challenge involves implementing 'smooth scrolling' functionality that scrolls to the ...

Is there a way for the remaining divs in a column to match the width of the widest div?

I'm attempting to ensure that the divs in a column take the width of the widest element, with the exception of the top div which should span 100% of the screen. How can this particular layout be achieved? Please refer to the image below for a clear ex ...

A complete guide on utilizing *ngFor in Angular to display data rows

I am facing an issue with using *ngFor to create new "rows" for adding new categories dynamically. Although there are no errors displayed when I run the program, the intended functionality is not achieved. I have tried making some modifications but it see ...

unable to display the responseJson findings

I'm having trouble understanding why this function for an API on National Parks isn't working as expected. As a relatively new programmer, I find that seeking help from others can often shed light on issues I may have missed. Any assistance woul ...

Positioning elements to the right inside a Bootstrap 4 thumbnail behaves in a unique way when compared to Bootstrap

<div class='img-thumbnail'> <img src="a.jpg" class='img-fluid'> </div> This particular code snippet is from an HTML file using Bootstrap 4 CSS. It displays an image inside a styled box. Adding an h4 heading places ...

Issue encountered while using AJAX to load images

Currently, I am utilizing a jQuery plugin to fetch images from Flickr. My aim is to organize these images into 3 columns dynamically as they are loaded, ensuring that each image is placed in the shortest column to achieve close to equal lengths for all col ...

Angular allows for the dynamic updating of og meta tags

Sharing dynamic content on WhatsApp has been made possible through an API. By utilizing the angular Meta class in my component.ts file, I am able to dynamically update the default meta tag property in index.html with the latest content. ...

What is the simplest way to implement AJAX jQuery form validation for this specific HTML form?

Being new to AJAX and JQuery, I have successfully designed this form using bootstrap. Now, my goal is to validate each input field before sending the data to the database. This validation process will include checking if fields are empty, contain valid ema ...

Use jQuery to set different background images for multiple divs at random time intervals

I am facing a challenge with displaying multiple divs next to each other on my website, each with a unique background image. I have written a script that randomly changes the background images at different time intervals, but currently all the images chang ...

Struggling to ensure buttons on my page respond and resize properly alongside the other elements

Despite making most of this page responsive, I'm having trouble getting the two buttons to scale appropriately. They are utilizing javascript for a mouseover effect, and I suspect that might be causing some issues. Ideally, I would like the images to ...

How can we develop play buttons for Facebook timelines similar to those found on Spotify?

Is it possible to create an HTML5 player that can be embedded on Facebook timelines, similar to the way Spotify has special privileges as a Facebook partner? I'm looking to provide a list of play buttons that, when clicked, will play songs sequentiall ...