Continuously fade out existing content using jQuery and seamlessly fade in new content

I am looking to create a continuous fading effect between two pieces of content in the same position. Currently, the fade in and fade out transitions work, but both contents are visible below each other when the document loads.

Basically, the content will continuously switch between #fade1 and #fade2.

HTML:

<section id="text-slider">
    <div class="container">
        <div class="row>
            <div class="col-md-4">
                <p id="fade1">"IT WAS SUCH A PLEASURE WORKING WITH STOKES STREET ON SPECTROSPECTIVE. THEY SURPASSED ALL EXPECATIONS. WE WERE GOBSMACKED, FRANKLY."</p>
                <p id="fade2" style="opactiy:0">test</p>
            </div><!-- col -->
            <div class="col-md-4">
                <p></p>
            </div><!-- col -->
            <div class="col-md-4">
                <p></p>
            </div><!-- col -->
        </div><!-- row -->
    </div><!-- container -->
</section><!-- text slider -->

JS:

    $(document).ready(function () {

      // runs fade code for homepage content
      fadeThem();

}

    // fades content in and out on homepage
        function fadeThem() {
            $("#fade1").fadeOut(3000, function() {
                $("#fade2").fadeIn(2000, fadeThem());
                $("#fade2").fadeOut(2000, fadeThem());
                $("#fade1").fadeIn(2000, fadeThem());
            });
        }

Answer №1

Have you considered placing your code within: $( window ).load(function() {} ;

Perhaps something along these lines:

    $( window ).load(function() {
        // -- Snipped -- //
        $(".right-image-crm").addClass('right-image-up');

        
        $(".left-image-crm").addClass("left-image-up");


        $(".center-image-crm").addClass("center-image-up");

        $(".loader").fadeOut(1000,function(){

}


        }); 

Answer №2

This code snippet contains a working example on JS Fiddle: http://jsfiddle.net/nfdebmen/4/

The code allows for multiple PlaceHolders to be used. In this specific example, there are 4 PlaceHolders included.

var _toggle = {
    
  totalNodes: null,
  lastNode: 0,
    
  duration: 1000,
    
  init: function () {
  
    _toggle.totalNodes = $(".toggle").length;
      
    _toggle.next();
  
  },
    
  next: function () {
  
    var nextNode = _toggle.lastNode + 1;
      
    if (nextNode >= _toggle.totalNodes) {
      
      nextNode = 0;
      
    }
      
    //
      
    $(".toggle:eq(" + _toggle.lastNode + ")").fadeOut(_toggle.duration, function () {
    
      $(".toggle:eq(" + nextNode + ")").fadeIn(_toggle.duration);
        
      _toggle.next();
    
    });
      
    _toggle.lastNode = nextNode;
      
  }
    
}

$(document).ready(function () {

  _toggle.init();

});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<section id="text-slider">
  <div class="container">
    <div class="row">
      <div class="col-md-4">
        <p class = "toggle active">"IT WAS SUCH A PLEASURE WORKING WITH STOKES STREET ON SPECTROSPECTIVE. THEY SURPASSED ALL EXPECATIONS. WE WERE GOBSMACKED, FRANKLY."</p>
        <p class = "toggle" style = "display: none;">test</p>
        <p class = "toggle" style = "display: none;">Ahsan</p>
        <p class = "toggle" style = "display: none;"><a href = 'http://aboutahsan.com' target = "_blank">http://aboutahsan.com</a></p>
      </div><!-- col -->
      <div class="col-md-4">
        <p></p>
      </div><!-- col -->
      <div class="col-md-4">
        <p></p>
      </div><!-- col -->
    </div><!-- row -->
  </div><!-- container -->
</section><!-- text slider -->

Answer №3

Have you tried implementing .fadeToggle()?

$(document).ready(function() {
  var p = $("p[id^=fade]");
  function fadeThem() {
    return p.fadeToggle(3000, function() {
      fadeThem()
    });
  }
  fadeThem();
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<section id="text-slider">
  <div class="container">
    <div class="row">
      <div class="col-md-4">
        <p id="fade1">"IT WAS SUCH A PLEASURE WORKING WITH STOKES STREET ON SPECTROSPECTIVE. THEY SURPASSED ALL EXPECATIONS. WE WERE GOBSMACKED, FRANKLY."</p>
        <p id="fade2" style="display:none">test</p>
      </div>
      <!-- col -->
      <div class="col-md-4">
        <p></p>
      </div>
      <!-- col -->
      <div class="col-md-4">
        <p></p>
      </div>
      <!-- col -->
    </div>
    <!-- row -->
  </div>
  <!-- container -->
</section>
<!-- text slider -->

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

Creating dynamic parameters in Nuxt JS for nested pages

Struggling with creating a dynamic param for my nuxt JS page's correct folder structure. The URL I'm aiming for is similar to: https://example.com/landing/123 The number 123 is the dynamic parameter I want to retrieve using this.$route. I&apos ...

The perplexity surrounding jQuery/Ajax variable naming

Could you please explain the difference between these two IDs? var id = $(this).attr("name"); var id = 1; The issue is that the first variable example is not functioning properly $.ajax({ type: "POST", url: "http: ...

Begin the process by hitting the enter key

Check out my website here! I want to enhance the functionality of my site by triggering a Google Images search when the 'enter' key is pressed, instead of just relying on a button click. This is the HTML structure: <p>Enter your search t ...

Import and load numerous JSON files into a responsive and visually appealing Bootstrap table

I am new to coding in javascript and I'm seeking assistance in loading multiple JSON files to populate a bootstrap table. Currently, I've managed to create a table by manually combining the content from different files into one variable called l ...

The vertical shift in one of the inline table cell DIVs is being caused by two of them

I've been searching for a solution to my issue, but have come up empty-handed. I apologize if this has already been discussed before. My HTML page contains a section that appears as follows: <div id=wrap> <div id=lb> Content ...

Leveraging three.js and tween.js to spin an object in 90-degree increments for a seamless 360-degree rotation loop

I've managed to get my animation working, but not in the exact way I had envisioned. What I'm trying to achieve is to have an object rotate 90 degrees with a delay (which currently works), and then continue rotating another 90 degrees, repeating ...

Run JavaScript code that has been fetched using AJAX

I am currently working on dynamically updating a javascript array based on data returned from an ajax call. Here is the code I have been using: function makeAjaxCall(data) { xmlhttp = new XMLHttpRequest(); xmlhttp.open("POST", "ShowBusiness", fals ...

Custom range-slider in Angular with flexible min/max values

I am currently working with the angular ui-slider and I am looking to dynamically load the min- and max values for the slider from an external source. This will allow me to set them in a structured way, similar to the following: $scope.minMaxValues = { ...

Struggling to compile TypeScript code

After updating TypeScript and NodeJS to the latest versions, I decided to test a simple TypeScript example by compiling it using both Node terminal and Windows 10 command line. Here is the code snippet I used: let greeting:string = "Hello!"; console.log(g ...

Having trouble using $.post in jQuery AJAX with the .click() method?

I am experiencing some issues with my ajax request. It appears that the $.post method is not functioning as expected, as no request is being sent. There is also no information showing up in Firebug. Interestingly, I can make this code work: $('.c ...

Tips for concealing an element with Vue-html-to-paper

I am working on printing pages and need to hide certain elements. Specifically, I do not want the two button signs to be displayed when I print. I am using a plugin called https://www.npmjs.com/package/vue-html-to-paper Here is my code: <div id="print ...

Tips for enabling menu wrapping with up and down arrow keys

In the Bootstrap 5 menu, when you press the up arrow key on the first item or the down arrow key on the last item, nothing happens. Is there a way to configure the up arrow to move to the last item in the menu if pressed on the first item, and for the dow ...

The custom modal does not seem to be compatible with the CSS3 animation library,

I managed to create my own Full page modal successfully, but now I want to enhance it with an animation. I am attempting to use animate.css to add animation effects such as zoomIn or zoomOut, but it's not working as expected. Here is the link to my J ...

What causes the content to spill over when the height of my content is set to 100%?

My webpage layout consists of a header, footer, and content section. I noticed that when I set the height of the content div to 100%, it overflows instead of expanding accordingly. I've added some CSS code to style these elements: * { margin: 0; ...

Animating an SVG in a circular motion starting from its central point with either CSS or XML

I've been grappling with the challenge of getting a part of my personal logo to rotate. My logo consists of "SB" and an asterisk, denoted as "st1," which is what I'm attempting to animate. Despite my attempts using XML and CSS, I've encounte ...

PHP reCAPTCHA integration with AJAX functionality

I have been attempting to integrate a reCAPTCHA into my PHP contact form using AJAX. The goal is to present the user with the reCAPTCHA challenge and allow them to input it. If the input is incorrect, an error message should be displayed without refreshing ...

Can anyone help me with integrating a search functionality inside a select tag?

I am working on a select dropdown and want to add a search box to easily filter through the available options. I know this can be done using the chosen library, but I prefer to implement it using plain JavaScript or jQuery. Does anyone have suggestions on ...

retrieving data in an array using an ajax request

Having trouble extracting two values from an ajax call. I can't seem to separate the results returned from the call. $.ajax({ type:'POST', url: 'inc/getuser.php?q='+boxnum, success:functi ...

Can you elaborate on how a numeric value is passed as an argument to the function within a React Tic Tac Toe tutorial when handling a click event?

I'm a bit confused about how the onClick event works with the handleClick function and passing numbers as arguments. Is this related to closures? Can someone explain how the numbers are passed as arguments when the click event happens? Also, should ...

Set a specific width for inline list items

When it comes to having list items with images on the left and text on the right, the issue arises when each image is of a different width. This can cause the text to not align vertically next to their respective images. Is there a way to ensure that t ...