1

I feel like I've tried everything, but I just can't seem to get this working. I have a variable called 't' that stores the value of $mobile_menu_height. This value is currently set to 280px;

var t = "<?php $mobile_menu_height ?>";  //this is currently 280px

Further down in the script I would like to get rid of the hard coded "288px" (I will leave 8px hardcoded) in this line

y.style.height = "calc(100vh - 288px)";

and instead be using the variable (I've tried using 't' and '$mobile_menu_height' but I can't get either to work in my calc() function. Ideally it would look something like this:

y.style.height = "calc(100vh - 8px - t)";

or

y.style.height = "calc(100vh - 8px - $mobile_menu_height)";

But I am simply stuck on how to get this working. Could it possibly be an issue with WordPress sanitization (or lack thereof) of the input of $mobile_menu_height? I have tried inputing both with and without the "px" measurement ie "280" and "280px".

Full script I am working on here:

    <script>
        function toggleMobileNav() {
            var x = document.getElementById("container-mobile-menu");
            var y = document.getElementById("content-container");
            var t = "<?php $mobile_menu_height ?>";

            if (x.style.display === "none") {
                x.style.display = "block";
                y.style.top = t;
                y.style.height = "calc(100vh - 288px)";
            } else {
                x.style.display = "none";
                y.style.top = "62px";
                y.style.height = "calc(100vh - 68px)";
            }
        }
    </script>
2
  • 1
    Can't you just concatenate the string? You should also strongly consider meaningful variable names - variables that are single letters are very difficult to make sense of Commented Aug 23, 2018 at 5:35
  • You need to echo the value: <?php echo $mobile_menu_height ?> or it will simply result in an empty string in your js-code. Commented Aug 23, 2018 at 5:45

2 Answers 2

1

You can use window.innerHeight to get the 100vh equivalent and manually compute like so

const windowH = window.innerHeight + 8 + parseInt(t);
y.style.height = windowH + "px";
Sign up to request clarification or add additional context in comments.

Comments

0

Thanks guys! It used a combination of your answers to fix things. I was indeed missing an echo, and the window.innerHeight worked like magic. I'll clean up those variable names now. Fixed code here for those wondering.

    <?php
        $mobile_menu_height = get_theme_mod( 'mobile_menu_height_setting', '' );
    ?>
    <script>
        function toggleMobileNav() {
            var x = document.getElementById("container-mobile-menu");
            var y = document.getElementById("content-container");
            var mobile_menu_content_drop = "<?php echo $mobile_menu_height; ?>";
            if (x.style.display === "none") {
                x.style.display = "block";
                y.style.top = mobile_menu_content_drop;
                const windowH = window.innerHeight - 8 - parseInt(mobile_menu_content_drop);
                y.style.height = windowH + "px";
            } else {
                x.style.display = "none";
                y.style.top = "62px";
                y.style.height = "calc(100vh - 68px)";
            }
        }
    </script>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.