I’m trying to load a different video based on whether or not the user is on desktop (landscape) or mobile (portrait).
The URL is www.bubina.com
If you load this on a mobile, it just loads the side of the video, but on desktop it’s fine. I have a separate, portrait version of the video that I need to load for mobile.
I’ve been trying to solve this with a number of fixes I found online: var javascript to call in different videos, using a media tag, etc, but I just can’t get it to work. I’d appreciate any help please.
You can have a look at what I’m doing here:
<style>
var mainVideo = $('video#LoadVideo');
var mobileSrc = "https://www.bubina.com/work/wp-content/uploads/2023/04/LMNT-for-homepage-mobile-with-text-480.mp4";
var desktopSrc = "https://www.bubina.com/work/wp-content/uploads/2023/04/LMNT-for-homepage-desktop-with-text-480.mp4";
if ($(window).width() < 980) {
mainVideo.append("<source type='video/mp4' src='"https://www.bubina.com/work/wp-content/uploads/2023/04/LMNT-for-homepage-mobile-with-text-480.mp4"'/>");
} else {
mainVideo.append("<source type='video/mp4' src='"https://www.bubina.com/work/wp-content/uploads/2023/04/LMNT-for-homepage-desktop-with-text-480.mp4"'/>");
}
</style>
</head>
<body>
<video id="LoadVideo">
</video>
</body>
I’ve also tried this:
<style>
#myVideo {
position: fixed;
right: 0;
bottom: 0;
min-width: 100%;
min-height: 100%;
}
</style>
</head>
<body>
<video autoplay muted loop id="myVideo">
<source src="https://www.bubina.com/work/wp-content/uploads/2023/04/LMNT-for-homepage-desktop-with-text-480.mp4" media="only screen and (min-device-width: 1080px)"></source>
<source src="https://www.bubina.com/work/wp-content/uploads/2023/04/LMNT-for-homepage-mobile-with-text-480.mp4" media="only screen and (max-device-width: 1080px)"></source>
</video>
</body>
Thanks in advance.
I tried to get a different video to load in desktop and mobile but the same one is loading on each device.
There is this question/answer: How to play 2 different videos based on screen size?
But the answer says the below:
if (window.innerWidth > 425) {
// display video for desktop
} else {
// display video for mobile
}
And I don’t understand what to put where it says "display video for desktop" and "display video for mobile". What sort of code goes there? Where do the URLs for the video go? I need more explanation than that – I’m really limited on javascript knowledge, but I do know CSS and HTML.
2
Answers
Your code has a few issues:
<script>
tag, not a<style>
tag. Also, you’re mixing up the quote you use for the string and the HTML attributes. You also didn’t include a<script>
tag for jQuery. I’ve fixed these typos below.< 425
to check for smaller screens. 980 pixels is too big.<source>
element, instead just create one upfront and change thesrc
attribute.Use Jquery to control what you want to load in the video part
For a better user’s experience
So the code is