How can jQuery adjust the video dimensions for various devices?

I'm trying to display a video inside a div using jQuery, but the issue I'm facing is that I want the video to adapt and fill the div based on different screen sizes.

Below is my code:

<script type="text/javascript">
    $(document).ready(function(){
        $('#viddiv').append('<video src="1.mp4"  type="video/mp4" loop="loop" autoplay="autoplay" </video>');
    });
</script>
</head>
<body>
<div data-role="page">
<div data-role="header">
    <h1>My Title</h1>
</div>
<div data-role="content">   
    <div id="viddiv">
    </div>
</div>
</div>

</body>
</html>

And my CSS:

@charset "UTF-8";
video {
    height:100%;
    width:100%;
}

@media only screen and (max-width: 1024px) {
    #viddiv {
        height: 768px;
        width: 1024px;
        background-color:#FF0;
    }    
}
@media only screen and (max-width: 768px) {

   #viddiv {
        height: 1024px;
        width: 768px;
        background-color:#00F;
    }
}
@media only screen and (max-width: 640px) {

   #viddiv {
        height: 480px;
        width: 640px;
        background-color:#0F0;
    }
}
@media only screen and (max-width: 320px) {

    #viddiv {
        height: 480px;
        width: 640px;
        background-color:#F00;
    }
}

I'm struggling with this and it seems like it should be an easy fix. Any help would be greatly appreciated!

Answer №1

If you want to achieve this effect, consider the following approach:

    $('#viddiv').append('<video src="1.mp4"  type="video/mp4" loop="loop" autoplay="autoplay" </video>')
.css({"width":screen.width, "height":screen.height});

View example on jsfiddle

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

Cannot conceal a text input field

I'm facing an issue with masking a text field that is generated after receiving an Ajax response. The Ajax request and response are working fine. I'm using the jQuery maskedinput plugin to achieve this. I've included all the necessary JS fil ...

<button class="fadeButton">Click me to fade in and out</

Hi everyone! I'm currently working on my website as a fun project and I have a code that displays a list of shapes. However, I'm looking to replace the fadeOut feature with a new button that, when clicked, will hide the shapes. The current code a ...

Using Jquery's click function is unable to display the information stored in the database

I'm encountering an issue with my Jquery onclick event in my form. I'm unable to display data from the database. For example, when I choose a category like "heart", the doctors associated with that category should be displayed. However, if I use ...

Could someone provide an explanation for this Jquery?

$(document).ready(function () { if ((screen.width >= 1024) && (screen.height >= 768)) { alert('Screen size: 1024x768 or larger'); $("link[rel=stylesheet]:not(:first)").attr({ href: "style2.css" ...

Verify whether the element in the DOM is a checkbox

What is the method to determine whether a specific DOM element is a checkbox? Situation: In a collection of dynamically assigned textboxes and checkboxes, I am unable to differentiate between them based on their type. ...

What is the best way to send JSON data from a function within an AMD module?

Why do I keep receiving "undefined" when trying to check for the existence of my json object? AFTER REVISION: After thoroughly reading the post referenced here: How do I return the response from an asynchronous call?, I came across a valuable insight. Tow ...

Picture cramped within a flexbox

I have a div containing an image that is larger than the screen, and I've set the div to be full width with overflow hidden. It works perfectly without creating scrollbars. However, when I try to use flexbox on this div, the image gets squeezed. Inter ...

Switching icon with jQuery upon clicking

I'm just starting to learn jQuery and I'm working on changing the font icon class when clicked to display a drop down. Right now, it changes the icon and drops down the content as expected. However, I'm facing an issue where I want the icon ...

Center the title vertically APEXCHARTS

I created this chart using Apex charts https://i.sstatic.net/j34Wc.png However, I am facing an issue where the 'Chart' title and caption are not properly aligned. The title should align with the graph horizontally, and the caption vertically. ...

Switching from a top to bottom position can be quite the challenge for many

If there's a more efficient method for accomplishing what I'm about to inquire about, please inform me. So far, this is the best solution I could devise. I am looking to have a series of divs that enclose a sliding div within them. The sliding d ...

How to prevent multiple JQuery mouseenter events from firing when divs are overlapping

Currently, I am using the JQuery mouseenter event to display a smaller DIV B inside another DIV A. However, I am facing an issue where when I hover over DIV B, the mouseenter event gets triggered again. What I actually want is for DIV B to be shown and hav ...

Changing color of entire SVG image: a step-by-step guide

Check out this SVG image I found: https://jsfiddle.net/hey0qvgk/3/ <?xml version="1.0" encoding="utf-8"?> <!-- Generator: Adobe Illustrator 19.1.0, SVG Export Plug-In . SVG Version: 6.00 Build 0) --> <svg version="1.1" width="90" height="9 ...

Pass information from ColdFusion to jQuery

I'm attempting to achieve a similar result as this, but I believe the syntax is not quite right: <cfset details = '{ name: "name", address:"address"}' /> <img data-details='#details#' onClick="DisplayDetails()" /> &l ...

How can JavaScript be used to deactivate an HTML form element?

Within this form, there are two selection buttons that require client-side validation. The user must choose one of them, and once a selection is made, the other button should automatically be disabled. Below is the script I have implemented: <html> ...

Unable to retrieve data from file input using jQuery due to undefined property "get(0).files"

I am facing an issue with retrieving file data in jQuery AJAX call from a file input on an ASP.NET view page when clicking a button. HTML <table> <td style="text-align:left"> <input type="file" id="AttachmenteUploadFile" name="Attachme ...

There is a noticeable gap at the top of the scroll area in my Bootstrap modal

Hello everyone, I have encountered an issue with the modals on my website where the scroll area does not fit the page properly. Please refer to the screenshot below for reference. I have not applied any extra styles to the modals and I am unable to pinpo ...

Performing operations on an HTML string using jQuery

I have a dynamically generated HTML snippet (a collection of table rows). The number of rows in this snippet can vary, it may be more than one. string myHtml = " <tr> <td class="text-left"> <labe ...

Adjusting the transparency of numerous elements using JavaScript or jQuery

My task involves creating a large form where elements initially have an opacity of 0.5, and when a button is clicked, the opacity changes to 1.0. While I can achieve this using JavaScript, I want to find a more efficient way by avoiding individual if state ...

Position the icon to the left side of the button using Bootstrap

Need help with centering text on a bootstrap 3 button while aligning the font awesome icon to the left side. <button type="button" class="btn btn-default btn-lg btn-block"> <i class="fa fa-home pull-left"></i> Home </button> Usi ...

Using jQuery to enhance an HTML form

Currently, I am in the process of creating an HTML form for user sign up which includes typical fields such as username, password, and email. My intention is to transfer the entire HTML form to another .php file using the POST method. The second .php file ...