jQuery text slideshow not working properly when a <br> tag is present

I'm looking to develop a basic text slideshow that smoothly transitions between phrases and loops continuously. However, I've run into an issue where the slideshow malfunctions when I add a line break in the text. It only seems to work properly when the text is on a single line. Any help or guidance on this matter would be highly appreciated.

Below is the code snippet along with a link to the jsFiddle for reference: http://jsfiddle.net/rezasan/fnbn8sbc/

//HTML
<div id="index_splashtext">
  <h3>An<br/>Intimate<br/>Hideaway</h3>
  <h3>A<br/>Paradise<br/>Preserved</h3>
</div>

//CSS
#index_splashtext {
  width:311px;
  margin:0 auto;
  height: 330px;
  margin-top: 25px;
  text-align:center;
}
#index_splashtext h3 {
  position: absolute;
  font-size: 4.5em;
  line-height: 1.2em;
  font-family: "adobe-garamond-pro",sans-serif;
  letter-spacing: 0.05em;
  font-weight: 400;
  margin-bottom: 70px;
  color: red;
}

//jQuery
$(function(){
    $('#index_splashtext h3:gt(0)').hide();
    setInterval(function(){
    $('#index_splashtext :first-child').fadeOut(2500)
    .next('h3').fadeIn(2500)
    .end().appendTo('#index_splashtext');}, 
    8000);
});

Answer №1

If you want to achieve the desired effect, it is recommended to utilize $('#index_splashtext h3:first') in place of

$('#index_splashtext :first-child')
. This ensures that the :first-child selector does not include the <br /> elements, preventing them from being hidden without the possibility of fading back in:

$(function () {
    $('#index_splashtext h3:gt(0)').hide();
    setInterval(function () {
        $('#index_splashtext h3:first').fadeOut(2500)
            .next('h3').fadeIn(2500)
            .end().appendTo('#index_splashtext');
    },
    8000);
});

Check out this jsFiddle example for reference

Answer №2

Here is a method to achieve the desired effect, which is also easily scalable!

HTML:

<div id="splash_content">
<h2></h2>
</div>

Javascript:

$(function(){
    var messages=[];
    var counter=0;
    var fadeInDuration = 2500;
    var fadeOutDuration = 2500;
    var holdDuration = 1000;
    messages.push('Welcome<br />to<br />Paradise');
    messages.push('An<br />Escape<br />Awaits');

    $('#splash_content h2').hide();

    setInterval(function(){
        $('#splash_content :first-child').
        html(messages[counter]).
        fadeIn(fadeInDuration).
        delay(holdDuration).
        fadeOut(fadeOutDuration);

        counter++;
        if(counter > messages.length-1){counter=0;}
        },fadeInDuration+fadeOutDuration+holdDuration + 1000);
});

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

Troubleshooting date format errors when parsing two parameters in a jQuery AJAX request

Does anyone have advice on how to make an ajax call with two parameters - number and date? I encountered the following error: Warning: date_format() expects parameter 1 to be DateTimeInterface, boolean given in... Here is the HTML code involved: <di ...

Error found in Paper.js at line 81: Uncaught TypeError - Unable to access properties of undefined (specifically '1') while utilizing Material UI and Joy UI

I am encountering an issue while using react js with material UI and JoyUI, where I am getting a blank screen. Below is the code snippet causing the problem: import React from 'react'; import { CssVarsProvider } from '@mui/joy/styles'; ...

Retrieve the total count of tables within a specific div element

If I have an unspecified amount of tables within a div, how can I determine the total number of tables using either plain JavaScript or jQuery? ...

Ways to position a dropdown submenu on the left-hand side in Bootstrap 4

I'm currently utilizing Bootstrap 4 with a multilevel dropdown feature as shown in the image below: https://i.sstatic.net/xgVLp.png My goal is to shift the submenus to the left side, in the opposite direction from how they are currently displayed. De ...

css numerous rectangles lined up in a horizontal manner

I'm having trouble figuring out how to create multiple parallelograms horizontally with some space between them using CSS. Is there a way to modify the code below for this purpose? Here is the HTML: <div id="parallelogram"></div> And he ...

What causes Gun.js to generate duplicate messages within a ReactJs environment?

I need assistance with my React application where gun.js is implemented. The issue I am facing is that messages are being duplicated on every render and update. Can someone please review my code and help me figure out what's wrong? Here is the code s ...

How can I create a full screen button in WebGL with HTML and JavaScript?

How can I create a WebGL full screen button using HTML and JavaScript? I've done multiple searches on this topic but couldn't find a suitable solution. However, I did manage to find a button that works for putting my page in full screen mode. &l ...

Tips for making a div with a single triangular side using Reactjs

Hey there, I'm looking to design a header similar to the one shown in this image. Can someone provide guidance on how I can achieve this UI using React.js? https://i.sstatic.net/zmW5x.jpg ...

Arranging div elements in a vertical stack with CSS

Looking for help in arranging div elements in a way that they resemble an equalizer, similar to the one shown in this screenshot Equalizer. Can you provide advice on how to remove the dots from the list? I've already attempted using the CSS property ( ...

Understanding the relationship between CSS height and line-height can help developers create more

Is there a way to use CSS to set the height of a box based on its own line-height or its parent's line-height? This feature would make it much simpler to create text areas that are a specific multiple of lines, for example, displaying exactly three l ...

Rotating picture panel featuring an assortment of images

I'm working on a responsive image grid and looking to create a unique design. Instead of repeating the same image in each box, I would like to implement a mask that shows the image across all boxes, similar to this: Additionally, I want to add a flip ...

When you hover over nested HTML-lists in a webpage, make the tree's long text lines expand gracefully using a combination of HTML, CSS

In my Angular 4 application, I have a left div that displays a tree of items as recursive HTML lists. When there is a long text, it gets cut off by the border of the div and a scrollbar appears. I want to have the text expand beyond the border and show in ...

Position fixed property

While experimenting with the fixed position property to maintain a constantly positioned nav bar on mobile, I encountered some challenges during the trial and error process. However, by utilizing margin top and translation functions, I was able to successf ...

C++ engine for embedding HTML, CSS, and scripts

I am working on a C++ Windows native application and I'm looking for an HTML engine to help with displaying data in the GUI. After some searching, I came across Sciter, which seems like a good fit. However, I've noticed that it lacks complete CS ...

The "truth" parameter within the xmlHttpRequest .open() function

After referencing MDN, it mentioned that by default, the JavaScript function will keep running until the server response is received. This is essentially what AJAX is all about - Asynchronous JavaScript and XML. Even though I have some experience with A ...

I'm curious if anyone has had success utilizing react-testing-library to effectively test change events on a draftJS Editor component

​I'm having trouble with the fireEvent.change() method. When I try to use it, I get an error saying there are no setters on the element. After that, I attempted using aria selectors instead. const DraftEditor = getByRole('textbox') Draf ...

Chrome is failing to display the CSS background image

My issue arises when viewing the webpage on Google Chrome for macOS as it does not display background-images from the CSS stylesheet. However, everything works fine when using Coda 2. Furthermore, the page displays correctly in Chrome when opened directly ...

What is the best method to eliminate excess space between images on a webpage using CSS?

Currently, I am developing a small project just for myself. The main page will consist of multiple images, each spanning the entire page. I am having trouble eliminating this unwanted spacing: https://i.sstatic.net/7eLl5.jpg Your assistance would be gre ...

Create a visually appealing layout by connecting items in an unordered list with lines using HTML

Currently, I am developing a family lineage platform where I showcase an individual's progeny in a non-sequential and tiered catalog beneath their identity. Does anyone have suggestions on how to connect the bullet points within this listing via bord ...

Combining two arrays in a view and then storing them in a database using Codeigniter

Hello everyone, I am a new member on this platform... Currently, I am working with the codeigniter framework and have two separate arrays in my view. The first array includes data from a form input named customerData, customerData = { customerid ...