I want to scroll to the top of the page after getting an ajax call response using angularjs. Basically, I am displaying alert messages on top of the page and I want to focus the alert message as the ajax response received.
Thanks
I want to scroll to the top of the page after getting an ajax call response using angularjs. Basically, I am displaying alert messages on top of the page and I want to focus the alert message as the ajax response received.
Thanks
You can use
where
x
is the pixel along the horizontal axis andy
is the pixel along the vertical axis.Scroll to top
Focus on element
Example
Update:
Also you can use
$anchorScroll
Example
Ideally we should do it from either controller or directive as per applicable. Use
$anchorScroll
,$location
as dependency injection.Then call this two method as
Here
scrollToDivID
is the id where you want to scroll.Assumed you want to navigate to a error message div as
For more information please see this documentation
You can use
$anchorScroll
.Just inject
$anchorScroll
as a dependency, and call$anchorScroll()
whenever you want to scroll to top.Don't forget you can also use pure JavaScript to deal with this situation, using:
use:
$anchorScroll();
with
$anchorScroll
property