Viewing 9 posts - 1 through 9 (of 9 total)
  • Author
    Posts
  • #1407774

    Dear Support Team:

    As part of my responsive images work, I am trying to replace a banner-like image on my home page, that spans the entire browser window width, with a smaller image on smaller windows/screens. I have an Image inside a Color Section at the top of the home page (under the header) that I inserted from my Media Library (that includes the alt & title text). Initially I tried with CSS (couldn’t get it to include alt & title text on the images), and now with Javascript (can’t get it to work at all).

    I’ve tried specifically in (i) Firefox in Windows 7, (ii) Safari, Firefox, & Chrome on a Mac desktop, and (iii) Safari on an iPhone: the smaller banner does not load (and the larger banner displays only because it’s in the Color Section).

    So, as I wrote, initially I tried with CSS:

    @media only screen and (min-width:965px) {
        .homeBannerIMG {
            content: url(https://...banner1.jpg);
        }
        .homeBannerIMG::before { /* For Firefox issues */
            content: url(https://...banner1.jpg);
        }
    }
    
    @media only screen and (max-width:964px) {
        .homeBannerIMG {
            content: url(https://...banner2.jpg); /* Concatenating '/ "test alt"' to the end doesn't work */
        }
        .homeBannerIMG::before{
            content: url(https://...banner2.jpg);
        }
            /* Note: https://developer.mozilla.org/en-US/docs/Web/CSS/content says this works:
                content: url("http://www.example.com/test.png") / "This is the alt text";
            BUT IT DOESN'T FOR ME!. Plus I want the image Title as well for the hover tooltip. */

    But I wasn’t able to maintain my alt & title text on the image. So I switched to Javascript (in my child functions.php file), first trying:

    function displayHomeBanner() {
        if (window.matchMedia("(min-width: 965px)").matches) {
            document.getElementById("homeBannerIMG").src="https://...banner1.jpg";
        } else {
            document.getElementById("homeBannerIMG").src="https://...banner2.jpg";
        }
    }

    But that didn’t work: when I made my browser window below 965px wide, banner2 was not swapped in; it stayed as banner1. Then I tried:

    document.getElementById("homeBannerIMG").getElementsByTagName('img')[0].src="https://...banner2.jpg";

    But that didn’t work. Then I tried:

    document.getElementsByClassName("homeBannerIMG")[0].src="https://...banner2.jpg";

    Still no luck. I just can’t get banner2 to display.

    Note that my full CURRENT code in my child function.php file is:

    function windowSizeActions() {
        ?>
        <script>
            window.onresize = actOnWindowSize;
            window.onload = actOnWindowSize;
            
            function actOnWindowSize() {
                
                function displayWindowSize() {
                    myWidth = window.innerWidth;
                    myHeight = window.innerHeight;
                    document.getElementById("dimensions").innerHTML = " >>> " + myWidth + " x " + myHeight;
                }
            
                function displayHomeBanner() {
                    if (window.matchMedia("(min-width: 965px)").matches) {
                        /*document.getElementById("homeBannerIMG").src="https://...banner1.jpg";*/
                        document.getElementsByClassName("homeBannerIMG")[0].src="https://...banner1.jpg";
                    } else {
                        /*document.getElementById("homeBannerIMG").src="https://...banner2.jpg";*/
                        /*document.getElementById("homeBannerIMG").getElementsByTagName('img')[0].src="https://...banner2.jpg";*/ /* Based on https://kriesi.at/support/topic/using-javascript-to-set-image-src-using-developer-id/: document.getElementById(‘work_image’).getElementsByTagName(‘img’)[0].src = work_image; */
                        document.getElementsByClassName("homeBannerIMG")[0].src="https://...banner2.jpg";
                    }
                }
                displayWindowSize();
                displayHomeBanner();
            }
            
        </script>
        <?php
    }
    add_action( 'wp_enqueue_scripts', 'windowSizeActions' );

    First of all, as you can see, I’ve commented out my earlier attempts at loading the src, with just my last attempt with getElementsByClassName live.

    Secondly, I have my displayHomeBanner function embedded in the function actOnWindowSize, and that in an overall function windowSizeActions, because I need debug code to display the window size, done by my displayWindowSize function (and another function elsewhere to set up the HTML). That function works: my window size is displayed at the top of my window constantly, and changes as I change the browser window size. So this also ensures that things are working, except displayHomeBanner is not working.

    Also note that I have tried my code outside of WordPress/Enfold/Aria, using any w3schools.com‘s page’s Try it Yourself button:

    <!DOCTYPE html>
    <html>
    <body>
    
    <div>
      <img id="homeBannerIMG" src="https://...banner1.jpg" alt="test alt" title="test title">
    </div>
    
    <script>
      window.onresize = actOnWindowSize;
      window.onload = actOnWindowSize;
            
      function actOnWindowSize() {
                
        function displayWindowSize() { /* This of course won't work here, but it's irrevelent. */
          myWidth = window.innerWidth;
          myHeight = window.innerHeight;
          document.getElementById("dimensions").innerHTML = " >>> " + myWidth + " x " + myHeight;
        }
            
        function displayHomeBanner() {
          if (window.matchMedia("(min-width: 965px)").matches) {
            document.getElementById("homeBannerIMG").src="https://...banner1.jpg";
          } else {
            document.getElementById("homeBannerIMG").src="https://...banner2.jpg";
          }
        }
                
        displayHomeBanner();
      }
    </script>
    
    </body>
    </html>

    and

    <!DOCTYPE html>
    <html>
    <body>
    
    <div>
      <img class="homeBannerIMG" src="https://...banner1.jpg" alt="test alt" title="test title">
    </div>
    
    <script>
      window.onresize = actOnWindowSize;
      window.onload = actOnWindowSize;
            
      function actOnWindowSize() {
                
        function displayWindowSize() { /* This of course won't work here, but it's irrevelent. */
          myWidth = window.innerWidth;
          myHeight = window.innerHeight;
          document.getElementById("dimensions").innerHTML = " >>> " + myWidth + " x " + myHeight;
        }
            
        function displayHomeBanner() {
          if (window.matchMedia("(min-width: 965px)").matches) {
            document.getElementsByClassName("homeBannerIMG")[0].src="https://...banner1.jpg";
          } else {
            document.getElementsByClassName("homeBannerIMG")[0].src="https://...banner2.jpg";
          }
        }
                
        displayHomeBanner();
      }
            
    </script>
    
    </body>
    </html>

    In both, my displayHomeBanner function works: my banner2 displays on smaller window sizes (and the title tooltip works)!

    Can you help me get it to work in WordPress please.

    Thank you,
    Gary

    • This topic was modified 1 year, 6 months ago by garysch37.
    • This topic was modified 1 year, 6 months ago by garysch37.
    #1407781

    Hey Gary,
    Thanks for the link to your site, the issue that I found was that your code was looking for the image scr at the custom class homeBannerIMG, but the structure is actually homeBannerIMG div div img
    so I changed your code from: document.getElementsByClassName(“homeBannerIMG”)[0].src=”https://www&#8230;
    to: document.querySelector(“.homeBannerIMG img”).src=”https://www&#8230;
    and this now works, please check.

    Best regards,
    Mike

    #1407975

    Thanks for helping me, Mike. But it doesn’t seem to have done the trick. It still doesn’t swap out the image on any of the browsers I’ve tried (with me first clearing all cookies & cache for the website), including Firefox in Windows 7; Safari, Firefox, and Chrome on a Mac desktop; and Safari on my iPhone.

    All the best, Gary

    #1407988

    Hi,
    I checked the source code (DOM) in Windows in Chrome, Firefox, & Edge and I can see the image change from banner1.jpg to banner2.jpg in Dev Tools, please see the screenshot in the Private Content area.

    Best regards,
    Mike

    #1408002

    Hi Mike!

    That is so bizarre, because here’s the HTML in Chrome’s inspector for the image’s element (which is also seen in your screen shot in my private area):

    <img decoding="async" class="wp-image-412 avia-img-lazy-loading-not-412 avia_image " src="...banner2.jpg" alt="..." title="..." height="831" width="1830" itemprop="thumbnailUrl" srcset="...banner1.jpg 1830w, ...banner1-300x136.jpg 300w, ...banner1-1030x468.jpg 1030w, ...banner1-768x349.jpg 768w, ...banner1-1536x697.jpg 1536w, ...banner1-1500x681.jpg 1500w, ...banner1-705x320.jpg 705w" sizes="(max-width: 1830px) 100vw, 1830px">

    As you can see, the src has banner2, but the srcset contains nothing but banner1, and it’s banner1 that is displayed, because banner2 contains just the part of the image with the senior couple, not the graphics background. On smaller screens I want to maximize displaying the couple alone, without have to shrink them to include the background.

    Do you know how to get banner2’s srcset in there so it renders properly?

    Now, I have been experimenting with doing this, arguably, the correct way: setting the Color Section (class seniorCoupleContainer) to the background graphic image itself using CSS (which works better anyway because I want it to span the entire browser’s width), and inserting an Image media element (class seniorCouple) into the Color Section containing just the happy couple–instead of using a graphics program to combine them as a single image for bigger screens, and swapping the image of the couple with background with just the image of the couple alone on smaller screens.

    .seniorCoupleContainer {
        background-image: url(...home-page-banner-background.png);
        background-size: 2881px 601px;
    }
    .seniorCouple {
        top: -35px;
    }

    You can see this in a second Color Section I created on the home page below the original banner at the top. With and without the background image, it all works well responsively too:

    @media only screen and (max-width:964px) {
        .seniorCoupleContainer {
            background-image: none;
        }
    }

    However, because there may be other situations I need to use Javascript to swap images, can you please help me solve my original probem?

    Thanks,
    Gary

    • This reply was modified 1 year, 6 months ago by garysch37.
    • This reply was modified 1 year, 6 months ago by garysch37.
    • This reply was modified 1 year, 6 months ago by garysch37.
    • This reply was modified 1 year, 6 months ago by garysch37.
    #1408007

    Just one more thing, Mike. Can you please tell me why there is that blue stripe underneath my new experimental Color Section that also shrinks responsively as I decrease the browser window’s width, and then disappears around 1005px wide–before the responsive 964px width. It’s not part of the original background graphic, but it also doesn’t show if I don’t display the background image on the wider screens. Thanks, Gary

    • This reply was modified 1 year, 6 months ago by garysch37.
    • This reply was modified 1 year, 6 months ago by garysch37.
    #1408015

    Hi,
    Thanks for the feedback, I originally didn’t notice the difference between the two images. I added a line to your javascript to also change the srcset image, like this:

    window.onresize = actOnWindowSize;
    window.onload = actOnWindowSize;
    
    function actOnWindowSize() {
      function displayHomeBanner() {
          if (window.matchMedia("(min-width: 965px)").matches) {
            document.querySelector(".homeBannerIMG img").src="banner1.jpg";
            document.querySelector(".homeBannerIMG img").srcset="banner1.jpg";
          } else {
            document.querySelector(".homeBannerIMG img").src="banner2.jpg";
            document.querySelector(".homeBannerIMG img").srcset="banner2.jpg";
          }
      }
    displayHomeBanner();
    }

    Please clear your browser cache and check.

    Best regards,
    Mike

    #1408049

    Thank you very much, Mike. That did the trick: I didn’t realize I could access the srcset field in img like that, doh! So now I know how to do that, for the situations I need it.

    And the situation came up immediately, as I ended up changing my method of doing this home page banner, to use the second method I mentioned, putting my background graphic into the Color Section, and my happy couple image in an Image media element in that Color Section, and then making it all responsive, including using your querySelector with src & srcset to swap out the happy couple images responsively at certain breakpoints, and using it again with backgroundImage to swap out my background graphic at the same breakpoints. Yes, it may seem weird that I’m swapping images manually instead of using srcset, but it was all necessary at these breakpoints (or I’m just not experienced enough to have figured it out). It all worked out nicely.

    I even answered my own question about that blue bar under the banner: I totally forgot that WordPress makes background images default to repeating, so I just had to turn repeat off.

    Thanks again for all your help! You can close this thread if you wish. /gary

    Here’s the final code in case others are interested:

    In child theme’s function.php:

    function displayHomeBanner() {
        
       if (window.matchMedia("(min-width: 1px) and (max-width: 579px)").matches) {
            // BG GRAPHIC:
            document.querySelector(".seniorCoupleContainer").style.backgroundImage="none";
            // COUPLES PIC:
            document.querySelector(".seniorCouple img").src="...happy-senior-couple-5-556x313px.png";
            document.querySelector(".seniorCouple img").srcset="...happy-senior-couple-5-556x313px.png";
        }
        
        if (window.matchMedia("(min-width: 580px) and (max-width: 819px)").matches) {
            // BG GRAPHIC:
            document.querySelector(".seniorCoupleContainer").style.backgroundImage="url('...home-page-banner-background-4-1079x300px.png')";
            document.querySelector(".seniorCoupleContainer").style.backgroundSize="1079px 300px";
            document.querySelector(".seniorCoupleContainer").style.backgroundRepeat = "no-repeat";
            // COUPLES PIC:
            document.querySelector(".seniorCouple img").src="...happy-senior-couple-4-510x287px.png";
            document.querySelector(".seniorCouple img").srcset="...happy-senior-couple-4-510x287px.png";
        }
        
        if (window.matchMedia("(min-width: 820px) and (max-width: 989px)").matches) {
            // BG GRAPHIC:
            document.querySelector(".seniorCoupleContainer").style.backgroundImage="url('...home-page-banner-background-3-1439x400px.png')";
            document.querySelector(".seniorCoupleContainer").style.backgroundSize="1439px 400px";
            document.querySelector(".seniorCoupleContainer").style.backgroundRepeat = "no-repeat";
            // COUPLES PIC:
            document.querySelector(".seniorCouple img").src="...happy-senior-couple-3-700x394px.png";
            document.querySelector(".seniorCouple img").srcset="...happy-senior-couple-3-700x394px.png";
        }
        
        if (window.matchMedia("(min-width: 990px) and (max-width: 1199px)").matches) {
            // BG GRAPHIC:
            document.querySelector(".seniorCoupleContainer").style.backgroundImage="url('...home-page-banner-background-2-1778x500px.png')";
            document.querySelector(".seniorCoupleContainer").style.backgroundSize="1778px 500px";
            document.querySelector(".seniorCoupleContainer").style.backgroundRepeat = "no-repeat";
            // COUPLES PIC:
            document.querySelector(".seniorCouple img").src="...happy-senior-couple-2-871x490px.png";
            document.querySelector(".seniorCouple img").srcset="...happy-senior-couple-2-871x490px.png";
        }
        
        if (window.matchMedia("(min-width: 2000px)").matches) {
            // BG GRAPHIC:
            document.querySelector(".seniorCoupleContainer").style.backgroundImage="url('...home-page-banner-background.png')";
            document.querySelector(".seniorCoupleContainer").style.backgroundRepeat = "no-repeat";
            document.querySelector(".seniorCoupleContainer").style.backgroundSize="2881px 601px";
            // COUPLES PIC:
            document.querySelector(".seniorCouple img").src="...happy-senior-couple.png";
            document.querySelector(".seniorCouple img").srcset="...happy-senior-couple.png";
        }
    }

    In child theme’s style.css:

    .seniorCoupleContainer {
        background-image: url(https://www.clearlightcomputing.com/denture6/wp-content/uploads/2023/05/home-page-banner-background.png);
        background-repeat: no-repeat;
        background-size: 2881px 601px;
        max-height: 591px; /* Without this there is an inexplicable bottom padding (zeroing margin and padding does nothing): height of senior pic 576 + reset top 35 = 611, but this still leaves a 20px bottom padding. */
    }
    .seniorCouple {
        top: -35px;
    }
    
    /* Get rid of the bottom padding at all these breakpoints because it accummulates between the breakpoints, so this code sort of does a reset of 0 padding: */
    @media only screen and (min-width: 1px) and (max-width: 349px) {
        .seniorCoupleContainer {
            max-height: 171px;
        }
    }
    @media only screen and (min-width: 350px) and (max-width: 369px) {
        .seniorCoupleContainer {
            max-height: 185px;
        }
    }
    @media only screen and (min-width: 370px) and (max-width: 389px) {
        .seniorCoupleContainer {
            max-height: 199px;
        }
    }
    @media only screen and (min-width: 390px) and (max-width: 409px) {
        .seniorCoupleContainer {
            max-height: 213px;
        }
    }
    @media only screen and (min-width: 410px) and (max-width: 429px) {
        .seniorCoupleContainer {
            max-height: 227px;
        }
    }
    @media only screen and (min-width: 430px) and (max-width: 449px) {
        .seniorCoupleContainer {
            max-height: 241px;
        }
    }
    @media only screen and (min-width: 450px) and (max-width: 469px) {
        .seniorCoupleContainer {
            max-height: 255px;
        }
    }
    @media only screen and (min-width: 470px) and (max-width: 489px) {
        .seniorCoupleContainer {
            max-height: 267px;
        }
    }
    @media only screen and (min-width: 490px) and (max-width: 519px) {
        .seniorCoupleContainer {
            max-height: 284px;
        }
    }
    @media only screen and (min-width: 520px) and (max-width: 549px) {
        .seniorCoupleContainer {
            max-height: 299px;
        }
    }
    @media only screen and (min-width: 550px) and (max-width: 579px) {
        .seniorCoupleContainer {
            max-height: 313px;
        }
    }
    
    @media only screen and (min-width: 1px) and (max-width: 579px) {
        .seniorCouple {
            top: -50px;
            width: 564px; /* without this it's smaller for some reason */
        }
    }
    @media only screen and (min-width: 580px) and (max-width: 819px) {
        .seniorCoupleContainer {
            max-height: 302px; /* Get rid of bottom padding. */
        }
        .seniorCouple {
            width: 510px; /* without this it's smaller for some reason */
        }
    }
    @media only screen and (min-width: 820px) and (max-width: 989px) {
        .seniorCoupleContainer {
            max-height: 409px; /* Get rid of bottom padding. */
        }
        .seniorCouple {
            width: 700px; /* without this it's smaller for some reason */
        }
    }
    @media only screen and (min-width: 990px) and (max-width: 1199px) {
        .seniorCoupleContainer {
            max-height: 505px; /* Get rid of bottom padding. */
        }
        .seniorCouple {
            width: 871px; /* without this it's smaller for some reason */
        }
    }
    @media only screen and (min-width:2000px) {
        .seniorCoupleContainer {
            width: 100% !important;
            height: 591px;
            max-height: 591px; /* Get rid of bottom padding. */
        }
        .seniorCouple {
            width: 1024px;
        }
    }
    • This reply was modified 1 year, 6 months ago by garysch37.
    • This reply was modified 1 year, 6 months ago by garysch37.
    #1408054

    Hi,
    Glad we were able to help, and thanks for sharing your solution for others to use. If you have any further questions please create a new thread and we will gladly try to help you. Thank you for using Enfold.

    Best regards,
    Mike

Viewing 9 posts - 1 through 9 (of 9 total)
  • The topic ‘Set image source filename in Javascript to swap/switch for responsive images’ is closed to new replies.