Tagged: Accordion, screen size, toggles
-
AuthorPosts
-
January 25, 2021 at 11:45 am #1275227
Dear all,
I am using the accordion element (FAQ-style) and would like to set the attribute “behaviour – initial open” in relation to the screen size of the decive. On mobile screens, I would like it to be “0” (i.e., all accordion toggles closed on page load). Whereas on desktop, I would like it to have the value “1” (i.e., first accordion toggle open on page load).
Do you probably have some code for this?
Many thanks in advance.
Best,
Hagen
January 27, 2021 at 10:59 am #1275722Hey HagenWittig,
Thank you for the inquiry.
We could close the active item on mobile view by clicking it using a custom script, or by dispatching a click event on that particular item. Please try to add this code in the functions.php file.
add_action('wp_footer', function() { ?> <script> document.addEventListener("DOMContentLoaded", function() { if(window.innerWidth > 769) return; document.getElementsByClassName("activeTitle")[0].dispatchEvent(new Event('click')); }); </script> <?php }, 9999);
The code should work when the screen width of the device is less than 769px. Let us know if it helps.
Best regards,
IsmaelJanuary 27, 2021 at 8:36 pm #1275868Dear Ismael,
thank you very much. Your solution looks good to me, however, it doesn’t seem to work on my end… ;-)
The particular site I want it to work for is:
https://www.dental.one/terminvereinbarung-neu2
Feel free to check it yourself. The two accordion toggles should be closed on page load on mobile devices. On desktop, I want them to be open on page load.
Further, your code should only influence this particular site and not the rest of the website. Can we add something to solve this?
Thanks again and best regards,
HagenJanuary 28, 2021 at 6:09 am #1275969Hi,
Thank you for the update.
We cannot find the recommended script in the document or page. Did you remove it? Please add the script again so that we could check if it is actually working or if there is any error. Please make sure to copy the code directly from the forum and not from your email.
Best regards,
IsmaelJanuary 28, 2021 at 7:53 pm #1276110This reply has been marked as private.January 29, 2021 at 5:29 am #1276236Hi,
Thank you for the info.
We just noticed that all toggler are opened on page load, so we adjusted the script in the functions.php file a bit to loop through all togglers and close all of them on mobile view. This is the final code.
/*------------------------------------------------------------- // Close open accordion toggles on mobile devices //------------------------------------------------------------*/ add_action('wp_footer', function() { ?> <script> document.addEventListener("DOMContentLoaded", function() { if(window.innerWidth > 769) return; setTimeout(function() { var actives = document.getElementsByClassName("toggler"); Array.prototype.forEach.call(actives, el => { el.dispatchEvent(new Event('click')); el.classList.remove("activeTitle"); }); }, 500); }); </script> <?php }, 9999);
Best regards,
IsmaelJanuary 29, 2021 at 3:17 pm #1276316Dear Ismael,
This is working beautifully. Wow! Thank you so much.
Your script is now executed on all pages of the website. Do you have some additional code to limit its execution to this particular site (i.e., “terminvereinbarung-neu2”)?
Thanks again and best regards,
HagenJanuary 29, 2021 at 3:39 pm #1276327Hi,
You are very welcome! Glad it is working.
To prevent the script from executing on other pages, please look for this line in the script above.
if(window.innerWidth > 769) return;
Below that, add this code.
var terminvereinbarung= document.querySelector(".page-id-3779"); if(!terminvereinbarung) return;
Best regards,
IsmaelJanuary 31, 2021 at 5:08 pm #1276597Dear Ismael,
Fantastic! The page now is looking and behaving like I wanted it to do. Thank you!
Best,
HagenFebruary 1, 2021 at 3:38 am #1276635Hi,
I’m glad this was resolved. If you need additional help, please let us know here in the forums.
Best regards,
Jordan Shannon -
AuthorPosts
- You must be logged in to reply to this topic.