It is possible to move to a certain position on a page using #elementId
. How can I do the same thing using Javascript / Jquery. When a JS function is called, I want to scroll to the specific position on that page.
After much googling I found that you just need to do this:
location.hash = "elementId"
Here's an example function that I tested on today's New York Times front page using the browser console:
function scrollToElement(pageElement) { var positionX = 0, positionY = 0; while(pageElement != null){ positionX += pageElement.offsetLeft; positionY += pageElement.offsetTop; pageElement = pageElement.offsetParent; window.scrollTo(positionX, positionY); } } var pageElement = document.getElementById("insideNYTimesHeader"); scrollToElement(pageElement);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With