3

I'm scrolling the page if the current Customer Community User is not a 'Guest User', but the page movement is abrupt. Is there an attribute I can set on scrollTo() for more elegant page scrolling in lightning components (similar to behavior on window.scroll)?

My Current Code

scrollTo('custom', 600, 200);

1 Answer 1

5

scrollTo(target, top, left) isn't a valid set of parameters for scrollTo, so presumably you're using a custom function. The way you'd do this using just standard JavaScript/Lightning would look like this:

var target = component.find("target");
var element = target.getElement();
var rect = element.getBoundingClientRect();
scrollTo({top: rect.top, behavior: "smooth"});
3
  • I actually wasn't able to find proper params for scrollTo(), wasn't aware that the they are contained in an object. I was able to use behavior parameter for my purposes - thank you. Aug 13, 2018 at 0:49
  • It is taking me to the top of the Page, but I want to navigate on a particular pixel. That pixel is not static, It may be changed. May 17, 2022 at 21:15
  • 1
    @RajatJaiswal It does work correctly, both in Aura and LWC. I suspect that you may getting tripped up with a weird getBoundingClientRect value, which is why you'd be scrolled to the top. Double-check your values.
    – sfdcfox
    May 17, 2022 at 21:30

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .