skip to Main Content

Having a problem with this code in JavaScript. The Logo marquee suddenly jump after at the end of each loop:

I have tried everything but I cant fix it and make it work the way I want

Here’s the entire script. that does work, but it makes the animation jump at a point. So that’s a problem to be sure.

<!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>

I hope that any one can help me and let me know what is the problem or can give me a hint.

2

Answers


  1. The issue is in moveMarquee function. The gap must be taken into account before resetting topValue.

    function moveMarquee() {
        topValue -= 1;
        console.log({ topValue });
        if (topValue < -(totalHeight + gap)) {// add a gap here
            topValue = 0;
        }
    
        marqueeList.css("top", topValue);
    }
    

    Note: The animation will run smoothly and perform better by using css @keyframes I think.

    Login or Signup to reply.
  2. I fixed it by changing the topValue when creating the loop, which is re-assigned in the function moveMarquee

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

    It was because of that 12px gap after each cycle.

    <!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>
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search