Viewing 13 posts - 1 through 13 (of 13 total)
  • Author
    Posts
  • #1490768

    Hi,

    We created a website with a video on the homepage. To speed up loading, we created two videos, one for desktop and one smaller for mobile. Each video was placed in a color section with its own visibility settings (image). Everything looks fine visually, but we noticed the website was loading slowly, even though both videos were loading in the console (image). How can we make it so that only the desktop or mobile version of the video loads on each device?

    Thanks advance,

    BR,

    Antonio.

    #1490780

    Hey Antonio,
    The setting that you are referring to uses css to hide the color section after page load, PHP can not determine the screen size of the device, since it is a server side code language. CSS is a client side code language that loads on the user device, but will not stop the video from being served. Another option would be Javascript, such as in this thread, but it is also a client side code language, while it can stop the load of the video as long as the Javascript runs first, but using a caching plugin like WPRocket may derfure the Javascript and cause it to not work. Most caching plugins force Javascript to run in the footer, but this would need to run in the header before the videos.
    So ultimately you will be losing the render blocking of Javascript to gain the video from not loading, if your video is very large you will save on the page load, but otherwise you will see warnings that the Javascript is causing page render blocking.

    Best regards,
    Mike

    #1490781

    what you can do – ( not with enfold specific tools )!

    do not set the background-image with enfold color-section settings.
    put into the color-section on top a code-block element with this content
    (adjust to your path and videos):

    See Solution on next posts

    #1490792

    PS: Nice side effect: you could equip two sections on the same page (each loop) – you would then only have to place it in one color section:

        <video class="responsive-background-video" 
               autoplay muted loop playsinline
               data-video-mobile="hero-mobile.mp4"
               data-video-desktop="hero-desktop.mp4">
            <source src="" type="video/mp4">
        </video>
        <div class="content">Hero Content</div>

    and then in another section:

        <video class="responsive-background-video" 
               autoplay muted loop playsinline
               data-video-mobile="about-mobile.mp4"
               data-video-desktop="about-desktop.mp4">
            <source src="" type="video/mp4">
        </video>

    You must then add the corresponding data-video attributes to your video links.

    #1490800

    Edit: here is the solution with working separators on those color-sections

    the color-section that should react like this:
    give a custom class to it: av-video-section
    give a background-color to it that does not disturb your video switch (not white – something that fits to your video content
    place a codeblock element on top of your color-section ( codeblock to content – not as codesnippet)

    <video class="responsive-background-video" 
            autoplay muted loop playsinline
            data-video-mobile="path/to/mobile.mp4"
            data-video-desktop="path/to/desktop.mp4">
        <source src="" type="video/mp4">
    </video>
    

    this to your quick css:

    .responsive #top #wrap_all .avia-section.av-video-section {
      position: relative;
      overflow: hidden; 
      min-height: initial; /* taken from Enfold section setting  */
    }
    .responsive #top #wrap_all .avia-section.av-video-section .container {
      position: relative;
      z-index: 1;
      max-width: 100% !important;
      width: 100% !important;
      padding: 0 !important;
    }  
    .responsive #top #wrap_all .avia-section.av-video-section .responsive-background-video {
      position: absolute;
      top: 0;
      left: 0;
      width: 100%;
      height: 100%;
      object-fit: cover !important;
      z-index: 0;
      min-height: inherit; /* transfer from Enfold section setting  */
    }
    .responsive #top #wrap_all .avia-section.av-video-section .avia-divider-svg {
      z-index:2;
    }
    .responsive #top #wrap_all .avia-section.av-video-section .avia-divider-svg.avia-to-front {
      z-index:100;
    }

    the snippet for child-theme functions.php

    function custom_responsive_background_video_script() {
    ?>
    <script>
    (function($) {
        "use strict";
    
        $(document).ready(function() {
            $('.responsive-background-video').each(function() {
                const video = this;
                const $video = $(video);
                const source = video.querySelector('source');
                const win = $(window);
                
                // Get video paths from data attributes
                const mobileVideo = $video.data('video-mobile');
                const desktopVideo = $video.data('video-desktop');
                
                if (!mobileVideo || !desktopVideo) return;
                
                function updateVideoSource() {
                    const isMobile = win.width() < 768;
                    const newSrc = isMobile ? mobileVideo : desktopVideo;
                    
                    if (source.src.indexOf(newSrc) === -1) {
                        source.src = newSrc;
                        video.load();
                    }
                }
                
                // Initial load
                updateVideoSource();
                
                // Resize handling
                win.on('debouncedresize', updateVideoSource);
            });
        });
    })(jQuery);
    </script>
    <?php
    }
    add_action('wp_footer', 'custom_responsive_background_video_script', 999);

    see example page with divider : https://webers-testseite.de/background-video/

    #1490881

    Hi Mike and Guenni,

    I have try and looks good.
    The only issue is the vídeo is not in the background. I have create a test page no index because the website is on air.
    Many thanks!

    BR,

    Antonio.

    #1490888

    have you followed my link to the testpage? There is the video in the “background!” you can see the layout of that in the placed image below.
    The rest is the css you need.
    PS: Hope Mike could help you – because as a participant i can not see private content area.

    #1490937

    Hi Guenni,

    Sorry I repeated the process and it worked perfectly.
    We’ve tested the new page, but it still takes a long time to load. We’ve installed WProcket, but the page still seems very slow.
    What could we do to improve the loading time?
    Thanks advance,

    BR,

    Antonio

    #1490954

    Hi,
    When I check WPRocket is lazing loading the video until user inter action, try disabling  WPRocket to check, then try adding an expection for it to WPRocket if it helps.
    What I see is the video doen’t load until the mouse is over the background video, the faster the mouse is moved the faster the video loads.

    Best regards,
    Mike

    #1490955

    Hi,
    Please also keep in mind my previous post about how caching plugins work with javascript and the limitations with screen sizes.

    Best regards,
    Mike

    #1490969

    Are these self-hosted videos? How large are the files?

    #1490993

    Many thanks Make and Guenni,

    Yes, yes the delay comes from the wpRocket Js. Now works perfect!

    BR,

    Antonio.

    #1491018

    Hi,

    Thanks for the update. Please let us know if you should need any further help on the topic, or if we can close it.

    Best regards,
    Rikard

Viewing 13 posts - 1 through 13 (of 13 total)
  • You must be logged in to reply to this topic.