Transition the object in smoothly after the slide has changed

How can I make the h4 tags fade in after the divs slide into view, while also adding the class "current" to each visible slide? Check out the example on JSFiddle here.

<div class="slider">
    <div class="slides">
        <div class="slide slide-1">
            <h4>Slide 1</h4>
        </div>  
        <div class="slide slide-2">
            <h4>slide 2</h4>
        </div>  
        <div class="slide slide-3">
            <h4>slide 3</h4>
        </div>  
        <div class="slide slide-4">
            <h4>slide 4</h4>
        </div>  
        <div class="slide slide-1">
            <h4>Slide 1</h4>
        </div>
    </div><!-- end homepage slider container -->

CSS:

.slider{
    width: 550px;
    background: #d00d00;
    min-height: 385px;
    overflow: hidden;
}
.slides{
    width: 2750px;
    margin: 0;
}
.slide{
    position:relative;
    display: block;
    float: left;
    width: 550px;
    height:400px;
}
.slide-1{
    background:#dedede;
}
.slide-2{
    background:#999;
}
.slide-3{
    background:#333;
}
.slide-4{
    background:#555;
}
h4{
    background:#bada55;
    padding:15px 120px;
    display: inline-block;
    margin-top:30%;
    font-size:1.4em;
}

jQuery:

$(document).ready(function(){
    var width = 550;
    var speed = 1000;
    var pause = 3000;
    var currentSlide = 1;
    var $slider = $(".slider");
    var $slides = $slider.find('.slides');
    var $slide = $slider.find('.slide');
    
    $slide.first().addClass('current');

    setInterval(function(){

        $slides.animate({'margin-left': '-='+ width}, speed, function(){

            currentSlide++;
            if (currentSlide === $slide.length) {
                currentSlide = 1;
                $slides.css('margin-left', 0);
            }

        });

    }, pause);

});

Answer №1

To see the demonstration, click on this link. I made changes to the callback parameter of the animate() function.

$slides.animate({'margin-left': '-='+ width}, speed, function(){

            currentSlide++;
            $slide.removeClass("current");
            if (currentSlide === $slide.length) {
                currentSlide = 1;
                $slides.css('margin-left', 0);

            }
            $slides.find("h4").hide();
            $slide.eq(currentSlide-1).addClass("current");
            $slide.eq(currentSlide-1).find("h4").fadeIn();
        });

Answer №2

Update your CSS first to ensure the h4 element inside the slide div is hidden

.slide h4 {
    display:none;
}       

Next, make necessary adjustments to your jQuery code snippet

$(document).ready(function(){
    var width = 550;
    var speed = 1000;
    var pause = 3000;
    var currentSlide = 1;
    var $slider = $(".slider");
    var $slides = $slider.find('.slides');
    var $slide = $slider.find('.slide');
    $slide.first().addClass('current');

    // Fade in the h4 of the first slide 
    $('.current h4').fadeIn();


    setInterval(function(){

        $slides.animate({'margin-left': '-='+ width}, speed, function(){

            currentSlide++;

            if (currentSlide === $slide.length) {
                currentSlide = 1;
                $slides.css('margin-left', 0);

            }
            // Remove 'current' class from previously added element 
            $('.slide').removeClass('current');
            // Add 'current' class by selecting the correct element based on the slide number
            $('.slide-'+currentSlide).addClass('current');

            // Hide the previously faded-in h4 
            $('.slide h4').hide();
            // Fade in the h4 of the current slide 
            $('.current h4').fadeIn();
        });

    }, pause);
});

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

Avoiding repetition in json array using reactjs with the help of axios

After receiving guidance from @Akrion, I managed to resolve the issue! Check out my comments below our conversation for the solution. I am relatively new to reactJS and axios, but I recently collaborated with a classmate on a project. Now, I find myself s ...

What is the best way to customize the behavior of multiple instances of ng-include within the same view?

My goal is to have unique behavior for each instance of the calendar that I include multiple times in my main HTML view. Specifically, I want to display 3 different dates based on the day selected in each calendar. In my main HTML view, I have included th ...

Retrieving the 'red' pixel data from the texture rendered with gl.texImage2D

My objective is to transfer a Float32array to my fragment shader using a texture in order to process the data within the shader and then send it back to JavaScript. Since the data is not in the form of an image, I opted to transmit it as 'gl.R32F&apos ...

Assigning a value to a variable from a method in Vue: a step-by-step guide

I'm having trouble assigning values from a method to variables in HTML. Here's what I have in my code: <b-card-text>X position {{xpos}}</b-card-text> <b-card-text>Y position {{ypos}}</b-card-text> I would like to assign v ...

React: Render function did not return any content. This typically indicates that a return statement is missing

Can anyone help me troubleshoot this error? Error: CountryList(...): No render was found. This typically indicates a missing return statement. Alternatively, to display nothing, return null index.js import React from 'react'; import ReactDOM f ...

Achieving Perfect Alignment in Dreamweaver: Crafting a Stylish Header Container

Hello, I am currently working on a Dreamweaver site and have created a CSS style sheet within my template that includes a div tag called #HeaderBox. My goal is to make this box 40% of the screen's size, with specific pixel dimensions if necessary. I a ...

Office Outlook Client experiencing incorrect div width

I'm having trouble sending an email with HTML content because it's not displaying correctly in Microsoft Office Outlook when it comes to width. Any suggestions on how to fix this issue? <div style="width: 650px; border: 1px solid blue">hel ...

Some of the CSS code in my file is not displaying properly on my ejs file

For some reason, only the CSS styles for my footer are rendering on my ejs file. The rest of my CSS doesn't render at all. I've tried defining the path of my static files to my views directory using the path method: app.use(express.static(path.jo ...

Guide to inserting a custom image into an Icon in next js

For my Next.js 13 and Tailwind project, I am looking to customize a chat/messenger icon by filling it with an image of my choice. While attempting to use the Lucide React Messenger icon, I found that it only allows hex color values for filling, but I want ...

Our system has picked up on the fact that your website is failing to verify reCAPTCHA solutions using Google reCAPTCHA V2

Error Message We have noticed that your website is not validating reCAPTCHA solutions. This validation is necessary for the correct functioning of reCAPTCHA on your site. Please refer to our developer site for further information. I have implemented the re ...

Having trouble with selecting JS dropdown options in Python Selenium

My HTML view- Check out the website design HTML code- View the HTML code here When I click on the dropdown arrow, a new code appears - view the code snippet Although my code successfully displays the dropdown options, it does not allow for selecting an ...

Sending the HTML input value to a Knockout view

Can someone assist me with a dilemma I'm facing? Within CRM on Demand, I have a view that needs to extract values from CRM input fields to conduct a search against CRM via web service. If duplicate records are found, the view should display them. Be ...

Is the AJAX call returning JSON data?

Currently, I am utilizing AJAX technology to scrape player names and their respective ratings from a website. The approach I have chosen involves having AJAX return a JSON object which is then manipulated using jQuery to append each element of the JSON obj ...

Tips for utilizing a function within a callback function using jQuery

When using jQuery's .load() to load HTML files into a parent webpage, I am interested in executing jQuery/JS from the parent page against the loaded HTML file. It seems like this can be achieved with a callback function. The jQuery I'm using is ...

Passing data in object as key value after serializing using jQuery AJAX sortable is a common and

Using jquery ui sortable to rearrange a list with php, I extract the ids like this: var ids = $('#sort1').sortable('serialize'); It functions properly when including the ids in the ajax call data like so: data: ids To retrieve it in ...

Troubleshooting varying <li> heights in Google Chrome with CSS

Kindly review the image enclosed below ul li{ width:100px; height:200px; float:left; border:10px double #333; -webkit-border-radius: 10px; -moz-border-radius: 10px; } <ul> <li><a href="image.jpg"><img src ...

Exploring the Power of Nuxt's asyncData Feature for Handling Multiple Requests

Within my application, there is a seller page showcasing products listed by the specific seller. Utilizing asyncData to retrieve all necessary data for this page has been beneficial in terms of SEO. asyncData ({params, app, error }) { return app.$axi ...

Ways to update all URLs on a page with ajax functionality

My userscript is designed to modify the href of specific links on an IP-direct Google search page: // ==UserScript== // @name _Modify select Google search links // @include http://YOUR_SERVER.COM/YOUR_PATH/* // @include http://62.0.54.118/* // ==/Us ...

Using jQuery to apply CSS with relative percentage values (+=/-=/)

Today, I experimented with using relative values in jQuery's .css() function. $block.css({'margin-left': '-=100%'}); This resulted in the following style for the div: <div class="block" style="margin-left: -100px;"> I not ...

The error callback in Jquery ajax is triggered despite receiving a 200 status code in the response

I am facing an issue with my ajax code. The success callback contains an alert that is not working properly. Here is the code snippet: $(".change_forex_transaction_status").click(function(){ $("#insufficient_funds").css("display","none"); var id = $( ...