Editing the content using the # symbol

I've noticed quite a few websites that use the system I'm interested in. The system works like this: There is only one page and when you click a link in the menu, the page smoothly slides to that specific area using the # mark.

The URL typically appears as: example.com/#content1

While I couldn't find an exact current example, I can explain it like this:

---- menu (usually fixed) -----

First content


Second Content


And so on.

When clicking any link in the menu, the page transitions to that particular section seamlessly.

How can I implement this design on my website?

Answer №1

To create a targeted menu, assign specific ids to the elements you want to link to and ensure these ids match the hash fragment (e.g., #xyz).

For example:

<a href="#first">First</a>

Clicking the above link will direct the user to this corresponding element:

<div id="first">...</div>

You mentioned "sliding." By default, the page instantly jumps to the target instead of sliding. To achieve sliding, override the default behavior with an animation. Although recommending specific plugins is off-topic for SO, here is a simple jQuery example based on your question:

// Handle clicks on our navigation anchors
$(".nav a").on("click", function(e) {
  // Get the href attribute, which will be #first or similar, and get the element using that
  // as a CSS selector
  var hash = this.getAttribute("href");
  var target = $(hash);
  // Tell jQuery to animate us to that location. Note that some
          browsers animate body, others `html`, so we 
          do both
  $('body, html').animate({
    scrollTop: target.position().top
  }, 800);
  
  // Prevent the usual jump
  e.preventDefault();
  
  // Set the hash *without* causing the jump (for bookmarks and such)
  if (history && history.replaceState) {
    history.replaceState(null, null, hash);
  }
});
.fill {
  height: 5em;
}
<ul class="nav">
  <li><a href="#first">First</a></li>
  <li><a href="#second">Second</a></li>
  <li><a href="#third">Third</a></li>
  <li><a href="#fourth">Fourth</a></li>
</ul>
<div class="fill">
  Some content to fill space
</div>
<div id="first" class="fill">This is the first target</div>
<div id="second" class="fill">This is the second target</div>
<div id="third" class="fill">This is the third target</div>
<div id="fourth" class="fill">This is the fourth target</div>
<div class="fill">More filler</div>
<div class="fill">More filler</div>
<div class="fill">More filler</div>
<div class="fill">More filler</div>
<div class="fill">More filler</div>
<div class="fill">More filler</div>
<div class="fill">More filler</div>
<div class="fill">More filler</div>
<div class="fill">More filler</div>
<div class="fill">More filler</div>
<div class="fill">More filler</div>
<div class="fill">More filler</div>
<div class="fill">More filler</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

Answer №2

Anchor tags are defined by assigning an ID to an element in HTML.

For example, if you have a link like this:

www.yoursite.com/whatever.html#gohere

And a paragraph element with an id of 'gohere'

<p id="gohere"></p>

When you click the link, it will scroll down to that specific element on the page.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Creating Conditional Connections in jsPlumb

Attempting to establish connections between nodes based on specific rules, such as: The "API request" endpoint is restricted from connecting to any node except "Initiate Call". The "Failed" endpoint cannot be linked to "Play Audio", and so forth. Below ...

Difficulty encountered when applying a CSS class with JavaScript

The Javascript function I am currently using is able to select multiple links. This behavior occurs because of the Regular expression I applied with the '^' symbol. I decided to use this approach because my links are in the following format: htt ...

Changing the Color of a Table Cell with jQuery

I have designed a table cell in the following way <tr> <td id = 'even'>row 8, cell 1</td> <td id = 'odd'>row 8, cell 2</td> </tr> The colors and font sizes are defined using the CSS below #even { ...

Step-by-step guide on how to load an Ext JS tab after clicking on it

I have an Ext JS code block that creates 4 tabs with Javascript: var tabs; $(document).ready( function() { fullscreen: true, renderTo: 'tabs1', width:900, activeTab: 0, frame:true, ...

Progress Bar Modules

I am currently working on creating a customizable animated progress bar that can be utilized as follows: <bar [type]="'health'" [percentage]="'80'"></bar> It is functional up to the point where I need to adjust different p ...

Organizing CSS elements for input into a database

SCENARIO: In my current project, I am working on a CMS website where each user's custom CSS is stored in a database table with specific fields: int id (primary key) int site_id (index) string selector string property strin ...

jquery events such as onSubmit, onComplete, etc. are commonly used

It's interesting to observe that many tools in jQuery, especially those involving ajax, contain hooks like onSubmit, onComplete, and more. Is there a specific term for this practice and the standards associated with it? Does this practice belong sol ...

JavaScript keeps repeating as you perform basic addition operations

Dealing with a similar issue where things are not adding up correctly, I encountered a problem in a previous question about JavaScript variables only concatenating as strings. The pure JavaScript solution worked fine, so I have now consolidated all the cod ...

Enhancing user experience with autocomplete functionality using jQuery combined with server

Struggling with autocompleting a search field using jQuery-UI and encountering issues when trying to integrate PHP as the data source. The setup works smoothly with a variable as the source. JS: $(function () { var data = [ "ActionScript", ...

Center a grid of cards on the page while keeping them aligned to the left

I have a grid of cards that I need to align in the center of the page and left within the grid, adjusting responsively to different screen sizes. For example, if there are 10 cards and only 4 can fit on a row due to screen size constraints, the first two ...

Ways to deactivate selecting rows in a datatable

Is there a way to deactivate the select feature specifically within datatables? An example of using ctrl+a for disabling select all function is available here https://i.stack.imgur.com/RQNXT.png ...

When a Grid Item is enclosed in a link, it does not extend over rows

After avoiding HTML/CSS for a long time, I finally decided to dive in and create a simple CSS grid website with 3 items in the grid. Everything was working well and looking exactly as I wanted it to: https://i.sstatic.net/zVQ9U.jpg However, I had the ide ...

Positioning a div to the right of a floated element, yet beneath two additional elements

I have a CSS dilemma that's really getting to me. The code I currently have places the description text under the 'title' but I want it to continue over, under the 'button'. I've experimented with different arrangements, but n ...

The dynamic drop-down menu is giving incorrect values when the onchange function is called

Trying to implement Google Analytics tracking on my dynamic dropdown menu in WordPress has been a bit tricky. I want to be able to track when users click on any of the options and display the name of the selected value, not just the ID. However, I've ...

What is the best way to separate a table column into a distinct column at the specified delimiter count?

I successfully wrote code that splits the third column into new columns at the slash delimiter. However, I am struggling to modify it to split at the nth (i.e. 2nd) occurrence. I couldn't find a solution online, so I'm reaching out here for help ...

Are there differences in alignment?

I'm looking to create a unique visual effect where text is aligned differently on each line, shifting left, right, center, or wherever else, with the alignment origin varying by just a few pixels. This would be perfect for adding some visual interest ...

An abundant array of options spread across numerous dropdown menus

I have been tasked by a client to do something new that I haven't done before, so I am seeking the best approach. The project involves creating a form with three dropdown menus where users can select from thousands of options. For instance: Users mu ...

Add a CSS class to the text that is selected within a Content Editable div

Hey there, I'm having an issue where the class is being applied to the button when pressed instead of the selected text. Here's my current code: The button needs to be a div, but it might be causing the problem. I just want the highlighted text ...

Fade effect on content in Bootstrap carousel

I am currently making some updates to the website mcelroymotors.com. One issue I have encountered while working on the homepage carousel is that the caption only pops up on the first slide, and does not appear on any of the subsequent slides. I would lik ...

I'm puzzled as to why my fixed element is gravitating towards the right side of the entire screen instead of aligning with the right side of its parent element

I have inserted a code snippet that highlights my issue. I am attempting to ensure that the .navigation element remains fixed at the top of the colored divs, however, when using 'right: 0;', the .navigation element seems to shift to the right sid ...