I added below function on click event. But wasnt getting expected results.
function scrollToTargetAdjusted() {
var element = document.getElementById(‘targetElement’);
var headerOffset = 45;
var elementPosition = element.getBoundingClientRect().top;
var offsetPosition = elementPosition + window.pageYOffset - headerOffset;
window.scrollTo({
top: offsetPosition,
behavior: "smooth"
});
}
Hi Bindiya!
You can achieve the scrollTo event in the builder.io by simply adding custom code to onClick
event of a button. You can find more at Custom Code in the Builder Visual Editor.
You can also refer to the below code which worked for me. Thank you!
var element = document.getElementById('targetElement');
var headerOffset = 45;
var elementPosition = element.getBoundingClientRect().top;
var offsetPosition = elementPosition + window.pageYOffset - headerOffset;
window.scrollTo({
top: offsetPosition,
behavior: 'smooth'
});
Hello Manish, I have added links as shown in [Linking within pages in Builder.io - Builder.io]. But want to add a offset to targeted id. could you help?
Hi @Bindiya,
If you want to add an offset for scroll to the target element, I recommend binding the element with an onClick event to the links instead and adding the below lines of code to the event custom code section
var element = document.getElementById('targetElement');
var headerOffset = 45;
var elementPosition = element.getBoundingClientRect().top;
var offsetPosition = elementPosition + window.pageYOffset - headerOffset;
window.scrollTo({
top: offsetPosition,
behavior: 'smooth'
});
I have added below code for alphabet a onclick event.
var element = document.getElementById(‘section-a’);
var headerOffset = 80;
var elementPosition = element.getBoundingClientRect().top;
var offsetPosition = elementPosition + window.pageYOffset - headerOffset;
window.scrollTo({
top: offsetPosition,
behavior: ‘smooth’
});
Can be checked on click alphabet a from horizontal alphabet list.
https://qa.olivela.com/page/designer-directory-url-2
Offset is not getting set.
Hi @Bindiya,
The link seems to be working for me.
Can you try console.log(offsetPosition)
and check if there is an error on the js console?
Getting this error
_app-5dd6b45621fe87a7.js:1 Builder custom code error: Cannot read properties of null (reading ‘getBoundingClientRect’) in var element=document.getElementById(“targetElement”),headerOffset=75,elementPosition=element.getBoundingClientRect().top,offsetPosition=elementPosition+window.pageYOffset-headerOffset;window.scrollTo({top:offsetPosition,behavior:“smooth”}) TypeError: Cannot read properties of null (reading ‘getBoundingClientRect’)
Hi @Bindiya,
I don’t see any id with targetElement
on your page. Can you try the below code
var element=document.getElementById("section-z")
instead of
var element=document.getElementById(“targetElement”)
.
If you still have any issues then kindly share your builder editor content URL. Thank you!
I have added id “section-a”
Will you be able to share your builder content link?
Sorry, you are working on localhost so that content is not accessible.
Can you share screenshots of the code and the HTML id attribute?
Hi @Bindiya,
Could you please remove the Link URL and then try since there is an onClick event which will do the scroll to the section we don’t need the Link URL.
Without link function doesnt work
Hi @Bindiya,
I’ve got your ‘C’ working, can you check once?
Thanks Manish, it’s working as expected
1 Like