The logo marquee unexpectedly bounces up and down at the end of every

Struggling with a JavaScript code issue. The Logo marquee suddenly jumps at the end of each loop:

I've tried everything, but I can't seem to fix it and make it work as intended

Here's the complete script that technically works, but causes the animation to jump at a certain point.

<!DOCTYPE html>
<html>

<head>
    <title>Vertical Logo Marquee</title>
    <style>
        .marquee-container {
            height: 200px;
            /* Adjust height as needed */
            overflow: hidden;
            position: relative;
        }

        .marquee-list {
            position: absolute;
            top: 0;
            width: 100%;
            height: auto;
            /* Change height to auto */
            list-style: none;
            margin: 0;
            padding: 0;
            display: flex;
            flex-direction: column;
            /* Add flex-direction: column */
            gap: 12px;
            /* Add gap between items */
        }

        .marquee-list li {
            height: 150px;
            /* Change the height of list items */
            display: flex;
            align-items: center;
            background-color: red;
        }

        .marquee-list li img {
            max-height: 100%;
            max-width: 100%;
            margin: 0 auto;
        }
    </style>
</head>

<body>
    <div class="marquee-container">
        <ul class="marquee-list">
            <li><img src="logo1.png" alt="Logo 1"></li>
            <li><img src="logo2.png" alt="Logo 2"></li>
            <li><img src="logo3.png" alt="Logo 3"></li>
            <li><img src="logo4.png" alt="Logo 4"></li>
            <li><img src="logo5.png" alt="Logo 5"></li>
        </ul>
    </div>

    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <script>
        $(document).ready(function () {
            var marqueeContainer = $(".marquee-container");
            var marqueeList = $(".marquee-list");
            var marqueeItems = $(".marquee-list li");
            var marqueeHeight = marqueeContainer.height();
            var itemHeight = marqueeItems.outerHeight(true); // Include margin in height calculation
            var gap = 12; // Gap between items
            var totalHeight = (itemHeight + gap) * marqueeItems.length - gap; // Adjusted total height
            var topValue = 0;

            // Duplicate marquee items to create a continuous loop
            marqueeItems.clone().appendTo(marqueeList);

            function moveMarquee() {
                topValue -= 1;

                if (topValue < -totalHeight) {
                    topValue = 0;
                }

                marqueeList.css("top", topValue);
            }

            var marqueeInterval = setInterval(moveMarquee, 20);

            // Pause marquee on hover
            marqueeContainer.on("mouseenter", function () {
                clearInterval(marqueeInterval);
            }).on("mouseleave", function () {
                marqueeInterval = setInterval(moveMarquee, 20);
            });
        });
    </script>
</body>

</html>

Seeking help from anyone who can provide insight into what might be causing this issue or offer a solution.

Answer №1

The problem lies within the moveMarquee function. Always remember to consider the gap before resetting the topValue.

function moveMarquee() {
    topValue -= 1;
    console.log({ topValue });
    if (topValue < -(totalHeight + gap)) {// Don't forget about the gap
        topValue = 0;
    }

    marqueeList.css("top", topValue);
}

Note: To enhance performance and achieve smoother animation, utilizing css @keyframes might be a good idea.

Answer №2

By adjusting the topValue in the loop creation and re-assigning it within the moveMarquee function, I was able to resolve the issue.

if (topValue < -totalHeight) {
    topValue = 12;
}

The gap of 12px after each cycle was the root cause of the problem.

<!DOCTYPE html>
<html>

<head>
    <title>Vertical Logo Marquee</title>
    <style>
        .marquee-container {
            height: 200px;
            /* Adjust height as needed */
            overflow: hidden;
            position: relative;
        }

        .marquee-list {
            position: absolute;
            top: 0;
            width: 100%;
            height: auto;
            /* Change height to auto */
            list-style: none;
            margin: 0;
            padding: 0;
            display: flex;
            flex-direction: column;
            /* Add flex-direction: column */
            gap: 12px;
            /* Add gap between items */
        }

        .marquee-list li {
            height: 150px;
            /* Change the height of list items */
            display: flex;
            align-items: center;
            background-color: red;
        }

        .marquee-list li img {
            max-height: 100%;
            max-width: 100%;
            margin: 0 auto;
        }
    </style>
</head>

<body>
    <div class="marquee-container">
        <ul class="marquee-list">
            <li><img src="logo1.png" alt="Logo 1"></li>
            <li><img src="logo2.png" alt="Logo 2"></li>
            <li><img src="logo3.png" alt="Logo 3"></li>
            <li><img src="logo4.png" alt="Logo 4"></li>
            <li><img src="logo5.png" alt="Logo 5"></li>
        </ul>
    </div>

    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <script>
        $(document).ready(function () {
            var marqueeContainer = $(".marquee-container");
            var marqueeList = $(".marquee-list");
            var marqueeItems = $(".marquee-list li");
            var marqueeHeight = marqueeContainer.height();
            var itemHeight = marqueeItems.outerHeight(true); // Include margin in height calculation
            var gap = 12; // Gap between items
            var totalHeight = (itemHeight + gap) * marqueeItems.length - gap; // Adjusted total height
            var topValue = 0;

            // Duplicate marquee items to create a continuous loop
            marqueeItems.clone().appendTo(marqueeList);

            function moveMarquee() {
                topValue -= 1;

                if (topValue < -totalHeight) {
                    topValue = 12;
                }

                marqueeList.css("top", topValue);
            }

            var marqueeInterval = setInterval(moveMarquee, 20);

            // Pause marquee on hover
            marqueeContainer.on("mouseenter", function () {
                clearInterval(marqueeInterval);
            }).on("mouseleave", function () {
                marqueeInterval = setInterval(moveMarquee, 20);
            });
        });
    </script>
</body>

</html>

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

Using a javascript parameter in a cshtml file to filter data in a datatable

Here is the model code public class viewCase { public List<string> lstCategory { get; set; } public DataTable dtWrkTsk { get; set; } } This is the controller code string query = "SELECT WorkFlowID,Subject,Category FROM CMSTasksWorkFlow" ob ...

The query limit issue in Sails JS

I am encountering a 413 (Request Entity too large) error when making a request to a Sails Js app (v0.12). Despite my attempts to raise the request limit in the bodyParser, I have not seen any changes take effect. In config/http.js, I included a customize ...

Make the most of your Bootstrap 3 layout by utilizing a full page container that fills both the width and height between a fixed header and

I am currently working on a basic bootstrap page with the Example template from Bootstrap's website. I want the content in the middle to take up the space between the header and footer, while ensuring that both the header and footer remain visible at ...

What steps can I take to address the issue of missing @angular/Core modules?

I am encountering an issue with running my Angular 2 project. Here's what I have tried: - Attempted to run the project using npm install and npm start, but it did not work - Cloned a quickstart from Github and replaced it with my src folder, only to ...

Creating various iterations of a single CSS animation

Attempting to animate multiple instances of a cross mark animation can be tricky. The animation works well with one instance, but struggles when trying to create more than one. The challenge lies in controlling the size and position of each cross animation ...

Could you provide me with a demonstration of cross-domain functionality?

Similar Inquiry: Methods to bypass the same-origin policy Let's consider two domains for this example - "" and "". The first domain "" is generating data in JSON format as shown below: { "str_info": [ { "str_name": "Mark ...

refreshing the webpage with information from an API request

I am completely new to web development, so please bear with me. I am attempting to make a simple API call: const getWorldTotal = async () => { const response = await fetch('https://cors-anywhere.herokuapp.com/https://health-api.com/api/v1/cov ...

What is the most efficient way to utilize a single connection to interact with MongoDB?

I have a series of functions that are responsible for running various queries. Here is the code I am working with: var MongoClient = require('mongodb').MongoClient; async function createDatabase() { MongoClient.connect(urlMongoDB, function(er ...

Guide on automatically filling input text fields with database values when a checkbox is clicked

I need to automate the process of filling in multiple input text fields with values from variables retrieved through a SELECT query when a checkbox is clicked. For instance, if the SELECT query returns the Warehouse Street Address ($WarehouseStreetAddres ...

Effortlessly find data in an HTML table and showcase the results instantly without

I am looking for a way to implement search functionality in my table without refreshing the page. The fields above the table are used for searching and I want the results to be displayed within the same table. Here is an example of the code: <?php $for ...

Elements that allow for asynchronous data submission without requiring a traditional submit button

Hey there, I could really use some help with this puzzle I'm trying to solve. Here's the situation: <ul> <li>Number: <span id="Number">123</span></li> </ul> I want to set up a connection between t ...

Ensuring that only one field is selected with mandatory values using Joi validation

Is there a way to create a validation rule utilizing Joi that ensures if valueA is empty, then valueB must have a value, and vice versa? My current approach involves using Joi for validating an array of objects with properties valueA and valueB. Below is ...

Organize the array of objects

Within my React state, I aim to rearrange a set of 3 objects by always placing the selected one in the middle while maintaining ascending order for the others. Currently, I utilize an order property within each object as a way to keep track of the sequenc ...

Create a spinner control on an HTML webpage with the help of JavaScript

I am attempting to create a spinner control on my webpage, and I have tried the following code: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.o ...

Guide to attempting an asynchronous function again in JavaScript with a time delay

I've been facing a challenge while trying to retrieve a record from a database. The issue of race conditions often leads to situations where the record might not be available when attempting the initial fetch. How can I implement retry logic to overco ...

Interact with HTML style attribute using JavaScript

Is there a way to retrieve a specific CSS property from the style attribute of an HTML element, without considering the stylesheet or computed properties? For example: <div style="float:right"></div> function fetchStyleValue(element, propert ...

Display all elements on a webpage using Javascript without the need for scrolling

I'm looking for a way to ensure all items on a webpage load immediately, without having to scroll down multiple times before running a script to find a specific item. Is there a method to have all items load at once without the need to manually scroll ...

Manage over 200 checkboxes by storing them in the state

I am facing an issue with managing checkboxes within a table in my application. The table fetches user data and renders each row as its own component, with each row containing a checkbox. The goal is to enable the users to select checkboxes and retrieve t ...

Stylus Vue Vite: The Trio of Global Variables

I'm trying to figure out how to globally import and use stylus variables in my Vue Vite project. How can I achieve this and access the variables within the script section of my Single File Component (SFC)? Below is an excerpt from my Vite config: exp ...

Enhancing Rails: Tailoring the flash message to suit individual needs

I am embarking on my journey with ruby on rails and could use some guidance with the following scenario. In the application.html.erb file, flash messages are configured to fade out after a few seconds by default. <div id="message" class="modal alert a ...