Overview
The Siloé Perú website uses vanilla JavaScript (no frameworks) for all interactive features. The JavaScript code is embedded directly inindex.html (lines 726-856) and handles:
- Section navigation system
- 3D flip card interactions
- Form validation
- Smooth scrolling
The website uses plain JavaScript (ES5/ES6) with no external dependencies, making it lightweight and easy to understand.
Code Structure
All JavaScript is wrapped in a DOMContentLoaded event listener to ensure the DOM is fully loaded:index.html (lines 730-855)
Navigation System
Getting DOM References
The script starts by grabbing references to all navigation buttons and section containers (lines 734-741):index.html
Debug Logging
The code includes helpful console logs for debugging (lines 743-752):index.html
Section Switching Function
The core navigation logic is in themostrarSeccion() function (lines 755-793):
index.html
How It Works
Show Selected Section
Based on the section name parameter, adds
seccion-activa to the target section and nav-btn-active to its buttonEvent Listeners
Each button has a click event listener (lines 796-823):index.html
e.preventDefault() prevents the default button behavior. The if checks ensure the code doesn’t crash if an element is missing.Flip Card Interaction
Selecting All Flip Cards
The script selects all flip cards usingquerySelectorAll (lines 832-833):
index.html
Click Event Handler
Each flip card gets a click listener (lines 836-853):index.html
How Flip Cards Work
Check for Previously Flipped Card
If another card is already flipped and it’s not the current one, unflip it by removing the
flipped classThis ensures only one card is flipped at a time, creating a clean user experience.
State Management
The code uses a simple state management pattern:- Set to the card element when a card is flipped
- Set to
nullwhen the card is unflipped - Used to check if another card needs to be unflipped first
Smooth Scrolling
The navigation system includes smooth scrolling to top:- Modern Browsers
- Legacy Fallback
Event Delegation Pattern
The code uses direct event listeners on each element rather than event delegation. This is appropriate given:- Fixed number of navigation buttons (4)
- Fixed number of sections (4)
- Flip cards are selected once on page load
For dynamically added content, you would want to use event delegation on a parent element instead.
Browser Compatibility
The JavaScript uses modern but widely supported features:| Feature | Browser Support |
|---|---|
addEventListener | All modern browsers |
classList.add/remove/toggle | All modern browsers |
querySelectorAll | All modern browsers |
forEach | All modern browsers |
| Arrow functions | ES6+ (transpile for IE11) |
const/let | ES6+ (transpile for IE11) |
Adding New Interactive Features
Example: Adding a “Back to Top” Button
Debugging Tips
Console Logging
Add strategic console.log statements:Browser DevTools
Use browser DevTools to:- Set breakpoints in the Sources panel
- Inspect element references in the Console
- Monitor event listeners in the Elements panel
- Watch variable values during execution
Common Issues
Elements not found
Elements not found
Symptom:
Cannot read property 'addEventListener' of nullSolution:- Check that element IDs match exactly
- Verify script runs after DOM is loaded
- Check for typos in
getElementById()calls
Events not firing
Events not firing
Symptom: Click does nothingSolution:
- Check Console for JavaScript errors
- Verify event listeners are attached
- Make sure element isn’t being covered by another element
- Check z-index values
Classes not applying
Classes not applying
Symptom: Visual state doesn’t changeSolution:
- Verify CSS classes are defined in style.css
- Check class names for typos
- Inspect element in DevTools to see which classes are applied
- Check for CSS specificity conflicts
Performance Considerations
Efficient Selectors
Use
getElementById (fastest) when possible instead of querySelectorEvent Throttling
For scroll events, consider throttling to reduce function calls
Minimize Reflows
Batch DOM changes together to minimize browser reflows
Remove Unused Code
Remove console.log statements in production for slight performance gain
Next Steps
Responsive Design
Learn about mobile-first responsive patterns
Navigation Component
Deep dive into the navigation system component
Flip Cards Component
Explore the 3D flip card implementation
Content Customization
Customize text and messaging