You are looking for a fragment identifier.
Add an id
attribute to the section you want to scroll to.
Example: <section id="shop"> ... </section>
Then, use an anchor <a>
instead of a button, with an href
attribute.
Example: <a href="#shop">Shop</a>
Simple example:
#shop {
margin-top: 300vh;
background-color: tomato;
height: 100vh;
position: relative;
}
#shop::after {
content: 'Shop Content';
position: absolute;
color: white;
font-size: 3em;
top: 0;
left: 0;
width: 100%;
height: 100%;
display: flex;
justify-content: center;
align-items: center;
}
<a href="#shop">Shop</a>
<section id="shop"></section>
If smooth scroll is wanted, scrollIntoView
can be used like so:
document.querySelector('#shopBtn').addEventListener('click', function() {
document.querySelector('#shop').scrollIntoView({
behavior: 'smooth'
});
});
#shop {
margin-top: 300vh;
background-color: tomato;
height: 100vh;
position: relative;
}
#shop::after {
content: 'Shop Content';
position: absolute;
color: white;
font-size: 3em;
top: 0;
left: 0;
width: 100%;
height: 100%;
display: flex;
justify-content: center;
align-items: center;
}
<button id="shopBtn">Shop</button>
<section id="shop"></section>