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

How Can I Scroll to a Specific Anchor Using JavaScript?

Susan Sarandon
Release: 2024-11-16 15:20:03
Original
225 people have browsed it

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>
Copy after login

or

<h1>
Copy after login

Solution:

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

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

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!

source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template