FREE CODE #006 UI Components

Interactive Day & Night Landscape Toggle Animation

A polished day-to-night landscape animation built with HTML, CSS, JavaScript, and SVG. Switch between daylight and nighttime as the sky changes, the sun and moon move, stars appear, cottage lights turn on, and the entire scene smoothly transforms.

HTML5 CSS3 JavaScript jQuery SVG Web Animations API Web Audio API
VIEWS28
DOWNLOADS11
FORMATZIP
LICENSEFree to use
01 / ABOUT THIS RESOURCE

Use the same code shown in the video.

Interactive Day & Night Landscape Toggle Animation is a creative frontend component built with pure HTML, CSS, JavaScript, and SVG.


Instead of using a basic light-mode and dark-mode switch, this component transforms an entire miniature landscape between daytime and nighttime.


The scene begins with a bright daytime environment featuring a soft blue sky, clouds, green hills, trees, a cottage, a winding path, and a warm natural color palette.


When the interaction is activated, the whole landscape gradually transitions toward night.


The sky moves through several color stages instead of instantly changing from light to dark. The daytime colors blend through warm sunset tones before settling into a deep nighttime palette.


At the same time, the sun moves down toward the horizon while the moon rises into the scene.


The daytime clouds gradually fade and move away, while groups of stars appear in stages across the night sky. Some stars use subtle twinkle animations to keep the completed night scene feeling alive.


The landscape itself also changes with the time of day. Hills, trees, paths, cottage walls, roof colors, vegetation, and surrounding details smoothly shift into darker nighttime tones.


As evening arrives, the cottage windows begin to light up one by one. The attic window follows slightly later, and the porch light appears near the end of the transition, creating a warm contrast against the cooler night colors.


A small meteor effect can also appear after the night transition has completed, adding an extra detail to the scene.


The interaction works in both directions. Clicking the landscape while it is transitioning can request the opposite state without producing a sudden visual jump.


The animation preserves the current position and movement speed before reversing direction, which helps the transition feel continuous rather than restarting from the beginning.


An optional Auto Cycle mode is included. When enabled, the scene automatically moves between day and night, making it useful for portfolio showcases, landing pages, UI demonstrations, and social media recordings.


The component also includes accessibility support. The landscape behaves as an interactive switch, updates its ARIA state, provides live text describing the current scene, supports keyboard activation, and respects the user's reduced-motion preference.


Everything is contained inside a single standalone HTML file.


No Bootstrap, Tailwind CSS, React, jQuery, animation library, image package, or third-party dependency is required.


Key Features


• Animated day-to-night transition

• Multi-stage sky color transformation

• Moving sun animation

• Rising moon animation

• Animated cloud movement

• Progressive star reveal

• Twinkling star effects

• Nighttime meteor animation

• Animated cottage lighting

• Staggered window illumination

• Porch light animation

• Dynamic landscape color changes

• Smooth hill and vegetation transitions

• Reversible interaction

• Motion-preserving direction changes

• Auto Cycle mode

• Responsive SVG design

• Accessible switch behavior

• ARIA state updates

• Live screen-reader descriptions

• Keyboard-friendly interaction

• Reduced-motion support

• Pure HTML, CSS, JavaScript, and SVG

• No external libraries

• No external image assets


How It Works


The component represents the scene using a transition value between 0 and 1.


A value near 0 represents the daytime scene, while a value near 1 represents the nighttime scene.


During the animation, JavaScript uses that transition value to update many parts of the illustration.


The sky uses several color stages, allowing the scene to move naturally through daylight, sunset, dusk, and night rather than performing a simple two-color fade.


The colors of the hills, trees, house, roof, path, vegetation, and other environment elements are also interpolated during the transition.


The sun and moon follow separate curved paths.


As night approaches, the sun moves downward and away from its daytime position while the moon rises into the visible sky.


Clouds fade and move away during the transition.


Stars appear gradually in separate groups so the sky does not switch from empty to fully populated at once.


The cottage lighting is also staggered.


The left window begins glowing first, followed by the right window, attic window, and porch light.


This timing helps the environment feel as if evening is naturally arriving.


Smooth Reversible Animation


The transition uses interpolation that keeps both the current position and movement velocity when the direction changes.


This means the user can click again before the first animation finishes and the scene will smoothly reverse direction instead of snapping or restarting.


Nighttime Details


Once the scene reaches night:


• Stars remain visible

• Star twinkle animations become active

• Cottage windows stay illuminated

• The porch light remains visible

• The moon stays in the night position

• A meteor can briefly travel through the sky


Daytime Details


When the scene returns to day:


• Stars fade away

• The moon moves out of the main scene

• The sun returns

• Clouds become visible again

• Cottage lights turn off

• Landscape colors return to their brighter daytime palette


Auto Cycle


The Auto Cycle option can repeatedly move the scene between day and night.


This is useful for:


• Portfolio presentations

• Creative landing pages

• Demo recordings

• Social media content

• UI showcases

• Ambient website sections


Accessibility


The landscape uses switch semantics and updates its checked state when changing between day and night.


A live description communicates whether the scene is currently daytime, nighttime, or transitioning.


The interactive control can also be activated with a keyboard.


The component detects prefers-reduced-motion and immediately moves to the requested state for users who prefer less motion.


Responsive Design


The scene is built as scalable SVG artwork.


It automatically scales with the available width while preserving its proportions and visual quality.


Customization


The component can be customized to create a completely different environment.


You can modify:


• Sky colors

• Sunset colors

• Night palette

• Landscape colors

• House design

• Trees

• Sun position

• Moon position

• Star patterns

• Window-light timing

• Animation speed

• Auto Cycle timing

• Meteor behavior

• Background colors


You can also replace the existing landscape illustration with your own SVG artwork while keeping the same day-to-night animation system.


Recommended Use Cases


• Day/night theme interactions

• Creative landing pages

• Portfolio websites

• Interactive hero sections

• Weather interfaces

• Smart-home dashboards

• Travel websites

• Storytelling pages

• Creative UI experiments

• SVG animation practice

• Frontend portfolio projects

• UI/UX inspiration

• Social media coding demos


Important Note


This component visually demonstrates a day and night state.


It does not automatically change based on the user's real local time, weather, sunrise, sunset, or location.


If you want real-world behavior, connect the interaction to your own time, weather, location, or application-state logic.

READY TO BUILD?

Download the code and customize it.

No account is required. Downloads pass through Ahmad Khan Codes so the resource can keep an accurate download count.

Download ZIP