Determine the height of a video using jQuery before it completes loading

I have a video that's 8MB in size set to "object-fit:contain" and I need to find its height before it finishes loading on mobile devices. The reason for this is so that I can position content overlaying the video by matching the video's height with the content container.

What I'm trying to achieve is not the actual height of the video, but rather the height as it appears in the user's browser/viewport.

When I attempt to retrieve the height while the video is still loading, I get a much lower value (around 200 pixels) compared to what it actually ends up being (usually around 600 pixels). Although I've managed to get the correct height after the video has loaded using the load() function, this method is quite slow due to the large file size of my video.

Is there any workaround available for this issue?

jQuery(document).ready(function($){
    function vidHeight() {
        var newHeight = jQuery('#section-feature-device video').height();
        jQuery(".elementor-element-0113321").css("height", newHeight+"px");
    }
    jQuery(window).load(function () {
        var win = jQuery(this);
        if (win.width() <= 767) {
            vidHeight();
        }
    });
});

Answer №1

To determine the intrinsic height of a video, you can utilize the event listener known as loadedmetadata.

document.getElementById('video').addEventListener('loadedmetadata', (e) => console.log(e.target.videoHeight));
<video controls="true" poster="" id="video">
    <source type="video/mp4" src="http://www.example.com/video.mp4">
</video>

Answer №2

After some research, I managed to determine the height of a video based on its width being 100% by calculating the viewport width multiplied by the video's aspect ratio.

jQuery(document).ready(function($){
    function calculateVideoHeight() {  
        var viewportWidth = jQuery(window).width();
        var videoHeight = viewportWidth * 0.5625;
        jQuery("#section-feature").css("height", videoHeight+"px");
        //console.log("Video height is "+videoHeight)
    }   
    calculateVideoHeight();
    jQuery(window).on('resize', calculateVideoHeight);
});

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

Personalizing the height and width of a jQuery UI Slider

While adjusting the height/width of the jQuery UI Slider and handle, I've noticed that the handle sometimes slides too far to the left, going off the slider. Is there a recommended way to prevent this from happening? You can see what I mean by checki ...

Choosing between Ajax.ActionLink and combining Html.ActionLink with a Jquery.Ajax call

One way to call an asp.net mvc controller is through Ajax.ActionLink("Get customers","GetCustomers","Customer"); Another method involves using Html.ActionLink and a jQuery ajax call. What sets these two approaches apart? ...

Can JavaScript be injected and run after the page has already loaded?

Imagine having a JavaScript function that is dynamically inserted into the page after it has loaded. How can you then call this function? To simplify, let's consider making an Ajax call to retrieve the script markup with the following content: <sc ...

Utilizing Three.js to instantiate a variety of objects and interact with each one separately

Recently, I managed to create a cube in 'three.js', and now I'm looking to replicate that cube three more times and animate them individually using 'Tweenmax'. As a newcomer to three.js, I would greatly appreciate any guidance or ...

What is the process for changing the output paper size to A4 in React Native (expo android)?

Using React Native to develop an Android app for billing purposes, I encountered an issue with the output paper size being 216mmX279mm instead of the standard PDF size of 210mmX297mm. Utilizing expo-print and printToFileAsync from expo, I aim to achieve a ...

Refreshing the current variables within a JavaScript array

When I add variables to an array in JavaScript and then update each element, why do the variables all show the same value when outputting them in the console? In this example, I would anticipate seeing 777 for all variables in the console, but instead the ...

Is it possible to choose several classes with identical names and then trigger a shared function simultaneously?

Is there a way to make this function target all elements with the class ".stop" and stop the video when any of these elements are clicked? window.addEventListener("load", function(event) { window.addEventListener('scroll', checkScroll, false) ...

Advantages and disadvantages of content loaded via AJAX

Isn't it convenient to organize a dataset using various filters and see the results right away? One way I would approach this is by sending a POST request with the filter parameters to a page called dataset.php. This page would then return the filte ...

Is there a way to transmit a div using Node.js?

Scenario: I am developing a web application that allows users to draw on a canvas, save the drawing as an image, and share it with others using Node.js. Current Progress: Drawing functionality (real-time updates on both clients). Saving the canvas as a ...

What is the most efficient way to use jQuery to load a local file with the file:// protocol?

Is it possible to retrieve data from a data file using jQuery, such as a JSON .js file? For example: $.get("file:///C:/objectData.js", function() { alert('Load was performed.'); }); Currently, instead of a regular HTTP GET request, jQuery seem ...

What are some strategies to prevent redundancy in CSS when only altering a single element?

Is there a way to avoid repeating the elements in my code and only change the "rotate" value that interests me? I heard about using "Counter-increment" online but I'm not sure how to implement it in my code. I am using vuejs to prevent repeating 30 ...

Enforcing Height Constraints on Images in HTML/CSS

I'm currently working on a design project and encountering a minor issue. Feel free to take a look at the final design example linked here: In the menu on both sides, you'll notice a small border that runs from top to bottom, matching the heigh ...

Tips for accessing user input in JavaScript functions

In my ASP.NET code, I have created a dynamic image button and panel. Here is the code: Panel panBlocks = new Panel(); panBlocks.ID = "PanBlockQuestionID" + recordcount.ToString(); panBlocks.Width = 1300; panBlocks.Height = 50; panBlocks.BackColor = Color. ...

All pictures aligned with overflow

The first container is working fine, displaying 2 images vertically, but when there are 6 images, it overflows. The problem lies with container 2, where I want to create a horizontal list of images with overflow (only horizontal). This is the current sta ...

Having trouble with applying a class in Gtk3 css?

MY ATTEMPT AND EXPLANATION: In my application, I have the following layout structure: GtkWindow GtkOverlay GtkBox GtkGrid GtkButton Even after trying to apply this CSS style: GtkButton{ background-color:blue; } T ...

CriOS unable to recognize OPTIONS request from Tomcat 8

My application uses POST requests with CORS for backend services (from www.mydomain.com to api.mydomain.com). The backend is served by a Tomact8 server, implementing a CORSResponseFilter as shown below: public class CORSResponseFilter implements Container ...

Get rid of the title on Superfish Menu in Drupal 7

Exploring Drupal for the first time, I've been able to find solutions to all my queries except one - removing the "Main Menu" header from my superfish menu. The region called superfish has successfully had superfish added to it, but I'm unable t ...

Manage the number of items in each column using flexbox or grid layout strategy

Here's a similar situation .items { width: 100%; display: flex; align-items: center; justify-content: flex-start; flex-wrap: wrap; background: #cecece; padding: 10px; margin: 20px; } .item { height: 100%; flex: 0 0 calc(50% ...

Find the vertices located on the "upper surfaces" of the ring using Three.js

I'm currently immersed in a project centered around creating different types of "rings" using Three.js. Each ring is formed through an algorithm I designed myself, which involves defining 4 sections placed at π, π/2, 3π/2, and 2π, then utilizing q ...

Issues with Radio Button Functionality in Bootstrap 5 when Integrated with JS Validation

Here is the JavaScript code I created: let radioChange = document.getElementsByName("pain-location"); let headPain = document.getElementById("headPain"); let shoulderPain = document.getElementById("shoulderPain"); let backPain = document.getElementById("ba ...