How can I dynamically hide/show a section in Webflow based on the time of day?

Published on
September 22, 2023

To dynamically hide/show a section in Webflow based on the time of day, you can use custom code and the power of JavaScript. Follow these steps to achieve this functionality:

  1. Create a section: First, create the section you want to hide or show based on the time of day. Set its initial visibility to either visible or hidden, depending on your needs.

  2. Add custom code: In the Webflow Designer, navigate to the page where you want to implement this functionality. Click on the "Settings" tab in the right sidebar, and then select "Custom Code".

  3. Create a script: Within the "Custom Code" tab, click on the "Head Code" tab. Here, you need to write a JavaScript function that will handle the logic to show or hide the section based on the time of day.

  4. Implement the JavaScript function: To implement the JavaScript function, use the following code:

<script>function showHideSectionBasedOnTime() {  var now = new Date();  var hour = now.getHours();  var section = document.getElementById('section-id'); //Replace 'section-id' with the actual ID of your section    if (hour >= 8 && hour <= 17) { // Change the time range as per your requirements    section.style.display = 'block'; // Show the section  } else {    section.style.display = 'none'; // Hide the section  }}</script>
  1. Call the JavaScript function: Lastly, call the showHideSectionBasedOnTime() function by placing the following code just before the closing </body> tag:
<script>  showHideSectionBasedOnTime();</script>

Now, the section you specified will be dynamically hidden or shown based on the time of day.

Note: Don't forget to replace 'section-id' with the actual ID of your section, which you can find in the Webflow Designer when you select the section.

Additional Questions:

  1. How do I create a section in Webflow?
  2. How can I set the initial visibility of an element in Webflow?
  3. What is the purpose of custom code in Webflow?