Home  >  Article  >  Web Front-end  >  How Can I Scroll to a Specific Anchor Using JavaScript?

How Can I Scroll to a Specific Anchor Using JavaScript?

Susan Sarandon
Susan SarandonOriginal
2024-11-16 15:20:03140browse

How Can I Scroll to a Specific Anchor Using JavaScript?

Scrolling to a Specific Anchor using JavaScript

To navigate a webpage to a particular anchor using JavaScript, follow these steps:

Scenario:

You have HTML tags with specified names or IDs:

<a name="anchorName">...</a>

or

<h1>

Solution:

To achieve the same navigation effect as visiting http://server.com/path#anchorName:

function scrollTo(hash) {
  location.hash = "#" + hash;
}

No external libraries like jQuery are needed!

The above is the detailed content of How Can I Scroll to a Specific Anchor Using JavaScript?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn