Hey @jackmielke!
Use CSS to adjust hashlink placement
In the Data Tab, scroll to the “Edit JS + CSS” button and open the code panel. In the CSS panel, use your hashlink to select the element and include the :target
psuedoclass. Add some scroll-margin-top
to the element so that it’s pushed away from the very top of the page. For example, if you have a section called “Returns” and you gave it the ID of ‘returns’, your code would look like this:
#returns:target {
scroll-margin-top: 100px;
}
Use Javascript to scroll to an element
It’s also possible to do this with Javascript, which opens up some additional alignment options and also allows you to execute other stuff (like maybe applying styles) at the same time.
First, select the element that, when clicked, will link to another location on the page. Head to the Data tab in the Visual Editor and give it an Element Event
of Click
. Select “Edit Action” and then “Custom Code”.
Paste in the following code:
let section = document.querySelector('#SECTION_ID_GOES_HERE');
section.scrollIntoView({behavior: "smooth", block: "center", inline: "nearest"});
Then, edit the ‘#SECTION_ID_GOES_HERE’ to match the hashlink of the element. For example, if you have a section called “Returns” and you gave it the ID of ‘returns’, your code would look like this:
let section = document.querySelector('#returns');
section.scrollIntoView({behavior: "smooth", block: "center", inline: "nearest"});
If the element scrolls into view but now is no longer aligned to the right spot, you can change the block: "center"
of the second line of code to different alignments: start
, center
, end
, or nearest
.
You can also add other lines of Javascript after this, such as this code, which animates the addition of a subtle drop shadow to the element that was clicked:
event.target.style.transition = '.3s ease-in-out';
event.target.style.boxShadow = '0px 0px 50px 0px rgba(0,0,0,0.25)';
Thank you!
~Logan from Builder.io