Centralizing JavaScript Background Video

How can I make my video centered on any screen size? I have the CSS in place to ensure that the video always covers the screen proportionally, but I need help with centering it at all times.

HTML

<div class="heroWrapper">
  <video autoplay muted loop class="videojsfix" id="myVideo">
    <source src="/video/crystal.mp4" type="video/mp4">
  </video>
</div>

JS:

var windowSize = window.innerWidth;
var VideoWidth = document.getElementById("myVideo").offsetWidth;
var difference = windowSize - VideoWidth;
var video = document.getElementById("myVideo");
var moveDifference = ((difference) / 2);

if (VideoWidth > windowSize) {
    $(".videojsfix").css({
        left: 'moveDifference'
    });
};

The goal is to calculate the overlap of the video and divide it by 2 to determine how much to adjust the left CSS property to center the video on the screen. Can anyone explain why this code isn't working as expected?

Answer №1

Please verify if you have correctly implemented the CSS in your code.

if( VideoWidth > windowSize){
    $(".videojsfix").css({
    left: 'moveDifference'
}); };

if( VideoWidth > windowSize){
    $(".videojsfix").css({
    left: 'moveDifference'+ 'px';
}); };

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

Troubleshooting: Issues with Mod_rewrite affecting CSS, JS, and Image functionality

Today marks my first time posting a query in this forum. I'm currently facing an issue with mod_rewrite. Below is the structure of my filesystem: site_new/stylesheets/layout.css site_new/javascript/somescript.js site_new/hallendetails.php ...

Building a navigation system with previous and next buttons by implementing JavaScript

I am currently working with AngularJS and I have data that is being received in the form of an array containing two objects. As a newcomer, I am still trying to figure this out. data[ { "something":"something1", "something":"something1", "something":"some ...

What is the most effective way to receive input in JScript for Windows applications?

Currently, I am developing a Windows application (.exe) using JavaScript that is meant to calculate the sum of two numbers. However, I am facing an issue with getting user input in JavaScript for the Windows platform. var a = /*insert code for obtaining ...

tips for fetching information from sources beyond observable patterns

Is there a way to retrieve the value of this.dealType outside of the this.trasactionService.transactionEvent$ subscription? I need to access the data and value of this.dealType elsewhere, any help would be appreciated. While it has a value inside the this ...

Displaying the user's username upon logging into a website is a simple yet effective

I have successfully developed a simple web page using PHP and HTML. However, I am facing an issue in displaying the username after login. Additionally, I would like to hide the login and signup sections after successful login. Can anyone provide assistance ...

Is it possible for the binding model of Mat-Checkbox to be optional or null?

Greetings everyone! I am a newcomer to the world of Angular, where I have successfully created a dynamic list of checkboxes. However, I have encountered a challenge when trying to bind selected values from an API to these checkboxes. <div *ngFor="let b ...

Exploring the inner structure of an STL Object with the power of three.js

Currently, I am attempting to create a cross section of a heart model that I have imported using the STLLoader function in three.js. To achieve this, I am experimenting with the ThreeCSG wrapper for the csg.js library, similar to the approach outlined in t ...

Is it possible to refine an HtmlNodeCollection by the content of a specific child element's InnerText?

I am working with an HtmlNodeCollection named var searchResults that I extracted from a website using HtmlAgilityPack. Below is a snippet showing 3 Nodes from the collection (there are more of course): /* HTML code snippet here */ My goal is to filter ou ...

Using Bootstrap 5 to beautifully wrap an even quantity of boxes

My challenge is to optimize the spacing between 4 boxes based on screen size: The layout should adjust as follows: If it's xxl or larger, all 4 boxes should be in a single horizontal row. If it's md or larger, two boxes should be in the firs ...

Center two lines of text in a div in a vertical fashion

After going through several articles on this topic, I've picked up some tricks for vertical alignment like using line-height for a single line of text, display:table for multiple divs, and Flexbox in CSS3. However, I'm still struggling to align t ...

Exploring the power of Foundation For Apps and Angular by seamlessly displaying dynamic data sourced from

I'm currently working with Foundation for Apps, a framework that incorporates elements of AngularJS. My goal is to display the content from a local JSON file by accessing it through a controller and rendering the results as 'cards'. Addition ...

Dynamically Allocating Page Heights

I am tasked with creating an ASP.NET MVC page that will display a template of controls. The controls will be retrieved from an XML file. One issue I am facing is that the number of controls is not fixed, so the page height needs to be dynamic. Our site has ...

Avoid showing a pop-up window when switching to a different page

I have a pop-up window that displays a confirmation message only when I am trying to close the browser/tab. It is working on my page, but it also appears when I navigate to another page. I want the pop-up window to only show up when I try to close the ta ...

Escaping the clutches of an iframe: A guide to breaking free

I have a dilemma with an iframe setup like this: <div id="frameDiv" style="clear: both; border: 1px solid gray;"> <iframe id="reportFrame" width="100%" frameborder="0" style="height: 800px; fit: fill; fit-position: fill; overflow-y: scroll; over ...

Are there any strategies to bypass the color limitations in Bootstrap 5?

Recently, I began studying bootstrap through a crash course video tutorial. The developer demonstrated using various predefined color classes like danger, warning, and primary. Curious, I wondered if it would be possible to substitute dark with tomato re ...

Image with fluid dynamics centered in a div - resize simultaneously with div

I'm working on a fully fluid web page with a parent DIV set to width: 100%. Inside this div, there's an image with the following style: <div> <img src="image.png" alt="" /> </div> <style> div { width: 100%; } ...

Utilize Wordpress TinyMCE to apply the underline decoration using the "U" tag instead of the style

Is there a way to modify the function of the button in the WordPress content textarea of TinyMCE? Specifically, I am referring to the "u" button for underline formatting. <span style="text-decoration-line: underline;">text underlined</span> I ...

Any suggestions on how to avoid code repetition using Jquery?

This code allows me to create a menu for my gallery. By setting the .masonry # elements to display:none, I can use the corresponding ID in the menu to show the correct gallery when clicked. However, there is a lot of repetitive code in this implementatio ...

Troubleshooting the ModelState.IsValid issue while editing user data and saving changes

Need to make updates to user details such as Firstname, Lastname, and date-of-birth. Even though the ModelState is valid, encountering issues with the SaveChanges function not working. It seems that there are some errors related to the password and email f ...

Show buttons in varying styles aligned next to each other

Here is the code snippet I'm currently working with: <fieldset class=last> <button>Refresh</button> <button> Clear</button> </fieldset> <form method="POST" action="*******"> <button> Down ...