How can I dynamically hide/show a section in Webflow based on the time of day?
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:
-
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.
-
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".
-
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.
-
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>
-
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:
- How do I create a section in Webflow?
- How can I set the initial visibility of an element in Webflow?
- What is the purpose of custom code in Webflow?