document.addEventListener('DOMContentLoaded', function () {
// Get the contact element by its ID
var contactElement = document.getElementById('contact');
// Check if the contact element exists
if (contactElement) {
// Scroll to the contact element with smooth behavior
contactElement.scrollIntoView({
behavior: 'smooth'
});
}
});
code
Smooth Scroll to Contact Element on DOM Content Load
This JavaScript code listens for the DOMContentLoaded event, retrieves an element with the ID 'contact', and smoothly scrolls to it when the event is triggered. The comments provide explanations for each part of the code.
DOMContentLoaded
Smooth Scroll
scrollIntoView