top of page

Just Jolly Functions

Functions: Adding a Text Slider with HTML, CSS, and JavaScript

The Text Slider is a simple and interactive component that displays multiple pieces of text in a slideshow format. Users can navigate through the text slides using the "Previous" and "Next" buttons or by clicking on the navigation dots below the slider. The slider is responsive, styled cleanly with a modern design, and can easily be customized to suit various use cases.



Text Slider Code:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Text Slider</title>
    <style>
        /* Basic reset and styling */
        body {
            font-family: Arial, sans-serif;
            margin: 0;
            padding: 0;
            background-color: #f4f4f4;
        }

        /* Container for the slider */
        .slider-container {
            position: relative;
            width: 80%;
            max-width: 600px;
            margin: auto;
            padding: 20px;
            background-color: white;
            border-radius: 8px;
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
            text-align: center;
        }

        /* The text container inside the slider */
        .slider-text {
            font-size: 1.5rem;
            color: #333;
            padding: 20px;
            display: none; /* Initially hide all text */
        }

        /* Show the active text */
        .active {
            display: block;
        }

        /* Previous and Next buttons */
        .prev, .next {
            position: absolute;
            top: 50%;
            transform: translateY(-50%);
            background-color: rgba(0, 0, 0, 0.5);
            color: white;
            font-size: 18px;
            padding: 10px;
            cursor: pointer;
            border: none;
            border-radius: 5px;
            z-index: 1;
        }

        .prev {
            left: 10px;
        }

        .next {
            right: 10px;
        }

        /* Dots for slide indicators */
        .dots {
            text-align: center;
            padding-top: 10px;
        }

        .dot {
            display: inline-block;
            height: 15px;
            width: 15px;
            margin: 0 5px;
            background-color: #bbb;
            border-radius: 50%;
            cursor: pointer;
        }

        .active-dot {
            background-color: #717171;
        }
    </style>
</head>
<body>

<div class="slider-container">
    <div class="slider-text active">Welcome to my Just Jolly post!</div>
    <div class="slider-text">Like and share my post.</div>
    <div class="slider-text">Feel free to leave comments.</div>
    <div class="slider-text">Enjoy reading my post.</div>

    <button class="prev">&#10094;</button>
    <button class="next">&#10095;</button>
</div>

<div class="dots">
    <span class="dot"></span>
    <span class="dot"></span>
    <span class="dot"></span>
    <span class="dot"></span>
</div>

<script>
    let currentSlide = 0;
    const texts = document.querySelectorAll(".slider-text");
    const dots = document.querySelectorAll(".dot");

    // Function to show the current slide
    function showSlide() {
        // Hide all texts and remove active class from dots
        texts.forEach(text => text.classList.remove("active"));
        dots.forEach(dot => dot.classList.remove("active-dot"));

        // Show the active slide
        texts[currentSlide].classList.add("active");
        dots[currentSlide].classList.add("active-dot");
    }

    // Event listeners for the Previous and Next buttons
    document.querySelector(".prev").addEventListener("click", () => {
        currentSlide = (currentSlide === 0) ? texts.length - 1 : currentSlide - 1;
        showSlide();
    });

    document.querySelector(".next").addEventListener("click", () => {
        currentSlide = (currentSlide === texts.length - 1) ? 0 : currentSlide + 1;
        showSlide();
    });

    // Event listeners for the dots
    dots.forEach((dot, index) => {
        dot.addEventListener("click", () => {
            currentSlide = index;
            showSlide();
        });
    });

    // Automatically change slide every 3 seconds
    setInterval(() => {
        currentSlide = (currentSlide + 1) % texts.length;
        showSlide();
    }, 3000);

    // Initial display
    showSlide();
</script>

</body>
</html>

Explanations of the Code:


HTML Structure

  1. Slider Container:

    • The .slider-container class is a div that holds the entire slider.

    • Inside this container, there are multiple <div class="slider-text"> elements. Each one contains the text that will be displayed in the slider.

  2. Text Elements:

    • Each slide has the class slider-text. The text elements will initially be hidden except for the one with the class active.

  3. Previous and Next Buttons:

    • The buttons <button class="prev">&#10094;</button> and <button class="next">&#10095;</button> are used to navigate between slides manually.

    • They are positioned on the left and right side of the container, respectively.

  4. Dots:

    • Dots below the text slider represent the navigation indicator for each slide. These are <span class="dot"> elements.

    • When a dot is clicked, the corresponding text slide is shown.


CSS Styling

  1. Basic Reset:

    • The body and other elements are reset to ensure there is no extra margin or padding. This creates a clean and uniform layout.

  2. Slider Container:

    • The .slider-container has a max-width of 600px, ensuring the slider is responsive. It has padding, a white background, rounded corners, and a shadow to make it look neat and modern.

  3. Text Elements:

    • The .slider-text elements are initially hidden using display: none; and are shown when they have the active class.

  4. Navigation Buttons:

    • The buttons are absolutely positioned on the left and right sides of the container. They are styled with a semi-transparent black background, white text, and a subtle border radius for a smooth, modern look.

  5. Dots:

    • Dots are created with the dot class and styled as small circular indicators that turn darker when active (using .active-dot).


JavaScript Logic

  1. currentSlide Variable:

    • This keeps track of which slide is currently being displayed. Initially, it's set to 0, which corresponds to the first slide.

  2. showSlide() Function:

    • This function hides all text slides and removes the active class from all dots. Then, it adds the active class to the current slide and the corresponding dot to indicate which slide is active.

  3. Manual Navigation (Prev/Next Buttons):

    • Event listeners are added to the Previous (.prev) and Next (.next) buttons. Clicking these buttons changes the currentSlide and calls showSlide() to update the displayed text.

    • If the currentSlide reaches the first slide (0), clicking "Prev" will cycle to the last slide, and if it reaches the last slide, clicking "Next" will cycle back to the first slide.

  4. Dot Navigation:

    • Each dot has an event listener attached. When a dot is clicked, the currentSlide is updated to the index of the clicked dot, and showSlide() is called to update the displayed text.

  5. Automatic Slide Change:

    • The setInterval() function automatically advances the slide every 3 seconds. After each interval, the currentSlide is incremented, and showSlide() is called to update the displayed text.

  6. Initial Slide Display:

    • The showSlide() function is called when the page loads to ensure the first slide is displayed initially.


How to Customize the Slider:

  1. Change Text Content:

    • Modify the text inside the <div class="slider-text"> elements to display your own messages.

  2. Adjust Text Size/Font:

    • Modify the .slider-text CSS class to change the font size, style, or color to suit your design preferences.

  3. Change the Slide Transition Interval:

    • Modify the setInterval() function to change how often the slide automatically changes (currently set to 3 seconds).

    setInterval(() => { currentSlide = (currentSlide + 1) % texts.length; showSlide(); }, 3000); // Change the 3000 to a different number (in milliseconds) for different timing

  4. Modify Button Styling:

    • Update the .prev and .next CSS classes to change button colors, sizes, or shapes.

  5. Add/Remove Dots:

    • To match the number of slides, add or remove dots in the <div class="dots"> section by adding or removing <span class="dot"></span> elements.


Customizing the Text Slider

The text slider you have is quite customizable. Here are different ways you can change the functionality, appearance, and behavior to suit your needs. I'll walk you through how users can make some simple and advanced changes.


1. Customizing Text Content

To change the text shown in the slider, simply modify the content inside the <div class="slider-text"> elements.


Example:

<div class="slider-text active">Welcome to my Just Jolly post!</div>
<div class="slider-text">Like and share my post.</div>
<div class="slider-text">Feel free to leave comments.</div>
<div class="slider-text">Enjoy reading my post.</div>

You can replace the text within the <div> tags with any custom message you want. For instance:

<div class="slider-text active">Check out our latest features!</div>

You can add or remove the <div class="slider-text"> blocks as needed, allowing you to control how many slides (text messages) appear in the slider.


2. Change the Font Style and Size

To modify the font family, size, or color, you can adjust the CSS inside the <style> tag.


Example to change font size and font family:

/* The text container inside the slider */
.slider-text {
    font-size: 2rem;  /* Increase font size */
    font-family: 'Courier New', Courier, monospace;  /* Change font to Courier */
    color: #333;
    padding: 20px;
    display: none; /* Initially hide all text */
}

You can change the font-size, font-family, and color properties to any values you prefer.


3. Adjust the Transition Duration

If you want the text transitions (changes between slides) to be faster or slower, you can modify the transition property in the CSS.


Example to change transition speed:

/* Show the active text */
.active {
    display: block;
    transition: opacity 1s ease-in-out; /* Increase transition speed to 1 second */
}

You can adjust the 1s to any value (e.g., 0.5s for faster transitions or 2s for slower transitions).


4. Modify Slide Change Speed

In the JavaScript, the slide changes automatically every 3 seconds. You can adjust this interval to change the speed at which the slides automatically transition.


Example to change slide change speed:

// Automatically change slide every 5 seconds (instead of 3 seconds)
setInterval(() => {
    currentSlide = (currentSlide + 1) % texts.length;
    showSlide();
}, 5000);  // Change the 3000 to 5000 (in milliseconds)

You can change the 3000 (3 seconds) to any other number for a faster or slower automatic slide transition.


5. Modify Navigation Button Design

To change the design of the Previous (.prev) and Next (.next) buttons, you can modify the CSS for those classes.


Example to change button colors and size:

/* Previous and Next buttons */
.prev, .next {
    position: absolute;
    top: 50%;
    transform: translateY(-50%);
    background-color: #4CAF50; /* Change the button color to green */
    color: white;
    font-size: 20px; /* Increase font size */
    padding: 12px 20px; /* Increase padding */
    cursor: pointer;
    border: none;
    border-radius: 8px; /* Make corners round */
    z-index: 1;
}

.prev:hover, .next:hover {
    background-color: #45a049;  /* Darken color when hovered */
}

This will change the background color of the buttons to green, and you can further style them to fit your design needs.


6. Adjust Dot Indicators

You can modify the dot indicator’s style and behavior, which represents the current slide.


Example to change dot size and color:

/* Dots for slide indicators */
.dot {
    display: inline-block;
    height: 20px;  /* Increase dot size */
    width: 20px;   /* Increase dot size */
    margin: 0 10px;  /* Adjust spacing between dots */
    background-color: #bbb;
    border-radius: 50%;
    cursor: pointer;
}

/* Active dot */
.active-dot {
    background-color: #717171;
}

You can change the height, width, background-color, and margin to fit your design needs.


7. Add More Slides

If you'd like to add more text slides to the slider, simply add more <div class="slider-text"> elements inside the .slider-container:


Example to add a new slide:

<div class="slider-text">Welcome to our platform!</div>
<div class="slider-text">Check out our latest blog posts.</div>
<div class="slider-text">Stay tuned for new updates.</div>
<div class="slider-text">Don't forget to subscribe!</div>

Remember to also update the <div class="dots"> section to match the number of slides. For example:


Example to add a new dot:

<div class="dots">
    <span class="dot"></span>
    <span class="dot"></span>
    <span class="dot"></span>
    <span class="dot"></span> <!-- New dot for the new slide -->
</div>

8. Pause and Play Functionality

You can add a button to allow users to pause and resume the automatic sliding of text. This can be done by adding a "Pause/Play" button and modifying the JavaScript.


Example to add a pause/play button:

<!-- Pause/Play Button -->
<button class="pause-play" style="position: absolute; top: 10px; right: 10px; background-color: rgba(0, 0, 0, 0.5); color: white; font-size: 18px; padding: 10px; border: none; border-radius: 5px;">Pause</button>

Then, add the following JavaScript code to control the pause and play functionality:

let isPlaying = true;
const pausePlayButton = document.querySelector('.pause-play');

// Function to toggle between pause and play
function togglePlayPause() {
    if (isPlaying) {
        clearInterval(autoSlide);  // Pause the automatic slide change
        pausePlayButton.textContent = "Play";
    } else {
        autoSlide = setInterval(() => {
            currentSlide = (currentSlide + 1) % texts.length;
            showSlide();
        }, 3000); // Adjust the speed here if needed
        pausePlayButton.textContent = "Pause";
    }
    isPlaying = !isPlaying;
}

// Add event listener to the pause/play button
pausePlayButton.addEventListener("click", togglePlayPause);

9. Make the Slider Responsive

To ensure the slider looks good on different screen sizes, you can use CSS media queries to adjust the layout on smaller screens.


Example for responsiveness:

@media (max-width: 600px) {
    .slider-container {
        width: 90%;
        padding: 10px;
    }

    .slider-text {
        font-size: 1rem;  /* Decrease font size on small screens */
    }
}

This media query ensures the slider container and text scale appropriately on devices with a screen width of 600px or less.


Conclusion:

This is a simple text slider that can be customized in various ways, such as changing the content, styling, transition timing, and slide navigation behavior. It’s a great starting point for creating more advanced sliders and can be easily modified to fit different types of content.


With these customization options, you can easily modify the text slider to fit your specific needs. Whether you want to change the text content, adjust the speed, or style the slider differently, the provided instructions give you a great foundation for building and tailoring your text slider.


Feel free to experiment with these modifications and make the text slider uniquely yours!


bottom of page