Switching between videos can lead to an undesired bouncing effect on

Whenever I switch between my video thumbnails, there seems to be a slight bounce as the new video loads. To better understand this issue, check out this video: .

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="styles.css">
</head>
<body>
    <main>
        <div class="video-container">
            <div class="main-video">
                <video id="main-video-1" class="main-video-element" controls>
                    <source src="1.mp4" type="video/mp4">
                    Your browser does not support the video tag.
                </video>
                <video id="main-video-2" class="main-video-element hidden" controls>
                    <source src="grass.mp4" type="video/mp4">
                    Your browser does not support the video tag.
                </video>
                <video id="main-video-3" class="main-video-element hidden" controls>
                    <source src="sunset.mp4" type="video/mp4">
                    Your browser does not support the video tag.
                </video>
            </div>
            <div class="small-videos">
                <div class="video-item" data-video-id="main-video-2">
                    <img src="grass.png" alt="Grass video thumbnail">
                </div>
                <div class="video-item" data-video-id="main-video-3">
                    <img src="sunset.png" alt="Sunset video thumbnail">
                </div>
            </div>
        </div>
    </main>

    <div class="divider"></div>
    <footer>
        <p>&copy; 2024 My Name. All rights reserved.</p>
    </footer>

    <script src="scripts.js"></script>
</body>
</html>

CSS

body {
    font-family: Arial, sans-serif;
    margin: 0;
    padding: 0;
    box-sizing: border-box;
    background-color: #f4f4f4;
    color: #333;
}

main {
    padding: 2rem 0;
    text-align: center;
}

.video-container {
    display: flex;
    flex-direction: column;
    align-items: center;
    width: 100%;
}

.main-video {
    position: relative;
    width: 100%;
    max-width: 900px;
    height: auto;
    margin-bottom: 1rem;
}

.main-video video {
    width: 100%;
    transition: opacity 0.5s ease;
}

.main-video .hidden {
    opacity: 0;
    pointer-events: none;
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
}

.small-videos {
    display: flex;
    gap: 1rem;
    width: 80%;
    max-width: 900px;
    justify-content: space-between;
}

.small-videos .video-item {
    flex: 1;
    cursor: pointer;
}

.small-videos img {
    width: 100%;
}

.divider {
    width: 80%;
    max-width: 900px;
    height: 1px;
    background-color: #ddd;
    margin: 4rem auto 1rem;
}

footer {
    text-align: center;
    padding: 1rem 0;
}

footer p {
    margin: 0;
    color: #333;
}

@media (max-width: 768px) {
    .main-video {
        width: 100%;
    }

    .small-videos {
        flex-direction: column;
    }

    .small-videos .video-item {
        margin-bottom: 1rem;
    }

    .divider {
        width: 100%;
    }
}

JS:

document.addEventListener("DOMContentLoaded", function() {
    const mainVideos = document.querySelectorAll(".main-video-element");
    const smallVideos = document.querySelectorAll(".video-item");

    smallVideos.forEach(videoItem => {
        videoItem.addEventListener("click", function() {
            const newVideoId = this.getAttribute("data-video-id");
            const newVideoElement = document.getElementById(newVideoId);

            mainVideos.forEach(video => {
                if (video.id === newVideoId) {
                    video.classList.remove("hidden");
                    video.play();
                } else {
                    video.classList.add("hidden");
                    video.pause();
                }
            });
        });
    });
});

I am troubleshooting why the video boxes shift slightly when switching between thumbnails. If you have any insights, please let me know.

Answer №1

To prevent any bouncing in the UI caused by video loading, simply place each video element within a div that has set dimensions of width and height. I trust this solution will be beneficial to you ;)

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 a stylish horizontal divider with circular accents at either end

Can someone help me create a border similar to the one shown in this image? I've attempted using the :after and :before CSS attributes, but without success. The HTML element is an h1 that requires a border at the bottom. Is it achievable? ...

What is the best method to determine the accurate height of a window that works across all browsers and platforms?

Is there a way to accurately determine the visible height of the browser window, taking into consideration any floating navigation bars or bottom buttons that may interfere with the actual viewing area? For example, mobile browsers often have floating bar ...

Whenever I adjust the layout of the navigation bar, the edges end up getting clipped

I'm having trouble with the border shape of my navbar. When I try to make it a rounded pill shape, the edges get cut off instead of being properly displayed. https://i.stack.imgur.com/sUN2Y.png Below is the HTML template: <template> <div cl ...

Enhance the functionality of the kendo grid by incorporating additional buttons or links that appear when the

Is there a method to incorporate links or buttons when hovering over a row in a kendo grid? I've searched through the documentation and looked online, but haven't found a solution. I'm considering if I should modify my row template to displa ...

Circular css3 animation originating from the center

Can someone provide an example of how to create a div rotating around a circle in CSS3 while the circle itself is already rotating? I attempted to use webkit frame, but it did not work correctly. If anyone has any examples or tips on achieving this effec ...

Angular JS Profile Grid: Discover the power of Angular JS

I came across an interesting example that caught my attention: http://www.bootply.com/fzLrwL73pd This particular example utilizes the randomUser API to generate random user data and images. I would like to create something similar, but with manually ente ...

Pop-up box with hyperlink

I successfully integrated multiple modals into my webpage, using a template from w3school that I customized. Now, I am curious to see if it is possible for each modal to have its unique link. For example, when I open Modal 4, the URL would change to myweb ...

Adapt appearance according to the length of the text

Currently, I have an array that stores multiple strings based on displayed charts. My objective is to find the longest string within this array. So far, this task has been executed without any issues. The code snippet for this process is as follows: var ...

Center align Bootstrap 4 dropdown menu

I am attempting to center align my dropdown menu using Bootstrap 4. Following the given example: [ This is the result I am achieving: [ This is the code I have implemented: <div class="container"> <div class="row"> <div class=" ...

Troubleshooting a problem with dynamic options in Materialize select set

I'm currently facing an issue with my two dependent dropdowns, country and state. When I select a country, I use JavaScript to populate the respective states in the state dropdown. However, even though the options are added correctly when I inspect th ...

"Dynamic" visual in a Vue.js development scheme

Utilizing Vue.js for the development of a hybrid mobile application has been my current focus, with the Quasar framework serving as a key component. Recently, I incorporated an image into the application using the <img /> tag and utilized the followi ...

What is the process for adjusting the switch size in react-bootstrap?

I've been struggling to increase the size of the react-bootstrap switch without success. I searched through the documentation and tried various methods, but couldn't find a solution other than directly modifying the bootstrap css. Here's the ...

Using CSS to showcase div elements with varying dimensions

How can I arrange divs with varying heights to be positioned close together, regardless of their height, and with a specific margin similar to buildings? I am aiming for a layout like the one on this website . I tried using float:left but it only float ...

Text positioned beneath a toggle switch in html that overlaps

Is there a way to fix the issue of the label on the left side overlapping below the selector switch on my website? I want to ensure that both labels are perfectly distributed on each side of the switch. Here is the code snippet: <div class="ro ...

Learn how to dynamically switch the background image of a webpage using a button in AngularJS

Hey there, I'm currently working on incorporating interactive buttons into my website to give users the ability to customize the background. I've been experimenting with Angular to achieve this feature. So far, I've managed to change the ba ...

Aligning text in the middle of a div vertically

Visit the Satoshi Faucet Index here I'm attempting to vertically align the text within the four divs containing sat, bit, mBTC, and BTC. I have tried using display:table-cell; vertical-align:middle; on the divs but unfortunately it isn't havi ...

Exploring the process of web scraping from dynamic websites using C#

I am attempting to extract data from using HtmlAgilityPack. The website is dynamic in nature, displaying content after the page has fully loaded. Currently, my code retrieves the HTML of the loading bar using this method, but encounters a TargetInvocation ...

Applying CSS to both pop-up and desktop interfaces can be achieved through the use of media queries or alternative solutions

I am facing a dilemma where I need to display the same content on both a pop-up and desktop view. While I have already implemented media queries to adjust the content for desktop and mobile views, I am struggling with displaying the same content on a pop ...

Ensuring the positioning of input fields and buttons are in perfect alignment

I've been struggling to align two buttons next to an input field, but every time I try, I end up messing everything up. I managed to align an input field with a single button, but adding a second button is proving to be quite difficult. Here is ...

Display a single video on an HTML page in sequential order

My webpage contains a video tag for playing online videos. Instead of just one, I have utilized two video tags. However, when attempting to play both videos simultaneously, they end up playing at the same time. I am seeking a solution where if I start pla ...